package request

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
)

// Response API return request
type Response struct {
	Method     string
	StatusCode int
	URL        *url.URL
	Body       []byte
}

// Request sends RESTful requests
func Request(method, url string, headers map[string]string, data io.Reader) (*Response, error) {
	req, err := http.NewRequest(method, url, data)
	if err != nil {
		return &Response{method, http.StatusInternalServerError, req.URL, nil}, err
	}
	for k, v := range headers {
		req.Header.Set(k, v)
	}
	client := &http.Client{Timeout: time.Second * 10}
	resp, err := client.Do(req)
	if err != nil {
		return &Response{method, http.StatusInternalServerError, req.URL, nil}, err
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return &Response{method, http.StatusInternalServerError, req.URL, nil}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode == 200 {
		return &Response{method, resp.StatusCode, resp.Request.URL, body}, nil
	}
	err = fmt.Errorf("request: %s - %d %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
	return &Response{method, resp.StatusCode, resp.Request.URL, nil}, err
}

// Get sends GET request
func Get(url string, headers map[string]string, data io.Reader) (*Response, error) {
	resp, err := Request(http.MethodGet, url, headers, data)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// Post sends POST request
func Post(url string, headers map[string]string, data io.Reader) (*Response, error) {
	resp, err := Request(http.MethodPost, url, headers, data)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// Put sends PUT request
func Put(url string, headers map[string]string, data io.Reader) (*Response, error) {
	resp, err := Request(http.MethodPut, url, headers, data)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// Delete sends DELETE request
func Delete(url string, headers map[string]string, data io.Reader) (*Response, error) {
	resp, err := Request(http.MethodDelete, url, headers, data)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// AsyncRequest send requests concurrently
func AsyncRequest(method, url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
	defer func() {
		chDone <- true
	}()
	resp, err := Request(method, url, headers, data)
	if err != nil {
		chErr <- err
	}
	ch <- resp
}

// AsyncGet sends GET requests concurrently
func AsyncGet(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
	AsyncRequest(http.MethodGet, url, headers, data, ch, chErr, chDone)
}

// AsyncPost sends POST requests concurrently
func AsyncPost(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
	AsyncRequest(http.MethodPost, url, headers, data, ch, chErr, chDone)
}

// AsyncPut sends PUT requests concurrently
func AsyncPut(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
	AsyncRequest(http.MethodPut, url, headers, data, ch, chErr, chDone)
}

// AsyncDelete sends DELETE requests concurrently
func AsyncDelete(url string, headers map[string]string, data io.Reader, ch chan<- *Response, chErr chan<- error, chDone chan<- bool) {
	AsyncRequest(http.MethodDelete, url, headers, data, ch, chErr, chDone)
}