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"
	"strconv"
)

type OrgPlayer struct {
	api.Api
}

// GetPage <手机端>获取球员列表
// @Summary <手机端>获取球员列表
// @Description <手机端>获取球员列表
// @Tags <手机端>球员数据
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Param data body dto.OrgPlayerGetPageReq true "body"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player [get]
// @Security Bearer
func (e OrgPlayer) GetPage(c *gin.Context) {
	req := dto.OrgPlayerGetPageReq{}
	s := service.OrgPlayer{}

	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.OrgPlayerGetPageReply, 0)
	var count int64

	userId := c.GetInt64("userId")
	req.PlayerUserId = strconv.FormatInt(userId, 10)

	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(), "查询成功")
}

// Get <手机端>获取球员详情
// @Summary <手机端>获取球员详情
// @Description <手机端>获取球员详情
// @Tags <手机端>球员数据
// @Param id path string false "id"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/{id} [get]
// @Security Bearer
func (e OrgPlayer) Get(c *gin.Context) {
	req := dto.OrgPlayerGetReq{}
	s := service.OrgPlayer{}
	//err := c.Bind(&req)
	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 dto.OrgPlayerGetReply

	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
	}

	//总分
	var reqTotalScoringGetData dto.OrgPlayerDataGetReq
	reqTotalScoringGetData.PlayerId = strconv.Itoa(object.Id)

	var replyTotalScoringGetData dto.OrgPlayerDataGetReply
	err = s.GetData(&reqTotalScoringGetData, p, &replyTotalScoringGetData)
	if err != nil {
		object.TotalScoring = "0"
	} else {
		object.TotalScoring = replyTotalScoringGetData.Scoring
	}

	//参加赛季
	var reqScoringGetData dto.OrgPlayerDataGetReq
	reqScoringGetData.PlayerId = strconv.Itoa(object.Id)
	reqScoringGetData.SeasonId = object.SeasonId

	var replyScoringGetData dto.OrgPlayerDataGetReply
	err = s.GetDataSeason(&reqScoringGetData, p, &replyScoringGetData)
	if err != nil {
		object.SeasonScoring = "0"
	} else {
		object.SeasonScoring = replyScoringGetData.Scoring
	}

	//出赛次数
	var reqMatchGetData dto.OrgPlayerDataGetReq
	reqMatchGetData.PlayerId = strconv.Itoa(object.Id)

	var replyMatchGetData dto.OrgPlayerDataGetReply
	err = s.GetDataMatch(&reqMatchGetData, p, &replyMatchGetData)
	if err != nil {
		object.CountMatch = "0"
	} else {
		object.CountMatch = replyMatchGetData.CountMatch
	}

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

// Get <手机端>获取球员赛季数据
// @Summary <手机端>获取球员赛季数据
// @Description <手机端>获取球员赛季数据
// @Tags <手机端>球员数据
// @Param playerId path string false "playerId"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/info [get]
// @Security Bearer
func (e OrgPlayer) GetInfo(c *gin.Context) {
	req := dto.OrgPlayerGetInfoReq{}
	s := service.OrgPlayer{}
	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
	}
	var object dto.OrgPlayerGetReply
	reqGet := dto.OrgPlayerGetReq{PlayerId: req.PlayerId}

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&reqGet, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取球员失败，\r\n失败信息 %s", err.Error()))
		return
	}

	//获取当前时间范围的赛季id
	seasonId := s.GetNowSeasonId()

	reply := new(dto.OrgPlayerGetInfoReply)
	reply.OrgPlayerInfo = object

	replyDataAvg := dto.OrgMatchInfo{}
	err = s.StatisticsScoringAvg("", seasonId, "avg", &replyDataAvg)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.DataAvg = replyDataAvg

	replyOrgMatchInfo := dto.OrgMatchInfo{}
	err = s.StatisticsScoringAvg(strconv.Itoa(req.PlayerId), seasonId, "avg", &replyOrgMatchInfo)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.OrgMatchInfo = replyOrgMatchInfo

	err, matchId := s.GetMatchId(strconv.Itoa(req.PlayerId), seasonId)
	var count int64

	replyGetRoundsScoring := make([]dto.OrgPlayerRoundsScoring, 0)
	err = s.GetRoundsScoring(&req, matchId, &replyGetRoundsScoring, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	reply.RoundsScoring = replyGetRoundsScoring

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

// Get <手机端>获取球员生涯数据
// @Summary <手机端>获取球员生涯数据
// @Description <手机端>获取球员生涯数据
// @Tags <手机端>球员数据
// @Param playerId path string false "playerId"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/get-match-season [get]
// @Security Bearer
func (e OrgPlayer) GetOrgMatchSeason(c *gin.Context) {
	req := dto.OrgPlayerGetInfoReq{}
	s := service.OrgPlayer{}
	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
	}
	//生涯数据
	err, matchId2 := s.GetMatchId(strconv.Itoa(req.PlayerId), "")

	replyGetRoundsScoring := make([]dto.OrgPlayerRoundsScoring, 0)
	var count int64
	err = s.GetSeasonRoundsScoring(&req, matchId2, &replyGetRoundsScoring, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取比赛球员信息 失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.PageOK(replyGetRoundsScoring, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get <手机端>获取球员精彩时刻
// @Summary <手机端>获取球员精彩时刻
// @Description <手机端>获取球员精彩时刻
// @Tags <手机端>球员数据
// @Param leagueId path string false "leagueId"
// @Param seasonId path string false "seasonId"
// @Param matchId path string false "matchId"
// @Param rounds path string false "rounds"
// @Param playerId path string false "playerId"
// @Param type path string false "type"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/evaluate [get]
// @Security Bearer
func (e OrgPlayer) GetOrgMatchEvaluate(c *gin.Context) {
	req := dto.OrgPlayerGetOrgMatchEvaluateReq{}
	s := service.OrgPlayer{}
	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
	}

	list := make([]dto.OrgPlayerGetOrgMatchEvaluateReply, 0)
	var count int64

	//p := actions.GetPermissionFromContext(c)
	err = s.GetOrgMatchEvaluate(&req, &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(), "查询成功")
}

// Get <手机端>获取球员评论
// @Summary <手机端>获取球员评论
// @Description <手机端>获取球员评论
// @Tags <手机端>球员数据
// @Param playerId path string false "playerId"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/evaluate-content [get]
// @Security Bearer
func (e OrgPlayer) GetOrgMatchEvaluateContent(c *gin.Context) {
	req := dto.OrgPlayerGetOrgMatchEvaluateContentReq{}
	s := service.OrgPlayer{}
	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
	}

	list := make([]dto.OrgPlayerGetOrgMatchEvaluateContentReply, 0)
	var count int64

	//p := actions.GetPermissionFromContext(c)
	err = s.GetOrgMatchEvaluateContent(&req, &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(), "查询成功")
}

// Get <手机端>精彩时刻比赛轮次列表
// @Summary <手机端>精彩时刻比赛轮次列表
// @Description <手机端>精彩时刻比赛轮次列表
// @Tags <手机端>球员数据
// @Param playerId path string false "playerId"
// @Success 200 {string} string  "{"code": 200, "data": [...]}"
// @Router /mobile/v1/org-player/get-rounds [get]
// @Security Bearer
func (e OrgPlayer) GetOrgRounds(c *gin.Context) {
	req := dto.GetRoundsReq{}
	s := service.OrgPlayer{}
	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
	}

	list := make([]dto.GetRoundsReply, 0)
	var count int64

	//p := actions.GetPermissionFromContext(c)
	err = s.GetOrgRounds(&req, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取球员失败，\r\n失败信息 %s", err.Error()))
		return
	}

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