package utils

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func HttpGet(url string, params map[string]string) (result []byte, err error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	q := req.URL.Query()
	for k, v := range params {
		q.Add(k, v)
	}
	req.URL.RawQuery = q.Encode()
	fmt.Println(req.URL.String())
	var resp *http.Response

	resp, err = http.DefaultClient.Do(req)
	if err != nil || resp == nil {
		return
	}
	output, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = output
	defer func() {
		if resp != nil {
			resp.Body.Close()
		}
	}()
	return
}

func HttpPost(url string, params interface{}) (result []byte, err error) {
	data, _ := json.Marshal(params)
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
	if err != nil {
		return
	}
	fmt.Println(url)
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = body
	return
}

func HttpPostReplyCode(url string, params interface{}) (result []byte, code int, err error) {
	data, _ := json.Marshal(params)
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
	if err != nil {
		return
	}
	fmt.Println(url)
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	code = resp.StatusCode
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = body
	return
}

func HttpGetReplyCode(url string, params map[string]string) (result []byte, code int, err error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	q := req.URL.Query()
	for k, v := range params {
		q.Add(k, v)
	}
	req.URL.RawQuery = q.Encode()
	fmt.Println(req.URL.String())
	var resp *http.Response

	resp, err = http.DefaultClient.Do(req)
	if err != nil || resp == nil {
		return
	}
	code = resp.StatusCode
	output, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = output
	defer func() {
		if resp != nil {
			resp.Body.Close()
		}
	}()
	return
}

func HttpPutReplyCode(url string, params interface{}) (result []byte, code int, err error) {
	data, _ := json.Marshal(params)
	client := &http.Client{}
	req, err := http.NewRequest("PUT", url, strings.NewReader(string(data)))
	if err != nil {
		return
	}
	fmt.Println(url)
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	code = resp.StatusCode
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = body
	return
}

func HttpDeleteReplyCode(url string, params interface{}) (result []byte, code int, err error) {
	data, _ := json.Marshal(params)
	client := &http.Client{}
	req, err := http.NewRequest("DELETE", url, strings.NewReader(string(data)))
	if err != nil {
		return
	}
	fmt.Println(url)
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(req)
	if err != nil {
		return
	}
	code = resp.StatusCode
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	result = body
	return
}
