package apis

import (
	"fmt"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	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 OrgTeamTeam struct {
	api.Api
}

func (e OrgTeamTeam) 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 "页码"
// @Param data body dto.OrgTeamTeamGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-team-team [get]
// @Security Bearer
func (e OrgTeamTeam) GetPage(c *gin.Context) {
	req := dto.OrgTeamTeamGetPageReq{}
	s := service.OrgTeamTeam{}
	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.OrgTeamTeamGetPageReply, 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
	}

	for k, v := range list {
		list[k].Rank = s.GetRankForGetPage(v.SeasonId, v.TeamId)
	}
	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-team/get-match [get]
// @Security Bearer
func (e OrgTeamTeam) GetPageMatch(c *gin.Context) {
	req := dto.GetPageMatchTeamTeamReq{}
	s := service.OrgTeamTeam{}
	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.TeamId), 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.MatchId, &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-team/{id} [get]
// @Security Bearer
func (e OrgTeamTeam) Get(c *gin.Context) {
	//req := dto.OrgTeamGetReq{}
	//s := service.OrgTeam{}
	//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
	//}
	//var object models.OrgTeam
	//
	//p := actions.GetPermissionFromContext(c)
	//err = s.Get(&req, p, &object)
	//if err != nil {
	//	e.Error(500, err, fmt.Sprintf("获取球队失败，\r\n失败信息 %s", err.Error()))
	//	return
	//}

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