package wx

import (
	"ColdVerify_server/logs"
	"ColdVerify_server/models/Account"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

var Qytoken string
func init() {
	go func() {
		gettoken()
		time.Sleep(time.Hour * 24)
	}()
}

func gettoken() {

	url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0befb06f19910467&corpsecret=83STGNLwghXsjYRacRhUHPL-fkkO1jwsMgabjXcRLyg"
	method := "GET"

	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return
	}

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

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))

	// Product _
	type Product struct {
		Access_token      string  `json:"access_token"`
		Errcode    int     `json:"errcode"`
	}

	p := &Product{}
	err = json.Unmarshal(body, p)
	if err != nil {
		time.Sleep(time.Second * 1)
		gettoken()
	}
	if p.Errcode != 0 {
		time.Sleep(time.Second * 1)
		gettoken()
	}

	Qytoken = p.Access_token

}
// Message represents the JSON structure
type Message struct {
	Touser           string `json:"touser"`
	Toparty          string `json:"toparty"`
	Totag            string `json:"totag"`
	Msgtype          string `json:"msgtype"`
	Agentid          int    `json:"agentid"`
	Text             Text    `json:"text"`
	Safe             int    `json:"safe"`
	EnableIDTrans    int    `json:"enable_id_trans"`
	EnableDuplicateCheck int `json:"enable_duplicate_check"`
}

// Text represents the text component of the message
type Text struct {
	Content string `json:"content"`
}
// @all  LiKeHui|xxxx
func WxSend(User,Content string) {
	if len(User) == 0 {
		logs.Println("Error User 为空")
		return
	}
	User = Account.AdminWXListToMap(Account.Read_Admin_List_ALL_1())[User]
	if len(User) == 0 {
		logs.Println("Error User 为空")
		return
	}
	url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+Qytoken
	method := "POST"

	// Example of creating and marshaling the struct to JSON
	message := Message{
		Touser:           User,
		Toparty:          "@all",
		Totag:            "@all",
		Msgtype:          "text",
		Agentid:          1000008,
		Text:             Text{Content: Content},
		Safe:             0,
		EnableIDTrans:    0,
		EnableDuplicateCheck: 0,
	}

	jsonData, err := json.Marshal(message)
	if err != nil {
		logs.Println("Error marshalling to JSON:", err)
		return
	}
	logs.Println(string(jsonData))
	payload := strings.NewReader(string(jsonData))

	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
		return
	}
	req.Header.Add("Content-Type", "application/json")

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

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	logs.Println("WxSend:",string(body))
	if strings.Contains(string(body),"access_token expired"){
		gettoken()
		time.Sleep(time.Second * 1)
		WxSend(User,Content)
	}

}