package controllers

import (
	"encoding/json"
	"oppmg/common/log"
	"oppmg/protocol"
	serveauth "oppmg/services/auth"
	"oppmg/storage/redisdata"
)

type AuthController struct {
	BaseController
}

//URLMapping 实现ControllerInterface中的URLMapping
func (c *AuthController) URLMapping() {
	c.Mapping("RefreshToken", c.RefreshToken)
}

// RefreshToken ....
// @router /refresh_token [get]
func (c *AuthController) RefreshToken() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	companyid := c.GetCompanyId()
	userid := c.GetUserId()
	logintoken, err := serveauth.ChangeLoginToken(userid, companyid)
	if err != nil {
		msg = protocol.NewReturnResponse(nil, err)
		return
	}
	err = serveauth.ResetLoginTokenRedis(logintoken)
	msg = protocol.NewReturnResponse(logintoken, err)
	return
}

// Login 登录
// @router /login [post]
func (c *AuthController) Login() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()

	var param protocol.RequestLogin
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	if len(param.Account) == 0 || len(param.Password) == 0 {
		msg = protocol.BadRequestParam("10021")
		return
	}
	logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
	if err != nil {
		msg = protocol.NewReturnResponse(nil, err)
		return
	}
	err = serveauth.ResetLoginToken(logintoken)
	if err != nil {
		log.Error("token 信息记录数据库失败")
	}
	err = serveauth.ResetLoginTokenRedis(logintoken)
	if err != nil {
		log.Error("token 信息记录redis失败")
	}
	data := map[string]interface{}{
		"access": logintoken,
	}
	msg = protocol.NewReturnResponse(data, nil)
	return
}

//ChangeCompany 切换公司
//@Router /change_company [post]
func (c *AuthController) ChangeCompany() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()

	var param protocol.RequestSwapCompany
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	userid := c.GetUserId()
	if param.CompanyId <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	logintoken, err := serveauth.ChangeLoginToken(userid, param.CompanyId)
	if err != nil {
		msg = protocol.NewReturnResponse(nil, err)
		return
	}
	err = redisdata.SetLoginToken(logintoken, userid, param.CompanyId)
	if err != nil {
		log.Error("redisdata.SetLoginToken err:%s", err)
	}
	msg = protocol.NewReturnResponse(logintoken, err)
	return
}

//Me
//@router /me [get]
func (c *AuthController) Me() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	userid := c.GetUserId()
	companyid := c.GetCompanyId()
	userinfo, err := serveauth.UserBaseInfo(userid, companyid)
	if err != nil {
		log.Error("%s", err)
	}
	menus, err := serveauth.GetUserHasMenu(userid, companyid)
	if err != nil {
		log.Error("%s", err)
	}
	companys, err := serveauth.UserHasCompanys(userid)
	if err != nil {
		log.Error("%s", err)
	}
	data := map[string]interface{}{
		"user":     userinfo,
		"menus":    menus,
		"companys": companys,
	}
	msg = protocol.NewReturnResponse(data, nil)
	return
}