package apis

import (
	"fmt"
	"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/mobile/service"
	"go-admin/app/mobile/service/dto"
	"go-admin/common/actions"
	"go-admin/common/utils"
	"strconv"
)

type OrgPlayerRank struct {
	api.Api
}

// GetPage <手机端>获取球员排名
// @Summary <手机端>获取球员排名
// @Description <手机端>获取球员排名
// @Tags <手机端>比赛排名
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgPlayerRankGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player-rank [get]
// @Security Bearer
func (e OrgPlayerRank) GetPage(c *gin.Context) {
	req := dto.OrgPlayerRankGetPageReq{}
	s := service.OrgPlayerRank{}
	err := c.Bind(&req)
	err = e.MakeContext(c).
		MakeOrm().
		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.OrgPlayerRankGetPageReply, 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
	}

	listOwn := dto.OrgPlayerRankGetPageReply{}
	var countOwn int64
	reqOwn := dto.OrgPlayerRankGetPageReq{}
	reqOwn.TypeName = req.TypeName
	reqOwn.PlayerId = req.PlayerId
	reqOwn.PageIndex = 1
	reqOwn.PageSize = 9999
	err = s.GetPageOwn(&reqOwn, p, &listOwn, &countOwn)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取球队 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	reply := dto.OrgPlayerRankAllData{}
	reply.OwnData = listOwn
	reply.AllData = list

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

// GetPage <手机端>获取球队排名
// @Summary <手机端>获取球队排名
// @Description <手机端>获取球队排名
// @Tags <手机端>比赛排名
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgPlayerRankGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player-rank/team [get]
// @Security Bearer
func (e OrgPlayerRank) GetPageTeam(c *gin.Context) {
	req := dto.OrgPlayerRankGetPageReq{}
	s := service.OrgPlayerRank{}
	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.OrgPlayerRankGetPageReply, 0)
	var count int64

	err = s.GetPageTeam(&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 data body dto.OrgPlayerRankInfoGetReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player-rank/team-info [get]
// @Security Bearer
func (e OrgPlayerRank) GetPageTeamInfo(c *gin.Context) {
	req := dto.OrgPlayerRankInfoGetReq{}
	s := service.OrgPlayerRank{}
	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
	}

	//获取赛季id
	sPlayer := service.OrgPlayer{}
	e.MakeContext(c).MakeOrm().MakeService(&sPlayer.Service)
	req.SeasonId = sPlayer.GetNowSeasonId()

	reply := dto.OrgPlayerRankInfoGetReply{}

	//获取基本信息
	err = s.GetTeamInfoData(req.TeamId, req.SeasonId, &reply)
	//获取排名数据
	err = s.GetRankTeamInfo(req.TeamId, req.SeasonId, &reply)

	//获取平均值数据
	list := make([]dto.OrgPlayerRankGetPageReply, 0)
	var count int64

	err = s.GetPageTeamInfo(req.TeamId, req.SeasonId, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取球队 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	var sumScoring, sumRebound, sumAssist, sumSteal, sumFreeThrow, sumBlockShot, sumFoul, sumTwoPointShot, sumThreePointShot int

	for _, v := range list {
		sumScoring += utils.StringToInt(v.Scoring)
		sumRebound += utils.StringToInt(v.Rebound)
		sumAssist += utils.StringToInt(v.Assist)
		sumSteal += utils.StringToInt(v.Steal)
		sumFreeThrow += utils.StringToInt(v.FreeThrow)
		sumBlockShot += utils.StringToInt(v.BlockShot)
		sumFoul += utils.StringToInt(v.Foul)
		sumTwoPointShot += utils.StringToInt(v.TwoPointShot)
		sumThreePointShot += utils.StringToInt(v.ThreePointShot)
	}
	reply.Scoring = strconv.FormatFloat(float64(sumScoring)/float64(count), 'f', 2, 64)
	reply.Rebound = strconv.FormatFloat(float64(sumRebound)/float64(count), 'f', 2, 64)
	reply.Assist = strconv.FormatFloat(float64(sumAssist)/float64(count), 'f', 2, 64)
	reply.Steal = strconv.FormatFloat(float64(sumSteal)/float64(count), 'f', 2, 64)
	reply.FreeThrow = strconv.FormatFloat(float64(sumFreeThrow)/float64(count), 'f', 2, 64)
	reply.BlockShot = strconv.FormatFloat(float64(sumBlockShot)/float64(count), 'f', 2, 64)
	reply.Foul = strconv.FormatFloat(float64(sumFoul)/float64(count), 'f', 2, 64)
	reply.TwoPointShot = strconv.FormatFloat(float64(sumTwoPointShot)/float64(count), 'f', 2, 64)
	reply.ThreePointShot = strconv.FormatFloat(float64(sumThreePointShot)/float64(count), 'f', 2, 64)

	//获取球员列表
	replyPlayerList := make([]dto.OrgPlayerGetPageReply, 0)
	err = s.GetTeamInfoPlayerList(req.TeamId, &replyPlayerList)
	reply.PlayerList = replyPlayerList

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

// GetPage <手机端>获取联赛列表
// @Summary <手机端>获取联赛列表
// @Description <手机端>获取联赛列表
// @Tags <手机端>联赛列表
// @Param data body dto.OrgLeagueGetPageReq true "data"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /api/v1/org-player-rank/league [get]
// @Security Bearer
func (e OrgPlayerRank) GetPageLeague(c *gin.Context) {
	req := dto.OrgLeagueGetPageReq{}
	s := service.OrgLeague{}
	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.OrgLeagueGetPageReply, 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), 1, 999, "查询成功")
}
