package v1

import (
	"encoding/json"
	"opp/controllers"
	"opp/protocol"
	"opp/services/rank"

	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
)

type RankController struct {
	controllers.BaseController
}

//GetRankList 排行榜
// @router /getRankList [post]
func (this *RankController) GetRankList() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.GetRankListRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.GetRankList(header, request))
}

//GetRankType 获取榜单类型列表 (年榜/赛季榜)
// @router /getRankTypes [post]
func (this *RankController) GetRankType() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.GetRankTypeRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.GetRankType(header, request))
}

//GetRankRange 获取榜单竞争范围列表
//@router /getRankRanges [post]
func (this *RankController) GetRankRange() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.GetRankRangeRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.GetRankRange(header, request))
}

//GetRankPeriods  获取排行榜周期列表
//@router /getRankPeriods [post]
func (this *RankController) GetRankPeriods() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.GetRankPeriodsRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.GetRankPeriods(header, request))
}

//ComputeRankScore 手动计算排行榜
//@router /computeRankScore [post]
func (this *RankController) ComputeRankScore() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.ComputeRankScoreRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.ComputeRankScore(header, request))
}

//GetRankSortItems 获取排行榜评比项
//@router /getRankSortItems [post]
func (this *RankController) GetRankSortItems() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.GetRankSortItemsRequest
	if err := json.Unmarshal(this.ByteBody, &request); err != nil {
		log.Error(err)
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := controllers.GetRequestHeader(this.Ctx)
	msg = protocol.NewReturnResponse(rank.GetRankSortItems(header, request))
}