package httpclient

import (
	"bytes"
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"

	"go.uber.org/zap"

	log "github.com/go-admin-team/go-admin-core/logger"
	"github.com/valyala/fasthttp"
)

var (
	client     = &fasthttp.Client{MaxConnsPerHost: 1000000, TLSConfig: &tls.Config{InsecureSkipVerify: true}, ReadTimeout: time.Second * 60, MaxIdemponentCallAttempts: 0}
	clientForm = &fasthttp.Client{MaxConnsPerHost: 1000000, TLSConfig: &tls.Config{InsecureSkipVerify: true}, ReadTimeout: time.Second * 60, MaxIdemponentCallAttempts: 0}
)

// JumioPostBasicAuth
func JumioPostBasicAuth(uri, username, password string, data url.Values) ([]byte, error) {
	client := &http.Client{
		Timeout: time.Second * 10,
	}

	req, err := http.NewRequest("POST", uri, strings.NewReader(data.Encode()))
	if err != nil {
		return nil, err
	}

	req.SetBasicAuth(username, password)
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	response, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	body, _ := ioutil.ReadAll(response.Body)
	return body, nil
}

// JumioPostOauth
func JumioPostOauth(uri, token string, data []byte) ([]byte, error) {
	client := &http.Client{
		Timeout: time.Second * 10,
	}
	req, err := http.NewRequest("POST", uri, bytes.NewReader(data))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
	req.Header.Set("User-Agent", "myapp-v1.0.0")
	req.Header.Set("Accept", "*/*")
	req.Header.Set("Content-Type", "application/json")

	response, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	body, _ := ioutil.ReadAll(response.Body)
	return body, nil

}

// JumioHttp
func JumioHttp(method, uri, accessToken string, data url.Values) ([]byte, error) {
	client := &http.Client{
		Timeout: time.Second * 10,
	}
	req, err := http.NewRequest(method, uri, strings.NewReader(data.Encode()))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", accessToken))

	response, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	body, _ := ioutil.ReadAll(response.Body)
	xx := string(body)
	fmt.Println(xx)
	return body, nil
}

// FastPostByte  do application/json POST request via fasthttp
func FastPostByte(url string, body []byte) ([]byte, error) {
	req := fasthttp.AcquireRequest()
	resp := fasthttp.AcquireResponse()
	defer func() {
		fasthttp.ReleaseResponse(resp)
		fasthttp.ReleaseRequest(req)
	}()
	req.SetRequestURI(url)
	//req.Header.SetContentType("application/x-www-form-urlencoded")
	req.Header.SetContentType("application/json; charset=utf-8")
	req.Header.Add("Accept", "application/json")
	// if w.Authentication && len(w.JwtToken) > 0 {
	// 	req.Header.Set("Authorization", "Bearer "+w.JwtToken)
	// }

	req.Header.SetMethod("POST")
	req.SetBody(body)

	// if !loghelper.LevelIsError() {
	//	loghelper.InfoLog("FastPostByte:", string(body))
	// }
	err := client.Do(req, resp)
	if err != nil {
		return nil, err
	}
	recbody := resp.Body()
	arr := make([]byte, len(recbody))
	copy(arr, recbody)
	return arr, nil
}

// FastGet Fast Get
//
//	@params header 格式 []string{
//		 "key:value",
//		 "key:value",
//	}
//
// @return "中国，深圳"
func FastGet(url string, header ...string) ([]byte, error) {
	req := fasthttp.AcquireRequest()
	resp := fasthttp.AcquireResponse()
	defer func() {
		fasthttp.ReleaseResponse(resp)
		fasthttp.ReleaseRequest(req)
	}()
	req.SetRequestURI(url)

	// if w.Authentication && len(w.JwtToken) > 0 {
	// 	req.Header.Set("Authorization", "Bearer "+w.JwtToken)
	// }

	for _, h := range header {
		kv := strings.Split(h, ":")
		req.Header.Set(kv[0], kv[1])
	}

	// define webapi client request Method
	req.Header.SetMethod("GET")
	// DO GET request
	err := client.Do(req, resp)
	if err != nil {
		return nil, err
	}
	body := resp.Body()
	// arr := make([]byte, len(body))
	// copy(arr, body)
	return body, nil
}

// fastFormPost  do  POST request via fasthttp
func fastFormPost(url string, parmlist map[string]string) (*fasthttp.Response, error) {
	req := fasthttp.AcquireRequest()
	resp := fasthttp.AcquireResponse()
	defer func() {
		fasthttp.ReleaseResponse(resp)
		fasthttp.ReleaseRequest(req)
	}()
	req.SetRequestURI(url)
	// if w.Authentication && len(w.JwtToken) > 0 {
	// 	req.Header.Set("Authorization", "Bearer "+w.JwtToken)
	// }
	req.Header.SetMethod("POST")
	args := req.PostArgs()
	for k, v := range parmlist {
		args.Set(k, v)
	}
	err := clientForm.Do(req, resp) // fasthttp.DoTimeout(req, resp, timeOut)
	if err != nil {
		log.Error("post request error", zap.Error(err))
		return nil, err
	}
	out := fasthttp.AcquireResponse()
	resp.CopyTo(out)
	return out, nil
}

// PostForm do  POST request via fasthttp
func PostForm(url string, parmlist map[string]string) ([]byte, error) {
	resp, err := fastFormPost(url, parmlist)
	if err != nil {
		return nil, err
	}
	arr := make([]byte, len(resp.Body()))
	copy(arr, resp.Body())
	// defer fasthttp.ReleaseResponse(resp)
	// return resp.Body()
	return arr, nil
}

// ClientIP 获取真实的IP  1.1.1.1, 2.2.2.2, 3.3.3.3
func ClientIP(ctx *fasthttp.RequestCtx) string {
	clientIP := string(ctx.Request.Header.Peek("X-Forwarded-For"))
	if index := strings.IndexByte(clientIP, ','); index >= 0 {
		clientIP = clientIP[0:index]
		// 获取最开始的一个 即 1.1.1.1
	}
	clientIP = strings.TrimSpace(clientIP)
	if len(clientIP) > 0 {
		return clientIP
	}
	clientIP = strings.TrimSpace(string(ctx.Request.Header.Peek("X-Real-Ip")))
	if len(clientIP) > 0 {
		return clientIP
	}
	return ctx.RemoteIP().String()
}

// MergeQuery appends additional query values to an existing URL.
func MergeQuery(u url.URL, q url.Values) url.URL {
	uv := u.Query()
	for k, vs := range q {
		for _, v := range vs {
			uv.Add(k, v)
		}
	}
	u.RawQuery = uv.Encode()
	return u
}
