//Package infra code generated by 'freedom new-project base'
package infra

import (
	"encoding/json"
	"strconv"

	"github.com/8treenet/freedom"
)

// JSONResponse .
type JSONResponse struct {
	Code             int
	Error            error
	Object           interface{}
	DisableLogOutput bool
}

// Dispatch .
func (jrep JSONResponse) Dispatch(ctx freedom.Context) {
	var content []byte

	var body struct {
		Code  int         `json:"code"`
		Error string      `json:"error"`
		Data  interface{} `json:"data,omitempty"`
	}
	body.Data = jrep.Object
	body.Code = jrep.Code

	if jrep.Error != nil {
		body.Error = jrep.Error.Error()
	}
	if jrep.Error != nil && body.Code == 0 {
		body.Code = 400
	}

	if content, jrep.Error = json.Marshal(body); jrep.Error != nil {
		content = []byte(jrep.Error.Error())
	}

	ctx.Values().Set("code", strconv.Itoa(body.Code))
	if !jrep.DisableLogOutput {
		ctx.Values().Set("response", string(content))
	}

	ctx.ContentType("application/json")
	ctx.StatusCode(200)
	if _, err := ctx.Write(content); err != nil {
		freedom.Logger().Error("JSONResponse dispatch error:%v", err)
	}
}
