package response

import (
	"fmt"
	"log"
	"net/http"
)

var request *http.Request

//Method : retorna o metodo
func Method() string {
	return request.Method
}

//Post : retorna um parametro post
func Post(parameter string) string {
	var r = request

	if err := r.ParseForm(); err != nil {
		fmt.Printf("ParseForm() err: %v", err)
		return ""
	}
	return r.FormValue(parameter)
}

//Get : retorna um parametro get
func Get(parameter string) string {
	var r = request

	keys, ok := r.URL.Query()["key"]

	if !ok || len(keys[0]) < 1 {
		log.Println("Url Param 'key' is missing")
		return ""
	}

	key := keys[0]

	return string(key)
}

//Parameter : retorna um parametro post ou get
func Parameter(parameter string) string {
	var post = Post(parameter)
	if post != "" {
		return post
	}

	var get = Get(parameter)
	if get != "" {
		return get
	}

	return ""
}

func SetRequest(r *http.Request) {
	request = r
}
