package media

import (
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/exception"
	"eta/eta_mini_ht_api/common/utils/page"
	"eta/eta_mini_ht_api/controllers"
	"eta/eta_mini_ht_api/service/media"
	"eta/eta_mini_ht_api/service/user"
)

type MediaController struct {
	controllers.ListController
}

func isLogin(listType string) bool {
	if listType == "logout" {
		logger.Info("当前用户未登录,展示部分详情")
		return false
	}
	if listType == "login" {
		return true
	}
	return false
}

// Search 搜索报告列表
// @Description 搜索报告列表
// @Success 200 {object}
// @router /search [get]
func (m *MediaController) Search(mediaType string, key string) {
	controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
		result = m.InitWrapData("分页搜索媒体列表失败")
		if key == "" {
			err = exception.New(exception.SearchKeyEmptyError)
			m.FailedResult("分页搜索报告列表失败", result)
			return
		}
		detailType := m.Data["detailType"].(string)
		//if mediaType == "" || !m.CheckMediaType(mediaType) {
		//	err = exception.New(exception.MediaTypeError)
		//	m.FailedResult("分页查询媒体列表失败", result)
		//	return
		//}
		pageRes := page.Page{
			Current:  m.PageInfo.Current,
			PageSize: m.PageInfo.PageSize,
		}
		if m.PageInfo.LatestId == 0 {
			pageRes.Total, pageRes.LatestId = media.SearchMaxMediaId(mediaType, key)
			m.PageInfo.LatestId = pageRes.LatestId
		} else {
			pageRes.LatestId = m.PageInfo.LatestId
			pageRes.Total = m.PageInfo.Total
		}
		pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
		list, err := media.SearchMediaList(mediaType, key, m.PageInfo, isLogin(detailType))
		if err != nil {
			m.FailedResult("分页搜索报告列表失败", result)
			return
		}
		reports := new(page.PageResult)
		reports.Data = list
		reports.Page = pageRes
		m.SuccessResult("分页搜索报告列表成功", reports, result)
		return
	})
}

// List 获取媒体列表
// @Description 获取媒体列表
// @Success 200 {object}
// @router /list [get]
func (m *MediaController) List(mediaType string, permissionIds string) {
	controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
		result = m.InitWrapData("分页查询媒体列表失败")
		if mediaType == "" || !m.CheckMediaType(mediaType) {
			err = exception.New(exception.MediaTypeError)
			m.FailedResult("分页查询媒体列表失败", result)
			return
		}
		permissionIdList, err := m.TransPermissionIds(permissionIds)
		if err != nil {
			logger.Error("品种列表解析错误:%v", err)
			m.FailedResult("分页查询媒体列表失败", result)
			err = exception.New(exception.QueryReportPageFailed)
			return
		}
		pageRes := page.Page{
			Current:  m.PageInfo.Current,
			PageSize: m.PageInfo.PageSize,
		}
		detailType := m.Data["detailType"].(string)
		var mediaIds []int
		if m.PageInfo.LatestId == 0 {
			pageRes.Total, pageRes.LatestId, mediaIds = media.GetTotalPageCountByPermissionIds(mediaType, permissionIdList)
			m.PageInfo.LatestId = pageRes.LatestId
			m.PageInfo.Total = pageRes.Total
		} else {
			pageRes.LatestId = m.PageInfo.LatestId
			pageRes.Total = m.PageInfo.Total
		}
		pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
		list, err := media.GetMediaPageByIds(mediaType, m.PageInfo, mediaIds, isLogin(detailType))
		if err != nil {
			m.FailedResult("分页查询媒体列表失败", result)
			return
		}
		mediaList := new(page.PageResult)
		mediaList.Data = list
		mediaList.Page = pageRes
		m.SuccessResult("分页查询媒体列表成功", mediaList, result)
		return
	})
}

type RecordCountReq struct {
	MediaId   int    `json:"MediaId"`
	MediaType string `json:"MediaType"`
}

// Count 获取品种列表
// @Description 获取最新发布的报告列表
// @Success 200 {object}
// @router /count [post]
func (m *MediaController) Count() {
	controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
		result = m.InitWrapData("媒体点击记录成功")
		var userInfo user.User
		userInfo = m.Data["user"].(user.User)
		recordReq := new(RecordCountReq)
		m.GetPostParams(recordReq)
		if recordReq.MediaType == "" || !m.CheckMediaType(recordReq.MediaType) {
			err = exception.New(exception.MediaTypeError)
			m.FailedResult("媒体点击记录失败", result)
			return
		}
		record := convertToRecordCount(recordReq)
		record.UserId = userInfo.Id
		record.Mobile = userInfo.Mobile
		err = media.CountMedia(record)
		if err != nil {
			err = exception.New(exception.MediaRecordClickCountFailed)
			m.FailedResult("媒体点击记录失败", result)
			return
		}
		m.SuccessResult("媒体点击记录成功", nil, result)
		return
	})
}

// GetMedia 获取媒体详情
// @Description 获取媒体详情
// @Success 200 {object}
// @router /media [get]
func (m *MediaController) GetMedia(mediaType string, mediaId int) {
	controllers.Wrap(&m.BaseController, func() (result *controllers.WrapData, err error) {
		result = m.InitWrapData("获取媒体详情失败")
		if mediaType == "" || !m.CheckMediaType(mediaType) {
			err = exception.New(exception.MediaTypeError)
			m.FailedResult("获取媒体详情失败", result)
			return
		}
		detailType := m.Data["detailType"]
		mediaDetail, err := media.GetMediaById(mediaType, mediaId)
		switch detailType {
		case "logout":
			logger.Info("当前用户未登录,展示部分详情")
			mediaDetail.Src = ""
			mediaDetail.Login = false
		case "login":
			mediaDetail.Login = true
		}
		if err != nil {
			m.FailedResult("获取媒体详情失败", result)
			return
		}
		m.SuccessResult("获取媒体详情成功", mediaDetail, result)
		return
	})
}
func convertToRecordCount(req *RecordCountReq) media.RecordCount {
	return media.RecordCount{
		MediaId:    req.MediaId,
		MediaType:  req.MediaType,
		Additional: "",
	}
}