package apis

import (
	"fmt"
	"go-admin/app/operate/models"
	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 OrgClubTeam struct {
	api.Api
}

// GetPage <俱乐部>获取球队数据列表
// @Summary <俱乐部>获取球队数据列表
// @Description <俱乐部>获取球队数据列表
// @Tags <俱乐部>球队
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgClubTeamGetPageReq true "data"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /api/v1/org-club-team [get]
// @Security Bearer
func (e OrgClubTeam) GetPage(c *gin.Context) {
	req := dto.OrgClubTeamGetPageReq{}
	s := service.OrgClubTeam{}
	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.OrgClubTeamGetPageReply, 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-team/get-match [get]
// @Security Bearer
func (e OrgClubTeam) GetPageMatch(c *gin.Context) {
	req := dto.GetPageMatchClubTeamReq{}
	s := service.OrgClubTeam{}
	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.Rounds, &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-team/get [get]
// @Security Bearer
func (e OrgClubTeam) Get(c *gin.Context) {
	req := dto.OrgClubTeamGetReq{}
	s := service.OrgClubTeam{}
	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.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.TeamId, replyGetMatchInfo.Rounds, &replyGetRoundsScoring)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.RoundsScoring = replyGetRoundsScoring

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

// Get <赛事>获取比赛附加数据（比赛详情，联赛，赛区，赛季，球队）
// @Summary <赛事>获取比赛附加数据（比赛详情，联赛，赛区，赛季，球队）
// @Description <赛事>获取比赛附加数据（比赛详情，联赛，赛区，赛季，球队）
// @Tags <赛事>比赛
// @Param id path string false "id"
// @Success 200 {string} string  {data=models.OrgMatchGetInfoReq} "{"code": 200, "data": [...]}"
// @Router /api/v1/org-club-team/info [get]
// @Security Bearer
func (e OrgClubTeam) GetInfo(c *gin.Context) {
	req := dto.OrgClubTeamGetInfoReq{}
	s := service.OrgClubTeam{}
	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)

	//联赛级别列表
	sOrgLeague := service.OrgLeague{}
	listOrgLeague := make([]models.OrgLeague, 0)
	var countOrgLeague int64 = 0

	err = e.MakeContext(c).MakeOrm().MakeService(&sOrgLeague.Service).Errors

	reqOrgLeague := new(dto.OrgLeagueGetPageReq)
	reqOrgLeague.PageIndex = 1
	reqOrgLeague.PageSize = 999

	err = sOrgLeague.GetPage(reqOrgLeague, p, &listOrgLeague, &countOrgLeague)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	//赛季列表
	sOrgSeason := service.OrgSeason{}
	listOrgSeason := make([]dto.OrgSeasonGetPageReply, 0)
	var countOrgSeason int64 = 0

	err = e.MakeContext(c).MakeOrm().MakeService(&sOrgSeason.Service).Errors

	reqOrgSeason := new(dto.OrgSeasonGetPageReq)
	reqOrgSeason.PageIndex = 1
	reqOrgSeason.PageSize = 999

	err = sOrgSeason.GetPage(reqOrgSeason, p, &listOrgSeason, &countOrgSeason)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	//赛区列表
	sOrgDivision := service.OrgDivision{}
	listOrgDivision := make([]models.OrgDivision, 0)
	var countOrgDivision int64 = 0

	err = e.MakeContext(c).MakeOrm().MakeService(&sOrgDivision.Service).Errors

	reqOrgDivision := new(dto.OrgDivisionGetPageReq)
	reqOrgDivision.PageIndex = 1
	reqOrgDivision.PageSize = 999

	err = sOrgDivision.GetPage(reqOrgDivision, p, &listOrgDivision, &countOrgDivision)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	//球队列表
	sOrgTeam := service.OrgTeam{}
	listOrgTeam := make([]dto.OrgTeamGetPageReply, 0)
	var countOrgTeam int64 = 0

	err = e.MakeContext(c).MakeOrm().MakeService(&sOrgTeam.Service).Errors

	reqOrgTeam := new(dto.OrgTeamGetPageReq)
	reqOrgTeam.PageIndex = 1
	reqOrgTeam.PageSize = 999

	err = sOrgTeam.GetPage(reqOrgTeam, p, &listOrgTeam, &countOrgTeam)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取联赛 失败，\r\n失败信息 %s", err.Error()))
		return
	}

	reply := dto.OrgMatchGetInfoReply{
		OrgLeagueList:   listOrgLeague,
		OrgSeasonList:   listOrgSeason,
		OrgDivisionList: listOrgDivision,
		OrgTeamList:     listOrgTeam,
	}

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