package controllers

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hongze_clpt/models"
	"hongze/hongze_clpt/services"
	"hongze/hongze_clpt/utils"
	"strconv"
	"time"
)

// 微路演
type MicroRoadShowController struct {
	BaseAuthController
}

// @Title 微路演列表
// @Description 微路演列表接口
// @Param   PageSize		query	int		true	"每页数据条数"
// @Param   CurrentIndex	query	int		true	"当前页页码,从1开始"
// @Param   KeyWord			query	string	false	"搜索关键词"
// @Param   AudioId			query	int		false	"音频ID"
// @Param   VideoId			query	int		false	"视频ID"
// @Param   ActivityVideoId			query	int		false	"活动视频ID"
// @Param   Filter			query	string		false	"筛选条件 为空:全部 1:视频 2:音频 3:逻辑解析 4:路演回放 多个用 , 隔开"
// @Success 200 {object} models.HomeListResp
// @router /list [get]
func (this *MicroRoadShowController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,用户信息为空"
		br.Ret = 408
		return
	}
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	keywords := this.GetString("KeyWord")
	audioId, _ := this.GetInt("AudioId")
	videoId, _ := this.GetInt("VideoId")
	activityVideoId, _ := this.GetInt("ActivityVideoId")
	filter := this.GetString("Filter")

	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}

	// 微路演列表
	list, total, e := services.GetMicroRoadShowPageListV8(pageSize, currentIndex, audioId, videoId, activityVideoId, filter, keywords)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取微路演列表失败, Err: " + e.Error()
		return
	}

	// 用户权限
	authInfo, permissionArr, e := services.GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取用户权限失败, Err: " + e.Error()
		return
	}

	// 获取默认图配置
	audioMap, videoMap, audioShareMap, videoShareMap, e := services.GetMicroRoadShowDefaultImgConfig()
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取微路演列表失败, Err: " + e.Error()
		return
	}

	for i := range list {
		// 权限
		au := new(models.UserPermissionAuthInfo)
		au.SellerName = authInfo.SellerName
		au.SellerMobile = authInfo.SellerMobile
		au.HasPermission = authInfo.HasPermission
		au.OperationMode = authInfo.OperationMode
		if au.HasPermission == 1 {
			// 非宏观权限进一步判断是否有权限
			if list[i].ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, list[i].ChartPermissionName) {
				au.HasPermission = 2
			}
		}
		// 无权限的弹框提示
		if au.HasPermission != 1 {
			if au.OperationMode == services.UserPermissionOperationModeCall {
				if list[i].Type == 1 {
					au.PopupMsg = services.UserPermissionPopupMsgCallActivity
				} else {
					au.PopupMsg = services.UserPermissionPopupMsgCallMicroVideo
				}
			} else {
				if list[i].Type == 1 {
					au.PopupMsg = services.UserPermissionPopupMsgApplyActivity
				} else {
					au.PopupMsg = services.UserPermissionPopupMsgApplyMicroVideo
				}
			}
		}
		list[i].AuthInfo = au
		list[i].PublishTime = utils.StrTimeToTime(list[i].PublishTime).Format(utils.FormatDate)
		// 默认图
		if list[i].BackgroundImg == "" {
			if list[i].Type == 1 {
				list[i].BackgroundImg = audioMap[list[i].ChartPermissionId]
			} else {
				list[i].BackgroundImg = videoMap[list[i].ChartPermissionId]
			}
		}
		// 分享图
		if list[i].ShareImg == "" {
			if list[i].Type == 1 {
				list[i].ShareImg = audioShareMap[list[i].ChartPermissionId]
			} else {
				list[i].ShareImg = videoShareMap[list[i].ChartPermissionId]
			}
		}
	}

	resp := new(models.MicroRoadShowListResp)
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.List = list
	resp.Paging = page

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 视频详情
// @Description 时间线接口
// @Param   VideoId   query   int  true       "视频ID"
// @Success 200 {object} models.IndustryVideoDetailResp
// @router /detail [get]
func (this *MicroRoadShowController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	videoId, _ := this.GetInt("VideoId")
	videoSimple, au, err := services.GetindustryVideoDetailById(user, videoId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取用户权限失败, Err: " + err.Error()
		return
	}
	resp := new(models.IndustryVideoDetailResp)
	resp.IndustryVideo = videoSimple
	resp.AuthInfo = au
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 记录用户浏览音频回放接口
// @Description 记录用户浏览音频回放接口
// @Param	request	body models.ActivityIdRep true "type json string"
// @Success Ret=200 {object} models.AddVideoHistoryReq
// @router /videoHistory/add [post]
func (this *MicroRoadShowController) VideoHistoryAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,用户信息为空"
		br.Ret = 408
		return
	}
	uid := user.UserId
	var req models.AddVideoHistoryReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	videoId := req.VideoId
	playSeconds := req.PlaySeconds

	var sellerName string
	sellerName, err = models.GetCompanySellerName(user.CompanyId)
	if err != nil {
		br.Msg = "报名失败!"
		br.ErrMsg = "获取对应销售失败,Err:" + err.Error()
		return
	}
	item := models.CygxMicroRoadshowVideoHistory{
		VideoId:     videoId,
		UserId:      uid,
		Mobile:      user.Mobile,
		Email:       user.Email,
		CompanyId:   user.CompanyId,
		CompanyName: user.CompanyName,
		RealName:    user.RealName,
		SellerName:  sellerName,
		PlaySeconds: strconv.Itoa(playSeconds),
		CreateTime:  time.Now(),
		ModifyTime:  time.Now(),
	}
	//if playSeconds != 0 {
	//	lastItem, err := models.GetLastCygxMicroRoadshowVideoHistory(videoId, user.UserId)
	//	if err != nil {
	//		br.Msg = "操作失败"
	//		br.ErrMsg = "操作失败,GetLastCygxMicroRoadshowVideoHistory Err:" + err.Error()
	//		return
	//	}
	//	err = models.UpdateLastCygxActivityVideoHistory(strconv.Itoa(playSeconds), lastItem.Id)
	//	if err != nil {
	//		br.Msg = "更新失败"
	//		br.ErrMsg = "更新失败,UpdateLastCygxActivityVideoHistory Err:" + err.Error()
	//		return
	//	}
	//} else {
	//	err = models.AddCygxMicroRoadshowVideoHistory(&item)
	//	if err != nil {
	//		br.Msg = "操作失败"
	//		br.ErrMsg = "操作失败,Err:" + err.Error()
	//		return
	//	}
	//	err = models.UpdateCygxActivityVideoCounts(videoId)
	//	if err != nil {
	//		br.Msg = "更新失败"
	//		br.ErrMsg = "更新失败,Err:" + err.Error()
	//		return
	//	}
	//}

	err = models.AddCygxMicroRoadshowVideoHistory(&item)
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}
	err = models.UpdateCygxActivityVideoCounts(videoId)
	if err != nil {
		br.Msg = "更新失败"
		br.ErrMsg = "更新失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}