package hotime

import (
	"encoding/json"
	"errors"
	"net/http"
)

type Context struct {
	contextBase
	Resp         http.ResponseWriter
	Req          *http.Request
	Application  *Application
	RouterString []string
	Config       Map
	Db           *HoTimeDB
	RespData     Map
	CacheIns
	SessionIns
	HandlerStr string //复写请求url
}

//唯一标志
func (this *Context) Mtd(router [3]string) Map {
	this.Application.Router[router[0]][router[1]][router[2]](this)
	d := this.RespData
	this.RespData = nil
	return d
}

//打印
func (this *Context) Display(statu int, data interface{}) {

	resp := Map{"statu": statu}
	if statu != 0 {
		temp := Map{}

		tpe := this.Config.GetMap("error").GetString(ObjToStr(statu))
		if tpe == "" {
			logFmt(errors.New("找不到对应的错误码"), 2, LOG_WARN)
		}

		temp["type"] = tpe
		temp["msg"] = data
		resp["result"] = temp
	} else {
		resp["result"] = data
	}

	this.RespData = resp

	//this.Data=d;
}

func (this *Context) View() {
	if this.RespData == nil {
		return
	}
	d, err := json.Marshal(this.RespData)
	if err != nil {
		return
	}
	this.RespData = nil
	this.Resp.Write(d)
}
