package auth2

import (
	"net/http"
	"strings"

	"git.rosy.net.cn/baseapi/utils"
)

type Context struct {
	accessUUID string
	w          http.ResponseWriter
	r          *http.Request
	// mapData    map[interface{}]interface{}
}

func NewContext(w http.ResponseWriter, r *http.Request) (ctx *Context) {
	ctx = &Context{
		w:          w,
		r:          r,
		accessUUID: utils.GetUUID(),
	}
	return ctx
}

func (ctx *Context) GetResponseWriter() http.ResponseWriter {
	return ctx.w
}

func (ctx *Context) GetRequest() *http.Request {
	return ctx.r
}

func (ctx *Context) GetTrackInfo() string {
	return ctx.accessUUID
}

func (ctx *Context) GetRealRemoteIP() (ip string) {
	r := ctx.r
	if r != nil {
		ip = r.Header.Get("X-Forwarded-For")
		if ip == "" {
			ip = r.Header.Get("X-real-ip")
		}
		if ip == "" {
			ip = strings.Split(r.RemoteAddr, ":")[0]
		} else {
			ip = strings.Split(ip, ",")[0]
		}
	}
	return ip
}
