package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/server/web"
	"hongze/hongze_open_api/services"
	"hongze/hongze_open_api/utils"
	"net/http"
	"net/url"
)

//不需要授权的基类
type BaseCommon struct {
	web.Controller
	Response
}

type Response struct {
	Code   int         `json:"code"`
	Data   interface{} `json:"data"`
	Msg    string      `json:"msg"`
	ErrMsg string      `json:"err_msg"`
}

const (
	SUCCESS    = 200 //成功
	ERROR      = 400 //代表业务处理失败,前端同学需要做额外逻辑处理
	SIGN_ERROR = 401 //签名异常
)

//返回数据
func (c BaseCommon) Result() {
	var content []byte
	var err error
	content, err = json.Marshal(c.Response)
	ip := c.Ctx.Input.IP()
	requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
	if err != nil {
		fmt.Println("base auth url.QueryUnescape Err:", err.Error())
		requestBody = string(c.Ctx.Input.RequestBody)
	}
	utils.ApiLog.Println("请求地址:", c.Ctx.Input.URI(), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)

	//不将errMsg暴露给用户
	c.Response.ErrMsg = c.Response.Msg
	//c.Controller.Data["json"] = c.Response
	//c.Controller.ServeJSON()

	// 将处理后的数据返回给前端
	content, err = json.Marshal(c.Response)
	if err != nil {
		http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
		return
	}
	//content = []byte(utils.StringsToJSON(string(content)))
	// 数据加密
	if services.CheckEncryption(c.Ctx.Request.URL.Path) {
		content = utils.DesBase64EncryptV2(content, utils.REPORT_KEY)
	}
	c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
	c.Ctx.Output.Body(content)

	c.StopRun()
}

//没有任何信息的返回
func (c BaseCommon) Ok() {
	c.Response.Code = SUCCESS
	c.Response.Msg = "操作成功"
	c.Response.Data = map[string]interface{}{}
	c.Result()
}

func (c BaseCommon) OkWithMessage(message string) {
	c.Response.Code = SUCCESS
	c.Response.Msg = message
	c.Response.Data = map[string]interface{}{}
	c.Result()
}

func (c BaseCommon) OkWithData(data interface{}) {
	c.Response.Code = SUCCESS
	c.Response.Msg = "操作成功"
	c.Response.Data = data
	c.Result()
}

func (c BaseCommon) OkDetailed(data interface{}, message string) {
	c.Response.Code = SUCCESS
	c.Response.Msg = message
	c.Response.Data = data
	c.Result()
}

func (c BaseCommon) Fail() {
	c.Response.Code = ERROR
	c.Response.Msg = "操作失败"
	c.Response.Data = map[string]interface{}{}
	c.Result()
}

func (c BaseCommon) FailWithMessage(message string) {
	c.Response.Code = ERROR
	c.Response.Msg = message
	c.Response.Data = map[string]interface{}{}
	c.Result()
}

func (c BaseCommon) FailWithMessageErr(message, errMsg string) {
	c.Response.Code = ERROR
	c.Response.Msg = message
	c.Response.ErrMsg = errMsg
	c.Response.Data = map[string]interface{}{}
	c.Result()
}

func (c BaseCommon) FailWithDetailed(code int, data interface{}, message string) {
	c.Response.Code = code
	c.Response.Msg = message
	c.Response.Data = data
	c.Result()
}

// SignError 签名异常
func (c BaseCommon) SignError(message string) {
	c.Response.Code = SIGN_ERROR
	c.Response.Msg = message
	c.Response.Data = map[string]interface{}{}
	c.Result()
}