package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/validation"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/web/beego/utils"
	comm "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr/protocol"
	"strconv"
)

type BaseController struct {
	beego.Controller
}

func (controller BaseController) JsonUnmarshal(v interface{}) error {
	body := controller.Ctx.Input.GetData("requestBody").([]byte)
	//fmt.Println("【RequestBody】 ",controller.Ctx.Input.Method(),controller.Ctx.Input.URL(),string(body))
	return json.Unmarshal(body, v)
}

func (controller BaseController) HandlerResponse(data interface{}, err error) {
	var response utils.JsonResponse
	defer func() {
		controller.Data["json"] = response
		controller.ServeJSON()
	}()
	if err == nil {
		response = utils.ResponseData(controller.Ctx, data)
		return
	}
	if _, ok := err.(*application.ServiceError); !ok {
		err = application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if x, ok := err.(protocol.CustomErrParse); ok {
		m := x.ParseToMessage()
		response = ResponseCustomData(controller.Ctx, data, m)
		return
	}
	response = utils.ResponseError(controller.Ctx, err)
}

func (controller BaseController) GetLimitInfo() (offset int, limit int) {
	offset, _ = controller.GetInt("offset")
	limit, _ = controller.GetInt("limit")
	return
}

func ResponseCustomData(ctx *context.Context, data interface{}, msg *protocol.ResponseMessage) utils.JsonResponse {
	jsonResponse := utils.JsonResponse{}
	jsonResponse["code"] = 0
	jsonResponse["msg"] = "ok"
	jsonResponse["data"] = data
	ctx.Input.SetData("outputData", jsonResponse)
	return jsonResponse
}

//Valid  valid struct
func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
	/*校验*/
	var err error
	valid := validation.Validation{}
	result, err = valid.Valid(obj)
	if err != nil {
	}
	if !result {
		msg = protocol.BadRequestParam(2)
		return
	}

	return
}

func (this *BaseController) Resp(msg *protocol.ResponseMessage) {
	this.Data["json"] = msg
	this.Ctx.Input.SetData("outputData", msg)
	this.ServeJSON()
}

func (this *BaseController) RespH5(msg *protocol.ResponseMessage) {
	if msg.Errno != 0 {
		msg.Errno = -1
	}
	this.Data["json"] = msg
	this.Ctx.Input.SetData("outputData", msg)
	this.ServeJSON()
}

//获取请求头信息
func (this *BaseController) GetRequestHeader(ctx *context.Context) *protocol.RequestHeader {
	h := &protocol.RequestHeader{}
	h.AccessToken = ctx.Input.Header("x-mmm-accesstoken")
	h.AppProject = ctx.Input.Header("x-mmm-appproject")
	h.DeviceType, _ = strconv.Atoi(ctx.Input.Header("x-mmm-devicetype"))
	h.Sign = ctx.Input.Header("x-mmm-sign")
	h.Uuid = ctx.Input.Header("x-mmm-uuid")
	h.TimeStamp = ctx.Input.Header("x-mmm-timestamp")
	h.Version = ctx.Input.Header("x-mmm-version")
	h.UserId, _ = strconv.ParseInt(ctx.Input.Header("x-mmm-id"), 10, 64)

	if len(h.AccessToken) > 0 {
		if claim, err := comm.ParseJWTToken(h.AccessToken); err == nil {
			h.UserId = claim.UserId
		}
	}
	return h
}