package response

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

var responseCode = 200

var responseWriter *http.ResponseWriter

//JSON :response json
func JSON(data interface{}) {
	w := *responseWriter
	js, err := json.Marshal(data)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(responseCode)
	w.Write(js)
}

//SetCode :informa o código de resposta
func SetCode(code int) {
	responseCode = code
}

func SetResponseWriter(w http.ResponseWriter) {
	responseWriter = &w
}
