package api

import (
	"gadmin/internal/admin/forms"
	"gadmin/internal/admin/service"
	"gadmin/utility"
	"github.com/gin-gonic/gin"
	"time"
)

func GetGrandmaster(ctx *gin.Context) {
	var params forms.GetGrandmasterReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	ctx.JSON(200, service.GrandMaster.Get(ctx, params))
}

func SetGrandmasterDanScore(ctx *gin.Context) {
	var params forms.SetGrandmasterDanScoreReq
	err := ctx.ShouldBind(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	ctx.JSON(200, service.GrandMaster.SetDanScore(ctx, params))
}

func GetGrandmasterList(ctx *gin.Context) {
	var params forms.GrandmasterReportReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	if params.Day == "" {
		params.Day = utility.Format(time.Now().AddDate(0, 0, -1))
	}
	if params.EndDay == "" {
		params.EndDay = utility.Format(time.Now().AddDate(0, 0, 0))
	}
	resp, err := service.GrandMaster.QueryLog(params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"data": resp, "code": 0})
}

func GrandmasterWaves(ctx *gin.Context) {
	var params forms.GrandmasterWavesReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	resp, err := service.GrandMaster.Waves(params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"data": resp, "code": 0})
}

func GrandmasterSeconds(ctx *gin.Context) {
	var params forms.GrandmasterSecondsReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	resp, err := service.GrandMaster.Seconds(params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"data": resp, "code": 0})
}

func GrandmasterDan(ctx *gin.Context) {
	var params forms.GrandmasterDanReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	resp, err := service.GrandMaster.Dan(params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"data": resp, "code": 0})
}

func GrandmasterMatch(ctx *gin.Context) {
	var params forms.GrandmasterMatchReq
	err := ctx.ShouldBindQuery(&params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}

	resp, err := service.GrandMaster.Match(params)
	if err != nil {
		ctx.JSON(200, gin.H{"code": 1, "msg": err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"data": resp, "code": 0})
}
