package ocr

import (
	"encoding/base64"
	"encoding/json"
	"io"
	"net/http"
	"net/url"
	"os"
	"strings"
	"time"

	"git.listensoft.net/tool/jspkit/logger"
)

//https://www.chaojiying.com/api-41.html

// application/x-www-form-urlencoded    用这个时要注意urlencode
// POST /Upload/Processing.php HTTP/1.1
// Cache-Control: no-cache
// Connection: Keep-Alive
// Content-Type: application/x-www-form-urlencoded
// Accept: */*
// User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
// Content-Length: 2915
// Host: upload.chaojiying.net
//
// user=userabc&pass=passpass&softid=96001&codetype=1902&file_base64=base64字符串
type Result struct {
	ErrNo  int    `json:"err_no"`
	ErrStr string `json:"err_str"`
	PicID  string `json:"pic_id"`
	PicStr string `json:"pic_str"`
	Md5    string `json:"md5"`
}

const (
	user     string = "canyang"
	pass     string = "Qzf@0911"
	softid   string = "898872"
	codetype string = "1902"
)

// 超级鹰
func GetCaptchaCode(imgPath string) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=" + codetype + "&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	return rs.PicStr
}

// 超级鹰 计算题
func GetCaptchaCodeJST(imgPath string) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6001&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	return rs.PicStr
}

// 超级鹰 坐标点选择
func GetCaptchaCode9103(imgPath string) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 40)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9103&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	logger.Info("图片地址：", imgPath, "返回值：", rs)
	return rs.PicStr
}

// 超级鹰 坐标点选择
func GetCaptchaCode9004(imgPath string, comID uint, len int) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 60)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9004&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	logger.Info(rs.PicStr + " 坐标接口")
	return rs.PicStr
}

// 超级鹰 坐标点选择
func GetCaptchaCode9101(imgPath string) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	logger.Info(rs.PicStr + " 坐标接口")
	return rs.PicStr
}

// 超级鹰 坐标点选择
func GetCaptchaCode9101Err(imgPath string) (string, string) {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return "", ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return "", ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return "", ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return "", ""
	}
	if rs.ErrNo != 0 {
		return "", ""
	}
	logger.Info(rs.PicStr + " 坐标接口")
	return rs.PicID, rs.PicStr
}

// 超级鹰 问答题
func GetCaptchaCodeWDT(imgPath string, comID uint, len int) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	bstr, err := base64Img(imgPath)
	if err != nil {
		return ""
	}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	return rs.PicStr
}

func GetCaptchaCodeWDTBase64(bstr string) string {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
	//bstr, err := base64Img(imgPath)
	//if err != nil {
	//	return ""
	//}
	params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	var rs Result
	err = json.Unmarshal(body, &rs)
	if err != nil {
		return ""
	}
	if rs.ErrNo != 0 {
		return ""
	}
	return rs.PicStr
}

func ReportError(imgid string) error {
	var client = http.Client{}
	client.Timeout = time.Duration(time.Second * 15)
	tyURL := "http://upload.chaojiying.net/Upload/ReportError.php"
	params := "user=" + user + "&pass=" + pass + "&id=" + imgid + "&softid=" + softid
	req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
	if err != nil {
		return err
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	logger.Info(string(body))
	return nil
}

func base64Img(path string) (string, error) {
	bs, err := os.ReadFile(path)
	if err != nil {
		return "", err
	}
	s := base64.StdEncoding.EncodeToString(bs)
	return s, nil
}
