package controllers

import (
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego/plugins/cors"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
	"openapi/protocol"
	"strconv"
	"strings"

	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/validation"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
)

type BaseController struct {
	Header *protocol.RequestHeader
	mybeego.BaseController
}

func init() {

}

var DefaultController *BaseController = &BaseController{}

//Valid  valid struct
func (this *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 {
		for _, err := range valid.Errors {
			if strings.HasSuffix(err.Key, ".Mobile") {
				msg = protocol.BadRequestParam(2001)
				return
			}
			log.Error(err.Key, err.Message)
		}
		msg = protocol.BadRequestParam(2)
		return
	}

	return
}

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

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

//获取请求头信息
func 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")
	return h
}

//过滤器
func FilterComm(ctx *context.Context) {
	if !CheckSign(ctx) {
		return
	}
	return
}

//检查签名
func CheckSign(ctx *context.Context) (result bool) {
	var (
		h       *protocol.RequestHeader
		sign    string
		signHex string
	)
	result = true
	h = GetRequestHeader(ctx)
	//1.检查签名
	sign = fmt.Sprintf("v!(MmM%v%v%vMmM)i^", h.TimeStamp, h.Uuid, h.AccessToken)
	sha256 := sha256.New()
	sha256.Write([]byte(sign))
	signHex = hex.EncodeToString(sha256.Sum(nil))
	if strings.Compare(signHex, h.Sign) != 0 {
		msg := protocol.BadRequestParam(113)
		log.Error(fmt.Sprintf("%v req:%v resp:%v %v", ctx.Request.RequestURI, common.AssertJson(h), common.AssertJson(msg), signHex))
		ctx.Output.JSON(msg, false, false)
		result = false
		return
	}
	return
}

//AllowOption 允许跨域请求
var AllowOption = func(ctx *context.Context) {
	if ctx.Request.Method != "OPTIONS" {
		return
	}
	f := cors.Allow(&cors.Options{
		AllowMethods: []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}, //允许的请求类型
		AllowHeaders: []string{"Origin", "Accept", "Content-Type", "Authorization",
			"x-mmm-cid", "x-mmm-uid", "x-mmm-accesstoken", "x-mmm-refreshtoken", "x-requested-with", "x-mmm-appproject", "x-mmm-devicetype"}, //允许的头部信息
		ExposeHeaders:    []string{"Content-Length"}, //允许暴露的头信息
		AllowCredentials: false,                      //不允许共享AuthTuffic证书
		AllowAllOrigins:  true,                       //允许的请求来源
	})
	f(ctx)
	ctx.Output.Body([]byte("{}"))
	ctx.Output.SetStatus(204)
	return
}

//LogRequestData Before Router
var LogRequestData = func(ctx *context.Context) {
	log.Info("====>Recv Request:%s", ctx.Input.URI())
	hmap := map[string]string{
		//protocol.HeaderAccessToken:  ctx.Input.Header(protocol.HeaderAccessToken),
		//protocol.HeaderRefreshToken: ctx.Input.Header(protocol.HeaderRefreshToken),
	}
	if ctx.Input.RequestBody != nil {
		log.Info("====>Recv data from client:\nHeadData: %v \nBodyData: %s", hmap, string(ctx.Input.RequestBody))
	} else {
		log.Info("====>Recv data from client:\nHeadData: %v ", hmap)
	}
}

func (this *BaseController) Prepare() {
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
	if this.Ctx.Input.Method() == "OPTIONS" {
		this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
		return
	}
	this.Query = map[string]string{}
	input := this.Input()
	for k := range input {
		this.Query[k] = input.Get(k)
	}
	if this.Ctx.Input.RequestBody != nil {
		this.ByteBody = this.Ctx.Input.RequestBody[:]
		if len(this.ByteBody) < 1 {
			this.ByteBody = []byte("{}")
		}
		this.RequestHead = this.GetRequestHead()
		this.Header = GetRequestHeader(this.Ctx)
		this.Header.SetRequestId(fmt.Sprintf("%v.%v.%s", this.Header.Uid, time.GetTimeByYyyymmddhhmmss(), this.Ctx.Request.URL))
		log.Debug(fmt.Sprintf("====>Recv data from uid(%d) ucid(%v) client:\nHeadData: %s\nRequestId:%s BodyData: %s", this.Header.Uid, this.Header.UserId, common.AssertJson(this.Header), this.Header.GetRequestId(), string(this.ByteBody)))
	}
}

func (this *BaseController) Finish() {
	if this.Ctx.Input.Method() == "OPTIONS" {
		return
	}
	strByte, _ := json.Marshal(this.Data["json"])
	length := len(strByte)
	if length > 5000 {
		log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s......", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte[:5000])))
	} else {
		log.Debug(fmt.Sprintf("<====Send to uid(%d) ucid(%v) client: %d byte\nRequestId:%s RspBodyData: %s", this.Header.Uid, this.Header.UserId, length, this.Header.GetRequestId(), string(strByte)))
	}
}