package controllers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"oppmg/common/log"
	"oppmg/protocol"
	"strconv"

	"github.com/astaxie/beego"
)

//BaseController 基础
type BaseController struct {
	beego.Controller
	// AppHead protocol.BaseHeader
}

//Options 实现beego.ControllerInterface 的接口
func (this *BaseController) Options() {

	this.Ctx.Abort(http.StatusBadRequest, "")

}

//Prepare 实现beego.ControllerInterface 的接口
func (this *BaseController) Prepare() {

}

func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) {
	appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken)
	appHead.RefreshToken = this.Ctx.Input.Header(protocol.HeaderRefreshToken)
	appHead.UID, _ = strconv.Atoi(this.Ctx.Input.Header(protocol.HeaderUID))
	appHead.UUID = this.Ctx.Input.Header(protocol.HeaderUUID)
	appHead.Timestamp, _ = strconv.Atoi(this.Ctx.Input.Header(protocol.HeaderTimestamp))
	appHead.Devicetype = this.Ctx.Input.Header(protocol.HeaderDevicetype)
	appHead.AppProject = this.Ctx.Input.Header(protocol.HeaderAppproject)
	return

}

//Finish 实现beego.ControllerInterface 的接口
func (this *BaseController) Finish() {
	strByte, _ := json.Marshal(this.Data["json"])
	length := len(strByte)
	if length > 5000 {
		log.Info(fmt.Sprintf("<====Send to client:  RspBodyData: %s......", string(strByte[:5000])))
	} else {
		log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s", string(strByte)))
	}
}

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