package controller

import (
	mhayaGin "github.com/mhaya/components/gin"
	cstring "github.com/mhaya/extend/string"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	mhayaLogger "github.com/mhaya/logger"
)

func (p *Controller) cashOut(c *mhayaGin.Context) {

	var pa param.CashOutReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if cashOut err {. params=%s", pa)
		code.RenderResult(c, code.Error)
		return
	}
	if pa.Tp == 0 || len(pa.PassWord) < 8 || len(pa.PassWord) > 50 {
		code.RenderResult(c, code.Error)
		return
	}
	switch pa.Tp {
	case 1:
		ret, ok := data.DiscreteRuleConfig.Get(models.UCashOutConfig)
		if !ok {
			code.RenderResult(c, code.Error)
			return
		}
		i, _ := cstring.ToInt(ret.Condition)
		if pa.Amount < float64(i) {
			code.RenderResult(c, code.Error)
			return
		}
	case 2:
		ret, ok := data.DiscreteRuleConfig.Get(models.TonCashOutConfig)
		if !ok {
			code.RenderResult(c, code.Error)
			return
		}
		i, _ := cstring.ToInt(ret.Condition)
		if pa.Amount < float64(i) {
			code.RenderResult(c, code.Error)
			return
		}
	default:
		code.RenderResult(c, code.Error)
		return
	}

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if cashOut token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}

	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "cashOut", &pa, nil)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[cashOut] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}

	code.RenderResult(c, code.OK)
}

func (p *Controller) setPwd(c *mhayaGin.Context) {

	var pa param.SetPwdReq
	if err := c.BindJSON(&pa); err != nil {
		mhayaLogger.Warnf("if setPwd err {. params=%s", pa)
		code.RenderResult(c, code.Error)
		return
	}

	if len(pa.ConfirmPassWord) < 8 || len(pa.PassWord) < 8 || len(pa.PassWord) > 50 || len(pa.Mail) < 5 || len(pa.Mail) > 50 {
		code.RenderResult(c, code.PassWordSettingError)
		return
	}

	if pa.PassWord != pa.ConfirmPassWord {
		code.RenderResult(c, code.PasswordInconsistencyError)
		return
	}

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if setPwd token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}
	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "setPwd", &pa, nil)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[setPwd] param = %v, errCode = %v", pa, errCode)
		code.RenderResult(c, errCode)
		return
	}

	code.RenderResult(c, code.OK)
}

func (p *Controller) cashOutList(c *mhayaGin.Context) {

	token, err := p.Auth(c)
	if code.IsFail(err) {
		mhayaLogger.Warnf("if cashOutList token err {. params=%s", c.GetHeader(constant.Token))
		code.RenderResult(c, code.AccountTokenValidateFail)
		return
	}
	var resp []*models.ToUserWithdrawal

	ctl, ok := p.GetChildActor(token.PlayerID)
	if !ok {
		code.RenderResult(c, code.Error)
		return
	}
	errCode := ctl.CallWait(token.TargetPath, "cashOutList", nil, &resp)
	if code.IsFail(errCode) {
		mhayaLogger.Warnf("[cashOutList] errCode = %v", errCode)
		code.RenderResult(c, errCode)
		return
	}

	code.RenderResult(c, code.OK, resp)
}
