package road

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 chart_permission_id		query int false "品种ID"
// @Success 200 {object}
// @failure 400 {string} string "获取失败"
// @Router /road/videos [get]
func VideoList(c *gin.Context) {
	var req request.RoadVideoListReq
	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, code := community.GetRoadVideoList(userinfo, req.PageIndex, req.PageSize, req.VideoId, req.ChartPermissionId, req.Keywords)
	if err != nil {
		if code == response.SPECIFIC_FAIL_CODE {
			response.SpecificFail(list, err.Error(), c)
			return
		}
		response.FailMsg("获取失败", "VideoList ErrMsg:"+err.Error(), c)
		return
	}

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

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

	idsArr := make([]int, 0)
	for i := range list.List {
		idsArr = append(idsArr, list.List[i].RoadVideoID)
	}

	// 弹幕数据
	idBulletsMap, e := bullet_chat.GetListMapBySourceAndIds(bullet_chat.SourceBulletChatRoadVideo, idsArr)
	if e != nil {
		response.FailMsg("获取失败", "获取视频社区弹幕列表Map失败, Err: "+e.Error(), c)
		return
	}
	for i := range list.List {
		ls := make([]*responseModel.BulletChatItem, 0)
		bs := idBulletsMap[list.List[i].RoadVideoID]
		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.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.RoadVideoPlayLogReq
	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, 2)
	if err != nil {
		response.FailMsg(errMsg, "VideoPlayLog ErrMsg:"+err.Error(), c)
		return
	}
	response.OkData("操作成功", newId, c)
}