package apis

import (
	"fmt"
	cDto "go-admin/common/dto"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/operate/service"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
)

type OrgMatchEvaluate struct {
	api.Api
}

// GetPage <赛事>赛后评价列表
// @Summary <赛事>赛后评价列表
// @Description <赛事>赛后评价列表
// @Tags <赛事>赛后评价
// @Param data body dto.OrgMatchEvaluateGetPageReq true "body"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgMatchEvaluateGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-match-evaluate [get]
// @Security Bearer
func (e OrgMatchEvaluate) GetPage(c *gin.Context) {
	req := dto.OrgMatchEvaluateGetPageReq{}
	s := service.OrgMatchEvaluate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]dto.OrgMatchEvaluateGetPageReply, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// GetPage <赛事>赛后评价比赛列表
// @Summary <赛事>赛后评价比赛列表
// @Description <赛事>赛后评价比赛列表
// @Tags <赛事>赛后评价
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-match-evaluate/get-match [get]
// @Security Bearer
func (e OrgMatchEvaluate) GetPageMatch(c *gin.Context) {
	req := dto.GetPageMatchReq{}
	s := service.OrgMatchEvaluate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)
	reply := new(dto.GetPageMatchReply)

	replyGetMatchInfo := new(cDto.PageMatchInfo)
	err = s.GetMatchInfo(strconv.Itoa(req.Id), p, replyGetMatchInfo)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.MatchInfo = *replyGetMatchInfo

	imgList := make([]cDto.Wonderful, 0)
	err = s.GetPageWonderful(replyGetMatchInfo.EvaluateId, &imgList, 1)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.MatchImg = imgList

	mvList := make([]cDto.Wonderful, 0)
	err = s.GetPageWonderful(replyGetMatchInfo.EvaluateId, &mvList, 2)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.MatchMv = mvList

	replyGetRoundsScoring := make([]cDto.RoundsScoring, 0)
	err = s.GetRoundsScoring(replyGetMatchInfo.PlayerId, replyGetMatchInfo.Rounds, "0", &replyGetRoundsScoring)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.RoundsScoring = replyGetRoundsScoring

	replyGetTotalScoring := make([]cDto.TotalScoring, 0)
	err = s.GetTotalScoring(replyGetMatchInfo.PlayerId, replyGetMatchInfo.Rounds, replyGetMatchInfo.SeasonId, &replyGetTotalScoring)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.TotalScoring = replyGetTotalScoring

	e.OK(reply, "查询成功")
}

// Get <赛事>赛后评价比赛详情
// @Summary <赛事>赛后评价比赛详情
// @Description <赛事>赛后评价比赛详情
// @Tags <赛事>赛后评价
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-match-evaluate/{id} [get]
// @Security Bearer
func (e OrgMatchEvaluate) Get(c *gin.Context) {
	req := dto.OrgMatchEvaluateGetReq{}
	s := service.OrgMatchEvaluate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	p := actions.GetPermissionFromContext(c)

	reply := new(dto.OrgMatchEvaluateGetReply)

	replyGetMatchInfo := new(cDto.PageMatchInfo)
	err = s.GetMatchInfo(strconv.Itoa(req.Id), p, replyGetMatchInfo)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.MatchInfo = *replyGetMatchInfo

	replyGetRoundsScoring := make([]cDto.RoundsScoring, 0)
	err = s.GetRoundsScoring(replyGetMatchInfo.PlayerId, replyGetMatchInfo.Rounds, strconv.Itoa(req.Id), &replyGetRoundsScoring)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.RoundsScoring = replyGetRoundsScoring

	e.OK(reply, "查询成功")
}

// Update <赛事>赛后评价影像维护
// @Summary <赛事>赛后评价影像维护
// @Description <赛事>赛后评价影像维护
// @Tags <赛事>赛后评价
// @Accept application/json
// @Product application/json
// @Param data body dto.OrgMatchEvaluateUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-match-evaluate/{id} [put]
// @Security Bearer
func (e OrgMatchEvaluate) Update(c *gin.Context) {
	req := dto.OrgMatchEvaluateUpdateReq{}
	s := service.OrgMatchEvaluate{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err, playerId := s.IsBeing(req.MatchId, req.Rounds, req.PlayerId)
	eId := 0
	if playerId == 0 {
		reqInsertData := dto.OrgMatchEvaluateInsertReq{
			MatchId:  req.MatchId,
			Rounds:   req.Rounds,
			PlayerId: req.PlayerId,
			MvStatus: req.MvStatus,
			Status:   req.Status,
		}
		err, eId = s.Insert(&reqInsertData)
	} else {
		err, eId = s.Update(&req, p)
	}
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	reqWonderfulInsert := dto.OrgMatchEvaluateWonderfulInsertReq{}
	reqWonderfulUpdate := dto.OrgMatchEvaluateWonderfulUpdateReq{}
	sWonderful := service.OrgMatchEvaluateWonderful{}
	err = e.MakeContext(c).
		MakeOrm().
		MakeService(&sWonderful.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	delIds := make([]int, 0)
	for _, v := range req.MatchImg {
		if v.WonderfulId != "" {
			upId, _ := strconv.Atoi(v.WonderfulId)
			reqWonderfulUpdate.Id = upId
			reqWonderfulUpdate.WonderfulUrl = v.WonderfulUrl
			reqWonderfulUpdate.WonderfulTitle = v.WonderfulTitle
			reqWonderfulUpdate.FileId = v.FileId
			reqWonderfulUpdate.UpdateBy = user.GetUserId(c)

			sWonderful.Update(&reqWonderfulUpdate)
			delIds = append(delIds, upId)
		} else {
			reqWonderfulInsert.Type = "1"
			reqWonderfulInsert.MatchEvaluateId = strconv.Itoa(eId)
			reqWonderfulInsert.WonderfulUrl = v.WonderfulUrl
			reqWonderfulInsert.WonderfulTitle = v.WonderfulTitle
			reqWonderfulUpdate.FileId = v.FileId
			reqWonderfulInsert.CreateBy = user.GetUserId(c)

			sWonderful.Insert(&reqWonderfulInsert)
			delIds = append(delIds, reqWonderfulInsert.Id)
		}
	}
	for _, vMv := range req.MatchMv {
		if vMv.WonderfulId != "" {
			upId, _ := strconv.Atoi(vMv.WonderfulId)
			reqWonderfulUpdate.Id = upId
			reqWonderfulUpdate.WonderfulUrl = vMv.WonderfulUrl
			reqWonderfulUpdate.WonderfulTitle = vMv.WonderfulTitle
			reqWonderfulUpdate.FileId = vMv.FileId
			reqWonderfulUpdate.UpdateBy = user.GetUserId(c)

			sWonderful.Update(&reqWonderfulUpdate)

			delIds = append(delIds, upId)
		} else {
			reqWonderfulInsert.Type = "2"
			reqWonderfulInsert.MatchEvaluateId = strconv.Itoa(eId)
			reqWonderfulInsert.WonderfulUrl = vMv.WonderfulUrl
			reqWonderfulInsert.WonderfulTitle = vMv.WonderfulTitle
			reqWonderfulUpdate.FileId = vMv.FileId
			reqWonderfulInsert.CreateBy = user.GetUserId(c)

			sWonderful.Insert(&reqWonderfulInsert)
			delIds = append(delIds, reqWonderfulInsert.Id)
		}
	}
	reqWonderfulDelete := dto.OrgMatchEvaluateWonderfulDeleteReq{}
	reqWonderfulDelete.Ids = delIds
	reqWonderfulDelete.EId = eId
	sWonderful.Remove(&reqWonderfulDelete)
	e.OK(req.GetId(), "修改成功")
}
