package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	log "github.com/sirupsen/logrus"
	"youngee_b_api/model/http_model"
	"youngee_b_api/service"
)

func WrapCodeLoginHandler(ctx *gin.Context) {
	handler := newCodeLoginHandler(ctx)
	baseRun(handler)
}

func newCodeLoginHandler(ctx *gin.Context) *CodeLoginHandler {
	return &CodeLoginHandler{
		req:  http_model.NewCodeLoginRequest(),
		resp: http_model.NewCodeLoginResponse(),
		ctx:  ctx,
	}
}

type CodeLoginHandler struct {
	req  *http_model.CodeLoginRequest
	resp *http_model.CommonResponse
	ctx  *gin.Context
}

func (h *CodeLoginHandler) getRequest() interface{} {
	return h.req
}

func (h *CodeLoginHandler) getContext() *gin.Context {
	return h.ctx
}

func (h *CodeLoginHandler) getResponse() interface{} {
	return h.resp
}

func (h *CodeLoginHandler) run() {
	msg, userData, err := service.LoginAuth.AuthCode(h.ctx, h.req.Phone, h.req.Code)
	if err != nil {
		logrus.Errorf("[CodeLoginHandler] call AuthCode err:%+v\n", err)
		log.Info("login fail,req:%+v", h.req)
		h.resp.Message = "err"
		h.resp.Data = nil
		h.resp.Status = 40000
		return
	}
	if msg == 1 {
		h.resp.Message = "验证码错误"
		h.resp.Status = 34000
		h.resp.Data = nil
		return
	}
	var data *http_model.CodeLoginData
	data = userData
	h.resp.Message = "ok"
	h.resp.Data = data
	h.resp.Status = 20000
	return
}

func (h *CodeLoginHandler) checkParam() error {
	return nil
}
