package controllers

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"time"
)

type AuthController struct {
	BaseController
}

// Login
// @router /login [post]
func (this *AuthController) Login() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.LoginRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if request.ClientId != clientId {
		msg = protocol.NewResponseMessage(101, "clientId无效")
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	data, err := auth.Login(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewReturnResponse(data, err)
}

//SmsCode
// @router /smsCode [post]
func (this *AuthController) SmsCode() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.SmsCodeRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.NewMesage(2)
		return
	}
	if b, _ := this.Valid(request); !b {
		msg = protocol.NewResponseMessage(2, "手机号码有误,请重新输入!")
		return
	}
	msg = protocol.NewReturnResponse(auth.SmsCode(request))
}

//AccessToken
// @router /accessToken [post]
func (this *AuthController) AccessToken() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.AccessTokenRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	msg = protocol.NewReturnResponse(auth.AccessToken(request))
}

//RefreshToken
// @router /refreshToken [post]
func (this *AuthController) RefreshToken() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.RefreshTokenRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	msg = protocol.NewReturnResponse(auth.RefreshToken(request))
}

//Revoke
//@router /revoke [post]
func (this *AuthController) Revoke() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	//var request *protocol.RevokeRequest
	//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(auth.Revoke(header, request))
}

//UpdateDevice
//@router /updateDevice [post]
func (this *AuthController) UpdateDevice() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	//var request *protocol.UpdateDeviceRequest
	//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(auth.UpdateDevice(header, request))
}

//AuthCheckSmsCode  验证手机号码(修改手机号码、重置密码的前一步)
//@router /checkSmsCod [post]
func (this *AuthController) AuthCheckSmsCode() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.Resp(msg)
	}()
	var request *protocol.AuthCheckSmsCodeRequest
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.BadRequestParam(1)
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	rsp, err := auth.AuthCheckSmsCode(header, request)
	if err != nil {
		msg = protocol.NewReturnResponse(rsp, err)
		return
	}
	err = CacheSms.Put(request.Phone, rsp.CaptchaCertificate, 5*time.Minute)
	msg = protocol.NewReturnResponse(rsp, err)
}