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

// GetPage <俱乐部>获取球员数据列表
// @Summary <俱乐部>获取球员数据列表
// @Description <俱乐部>获取球员数据列表
// @Tags <俱乐部>球员
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgClubPlayerGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-club-player [get]
// @Security Bearer
func (e OrgClubPlayer) GetPage(c *gin.Context) {
	req := dto.OrgClubPlayerGetPageReq{}
	s := service.OrgClubPlayer{}
	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.OrgClubPlayerGetPageReply, 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-club-player/get-match [get]
// @Security Bearer
func (e OrgClubPlayer) GetPageMatch(c *gin.Context) {
	req := dto.GetPageMatchClubPlayerReq{}
	s := service.OrgClubPlayer{}
	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
	}

	if req.Id == "" {
		e.Logger.Error(err)
		e.Error(500, err, "id不得为空")
		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-club-player/get [get]
// @Security Bearer
func (e OrgClubPlayer) Get(c *gin.Context) {
	req := dto.OrgClubPlayerGetReq{}
	s := service.OrgClubPlayer{}
	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, "查询成功")
}
