package wechat

import (
	"context"
	"fmt"
	"github.com/imroc/req/v3"
	"github.com/zeromicro/go-zero/core/logx"
	"strconv"
	"time"
	"wechat-api/ent"
	"wechat-api/ent/wx"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
)

func SyncAllWx(svcCtx *svc.ServiceContext) {
	// 获取微信列表
	var result types.WorkPhoneGetWeChatsResp
	client := req.C().DevMode()
	client.SetCommonRetryCount(2).
		SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
		SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
	res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=0")
	if err != nil {
		logx.Error("syncWx： ", err)
		return
	}
	if !res.IsSuccessState() {
		logx.Error("GetWeChats failed with status code： ", res.StatusCode)
		return
	}

	// 遍历微信列表
	for _, account := range result.Data {
		if account.Wechatid == nil || *account.Wechatid == "" {
			continue
		}
		wxinfo, err := svcCtx.DB.Wx.Query().
			Where(
				wx.And(
					wx.WxidEQ(*account.Wechatid),
					wx.CtypeEQ(1),
				),
			).
			Only(context.TODO())

		if err != nil && !ent.IsNotFound(err) {
			logx.Error("syncWx： ", err)
			return
		}
		var status uint8
		logx.Info(fmt.Printf("*account.Isonline： %d\n", *account.Isonline))
		if *account.Isonline == 0 {
			status = 1
		} else {
			status = 0
		}

		if wxinfo != nil {
			logx.Info(fmt.Printf("wxinfo is not nil, account： %s\n", *account.Wechatid))
			logx.Info(fmt.Printf("wxinfo is not nil, account： %s\n", *account.Wechatnick))
			err = svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
				SetServerID(0).
				SetPort(*account.Deviceid).
				SetProcessID(strconv.FormatInt(account.Cid, 10)).
				SetNotNilAccount(account.Wechatno).
				SetNotNilNickname(account.Wechatnick).
				SetNotNilHeadBig(account.Avatar).
				SetStatus(status).
				Exec(context.TODO())
			if err != nil {
				logx.Error("syncWx： ", err)
				continue
			}
		} else {
			logx.Error(fmt.Printf("wxinfo is nil, account： %+v\n", account))
			_, err := svcCtx.DB.Wx.Create().
				SetServerID(0).
				SetPort(*account.Deviceid).
				SetProcessID(strconv.FormatInt(account.Cid, 10)).
				SetWxid(*account.Wechatid).
				SetNotNilAccount(account.Wechatno).
				SetNotNilHeadBig(account.Avatar).
				SetNotNilNickname(account.Wechatnick).
				SetStatus(status).
				SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
				Save(context.TODO())
			if err != nil {
				logx.Error("syncWx： ", err)
				continue
			}
		}

		//data := map[string]interface{}{
		//	"MsgType": "TriggerFriendPushTask",
		//	"Content": map[string]interface{}{
		//		"WeChatId": *account.Wechatid,
		//	},
		//}
		//jsonStr, err := json.Marshal(data)
		//err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
		//if err != nil {
		//	logx.Error("syncWx： ", err)
		//	continue
		//}
		//
		//dataChatroom := map[string]interface{}{
		//	"MsgType": "TriggerChatroomPushTask",
		//	"Content": map[string]interface{}{
		//		"WeChatId": *account.Wechatid,
		//		"Flag":     1,
		//	},
		//}
		//jsonStrChatroom, err := json.Marshal(dataChatroom)
		//err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStrChatroom))
		//if err != nil {
		//	logx.Error("syncWx： ", err)
		//	continue
		//}
	}
}
