package response

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

//Method : retorna o metodo
func Method(r *http.Request) string {
	return r.Method
}

//Post : retorna um parametro post
func Post(r *http.Request, parameter string) string {
	if err := r.ParseForm(); err != nil {
		fmt.Printf("ParseForm() err: %v", err)
		return ""
	}
	return r.FormValue(parameter)
}

//Get : retorna um parametro get
func Get(r *http.Request, parameter string) string {
	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(r *http.Request, parameter string) string {
	var post = Post(r, parameter)
	if post != "" {
		return post
	}

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

	return ""
}
