package community

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/models/request"
	responseModel "hongze/hongze_yb/models/response"
	"hongze/hongze_yb/services/bullet_chat"
	"hongze/hongze_yb/services/community"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
)

// VideoList 视频列表
// @Tags 视频社区模块
// @Description 获取视频社区列表
// @Param page_index			query int false "页码"
// @Param page_size				query int false "每页数量"
// @Param keywords				query string false "只看我的"
// @Param video_id				query int false "视频ID"
// @Param variety_tag_id		query int false "标签ID"
// @Success 200 {object}
// @failure 400 {string} string "获取失败"
// @Router /video/list [get]
func VideoList(c *gin.Context) {
	var req request.VideoListReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.PageIndex == 0 {
		req.PageIndex = 1
	}
	if req.PageSize == 0 {
		req.PageSize = utils.PageSize20
	}
	userInfo := user.GetInfoByClaims(c)
	list, err := community.GetVideoList(int(userInfo.UserID), req.PageIndex, req.PageSize, req.VideoId, req.VarietyTagId, req.Keywords)
	if err != nil {
		response.FailMsg("获取失败", "VideoList ErrMsg:"+err.Error(), c)
		return
	}
	idsArr := make([]int, 0)
	for i := range list {
		idsArr = append(idsArr, list[i].CommunityVideoID)
	}

	// 点赞/吐槽数据
	err = community.HandleLikeOrTeaseByCommunityVideoItemList(userInfo.UserID, list)
	if err != nil {
		response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
		return
	}

	// 评论数据
	err = community.HandleCommentByCommunityVideoItemList(list)
	if err != nil {
		response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
		return
	}

	// 弹幕数据
	idBulletsMap, e := bullet_chat.GetListMapBySourceAndIds(bullet_chat.SourceBulletChatVideo, idsArr)
	if e != nil {
		response.FailMsg("获取失败", "获取视频社区弹幕列表Map失败, Err: "+e.Error(), c)
		return
	}
	for i := range list {
		ls := make([]*responseModel.BulletChatItem, 0)
		bs := idBulletsMap[list[i].CommunityVideoID]
		if bs != nil && len(bs) > 0 {
			for k := range bs {
				ls = append(ls, &responseModel.BulletChatItem{
					Id:      bs[k].ID,
					Content: bs[k].Content,
					UserId:  bs[k].UserID,
					Color:   bs[k].Color,
					Seconds: fmt.Sprint(bs[k].Seconds),
				})
			}
		}
		list[i].BulletChatList = ls
	}

	response.OkData("获取成功", list, c)
}

// VideoPlayLog 记录视频播放日志
// @Tags 视频社区模块
// @Description 记录视频播放日志
// @Param video_id		query  int  true  "音频ID"
// @Param source_agent	query  int  true  "操作来源，1：小程序，2：小程序 pc 3:弘则研究公众号，4:web pc"
// @Success 200 {string} string "操作成功"
// @failure 400 {string} string "操作失败"
// @Router /video/play_log [post]
func VideoPlayLog(c *gin.Context) {
	var req request.VideoPlayLogReq
	if err := c.ShouldBind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.VideoId == 0 {
		response.Fail("参数有误", c)
		return
	}
	if req.SourceAgent == 0 {
		response.Fail("参数有误", c)
		return
	}
	userinfo := user.GetInfoByClaims(c)
	newId, errMsg, err := community.SaveVideoPlayLog(userinfo, req.VideoId, req.SourceAgent, 1)
	if err != nil {
		response.FailMsg(errMsg, "VideoPlayLog ErrMsg:"+err.Error(), c)
		return
	}
	response.OkData("操作成功", newId, c)
}
