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/response"

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

type OrgTeamPlayer struct {
	api.Api
}

// 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-player [get]
// @Security Bearer
func (e OrgTeamPlayer) GetPage(c *gin.Context) {
	req := dto.OrgTeamPlayerGetPageReq{}
	s := service.OrgTeamPlayer{}
	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.OrgTeamPlayerGetPageReply, 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 pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-team-player/get-match [get]
// @Security Bearer
func (e OrgTeamPlayer) GetPageMatch(c *gin.Context) {
	req := dto.GetPageMatchTeamPlayerReq{}
	s := service.OrgTeamPlayer{}
	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(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-player/{id} [get]
// @Security Bearer
func (e OrgTeamPlayer) Get(c *gin.Context) {
	req := dto.OrgTeamPlayerGetReq{}
	s := service.OrgTeamPlayer{}
	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, "查询成功")
}
