package apis

import (
	"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/app/operate/models"
	"go-admin/common/actions"
	"go-admin/common/utils"
	"strconv"
)

type OrgPlayerUser struct {
	api.Api
}

// GetPage <手机端>获取验证码
// @Summary <手机端>获取验证码
// @Description <手机端>获取验证码
// @Tags <手机端>球员账户
// @Param username query string false "账号（手机号）"
// @Success 200 {string} string {"code": 200, "data": [...]}
// @Router /mobile/v1/org-player-user/get-msg [post]
// @Security Bearer
func (e OrgPlayerUser) GetMsg(c *gin.Context) {
	req := dto.GetMsgReq{}
	s := service.OrgPlayerUser{}
	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
	}

	err, userId := s.IsBeing(req.Username)
	if err != nil {
		e.Logger.Error(err)
		e.Error(101, err, "账号未找到")
		return
	}
	if userId == 0 {
		e.Error(101, err, "账号未找到")
		return
	}

	//生成验证码
	code := utils.CreateCaptcha()

	reqMsg := dto.OrgMsgInsertReq{}
	sMsg := service.OrgMsg{}
	errMsg := e.MakeContext(c).
		MakeOrm().
		MakeService(&sMsg.Service).
		Errors
	if errMsg != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	reqMsg.Username = req.Username
	reqMsg.Code = code
	reqMsg.MsgType = "1"
	err = sMsg.Insert(&reqMsg)

	smsText := "您的验证码为：" + code
	if result := utils.SendMobileMsg(req.Username, smsText); result.Status == 0 {
		reply := new(dto.GetMsgReply)
		e.OK(reply, "发送成功")
		return
	} else {
		e.Logger.Error(err)
		e.Error(500, err, "发送失败")
		return
	}

}

// GetPage 获取球员账户信息列表
// @Summary 获取球员账户信息列表
// @Description 获取球员账户信息列表
// @Tags <手机端>球员账户
// @Param username query string false "账号（手机号）"
// @Param code query string false "验证码"
// @Success 200 {string} string {"code": 200, "data": [...]}
// @Router /mobile/v1/org-player-user/login [post]
// @Security Bearer
func (e OrgPlayerUser) Login(c *gin.Context) {
	req := dto.LoginReq{}
	s := service.OrgPlayerUser{}
	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)
	data := new(models.OrgPlayerUser)
	req.Password = dto.Pwd(req.Password)

	err = s.Login(&req, p, data)
	if err != nil {
		e.Error(101, err, "账号未找到")
		return
	}

	reqMsg := dto.OrgMsgGetReq{}
	sMsg := service.OrgMsg{}
	err = e.MakeContext(c).
		MakeOrm().
		MakeService(&sMsg.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	reqMsg.Username = req.Username
	reqMsg.Code = req.Code
	reqMsg.MsgType = "1"
	err = sMsg.Get(&reqMsg)
	if err != nil {
		e.Logger.Error(err)
		e.Error(102, err, "验证码不正确")
		return
	}

	sMsg.Remove(req.Username)

	//自建token生成
	reply := new(dto.LoginReply)
	token, _ := utils.GenToken(int64(data.Id))
	reply.Token = token
	reply.PlayerUserId = strconv.Itoa(data.Id)

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