package common

import (
	"bytes"
	"encoding/json"
	"git.listensoft.net/tool/jspkit/common/lxhttp"
	"git.listensoft.net/tool/jspkit/logger"
	"io"
	"net/http"
	"strings"
	"time"

	"github.com/ysmood/leakless/pkg/utils"
)

func GetMobileCaptcha(mobile string) string {
	client := &http.Client{
		Timeout: 60 * time.Second,
	}
	defer client.CloseIdleConnections()
	resp, err := client.Post("https://public.listensoft.net/api/getTaxSms", "application/json", strings.NewReader(`{"mobile":"`+mobile+`"}`))
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	logger.Info("获取到的手机验证码： " + string(body))
	return string(body)
}

type GsnvYzm struct {
	ID   uint   `json:"id"`
	Code string `json:"code"`
}

// 获取工商年报验证码 code验证码 id正确后需要上传纳税人识别号
// 数组返回最多四个验证码 空或者都不对报错
// 验证码正确后调用上传纳税人识别号接口，两小时内返回都是第一个
func GetGsnbMobileCaptcha(mobile string, taxNo string) []GsnvYzm {
	utils.Sleep(60)
	client := &http.Client{
		Timeout: 60 * time.Second,
	}
	defer client.CloseIdleConnections()
	resp, err := client.Post("https://public.listensoft.net/api/getGsSms", "application/json", strings.NewReader(`{"mobile":"`+mobile+`","taxNo":"`+taxNo+`"}`))
	if err != nil {
		return []GsnvYzm{}
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return []GsnvYzm{}
	}
	list := []GsnvYzm{}
	json.Unmarshal(body, &list)
	return list
}

// 上传工商年报验证码对应的纳税人识别号 code验证码 id  正确后需要上传纳税人识别号
func UploadGsnbMobileCaptcha(id uint, taxNo string) {
	utils.Sleep(60)
	client := &http.Client{
		Timeout: 60 * time.Second,
	}
	defer client.CloseIdleConnections()
	param := map[string]interface{}{
		"id":    id,
		"taxNo": taxNo,
	}
	bys, _ := json.Marshal(param)
	client.Post("https://public.listensoft.net/api/uploadGsSms", "application/json", bytes.NewReader(bys))
	// _, err := io.ReadAll(resp.Body)
	// if err != nil {
	// 	return
	// }
}

// area--》传验证码中的地区关键字
func GetMobileCaptchaForArea(mobile, area string) string {
	resp, err := http.Post("https://public.listensoft.net/api/getTaxSms", "application/json", strings.NewReader(`{"mobile":"`+mobile+`","area": "`+area+`"}`))
	if err != nil {
		return ""
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	logger.Info("获取到的手机验证码： " + string(body))
	return string(body)
}

func GetMobileCaptchaForJiangSu(mobile string) (string, error) {
	c := lxhttp.NewHttpClient()
	req, err := http.NewRequest("POST", "https://public.listensoft.net/api/getTaxSms", strings.NewReader(`{"mobile":"`+mobile+`"}`))
	if err != nil {
		logger.Info(err)
		return "", err
	}
	req.Header.Set("Content-Type", "application/json")
	resp, err := c.Do(req)
	if err != nil {
		logger.Info(err)
		return "", err
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	logger.Info("获取到的手机验证码： " + string(body))
	return string(body), nil
}

// func GetMobileCaptchaForSiChuan(mobile, remark string) string {
// 	resp, err := http.Post("https://public.listensoft.net/api/getTaxSms", "application/json",
// 		strings.NewReader(`{"mobile":"`+mobile+`","remark":"`+remark+`"}`))
// 	if err != nil {
// 		return ""
// 	}
// 	body, err := io.ReadAll(resp.Body)
// 	if err != nil {
// 		return ""
// 	}
// 	return string(body)
// }
