package apis

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

	"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 OrgTeamMatchEvaluate struct {
	api.Api
}

func (e OrgTeamMatchEvaluate) getTeamId(c *gin.Context) string {
	sOrgTeamUser := service.OrgTeamUser{}
	e.MakeContext(c).MakeOrm().MakeService(&sOrgTeamUser.Service)
	_, teamId := sOrgTeamUser.GetTeamId(user.GetUserId(c))
	return teamId
}

// GetPage <球队>教练评价列表
// @Summary <球队>教练评价列表
// @Description <球队>教练评价列表
// @Tags <球队>教练评价
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-team-match-evaluate [get]
// @Security Bearer
func (e OrgTeamMatchEvaluate) GetPage(c *gin.Context) {
	req := dto.OrgTeamMatchEvaluateGetPageReq{}
	s := service.OrgTeamMatchEvaluate{}
	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.OrgTeamMatchEvaluateGetPageReply, 0)
	var count int64

	req.TeamId = e.getTeamId(c)

	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 pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-team-match-evaluate/get-match [get]
// @Security Bearer
func (e OrgTeamMatchEvaluate) GetPageMatch(c *gin.Context) {
	req := dto.GetPageTeamMatchReq{}
	s := service.OrgTeamMatchEvaluate{}
	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.GetPageTeamMatchReply)

	replyGetMatchInfo := new(cDto.PageMatchInfo)
	err = s.GetMatchInfo(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, "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-team-match-evaluate/{id} [get]
// @Security Bearer
func (e OrgTeamMatchEvaluate) 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.OrgTeamMatchEvaluateUpdateReq true "body"
// @Success 200 {string} string  	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/org-team-match-evaluate/{id} [put]
// @Security Bearer
func (e OrgTeamMatchEvaluate) Update(c *gin.Context) {
	req := dto.OrgTeamMatchEvaluateUpdateReq{}
	s := service.OrgTeamMatchEvaluate{}
	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)

	if req.MatchId == "" || req.Rounds == "" || req.PlayerId == "" {
		e.Logger.Error(err)
		e.Error(500, err, "数据有误")
		return
	}
	userId := user.GetUserId(c)

	sUser := service.OrgTeamUser{}
	e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service)
	err, userData := sUser.GetTeamUser(userId)
	if err != nil {
		e.Logger.Error(err)
		e.Error(131, err, "账户信息有误")
		return
	}

	timeObj := time.Now()
	var contentDate = timeObj.Format("2006-01-02 03:04:05")

	err, playerId := s.IsBeing(req.MatchId, req.Rounds, req.PlayerId)
	eId := 0
	if playerId == 0 {
		reqInsertData := dto.OrgMatchEvaluateInsertReq{
			MatchId:       req.MatchId,
			Rounds:        req.Rounds,
			TeamUserId:    strconv.Itoa(userId),
			PlayerId:      req.PlayerId,
			Content:       req.Content,
			ContentStatus: req.ContentStatus,
			ContentDate:   contentDate,
			ContentImg:    userData.SignImg,
		}
		reqInsertData.CreateBy = userId
		err, eId = s.Insert(&reqInsertData)
	} else {
		req.UpdateBy = userId
		err, eId = s.Update(&req, p)
	}
	fmt.Println(eId)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "修改成功")
}
