package hook

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/websocket"
	"strings"
)

// 获取标签信息
func (h *Hook) GetContactLabelList() (result GetContactLabelListReap, err error) {
	resp, err := h.Client.R().SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetContactLabelList")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("GetContactLabelList failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 获取好友和群信息
func (h *Hook) GetFriendAndChatRoomList(typeStr string) (result GetFriendAndChatRoomListReap, err error) {
	resp, err := h.Client.R().SetBody(&GetFriendAndChatRoomListReq{
		Type: typeStr,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetFriendAndChatRoomList")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 批量获取联系人简明信息(每次100个)
func (h *Hook) BatchGetContactBriefInfo(wxids []string) (result BatchGetContactBriefInfoReap, err error) {

	wxidList := strings.Join(wxids, ",")

	resp, err := h.Client.R().SetBody(&BatchGetContactBriefInfoReq{
		WxidList: wxidList,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/BatchGetContactBriefInfo")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("BatchGetChatRoomMemberWxid failed with status code %d", resp.StatusCode)
		return
	}
	return
}

// 添加新的好友
func (h *Hook) AddNewFriend(v3_wxid string, v4 string, desc string, addType string, role string) (result AddNewFriendReap, err error) {
	resp, err := h.Client.R().SetBody(&AddNewFriendReq{
		V3Wxid:  v3_wxid,
		V4:      v4,
		Desc:    desc,
		AddType: addType,
		Role:    role,
	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/AddNewFriend")
	if err != nil {
		return
	}
	if !resp.IsSuccessState() {
		err = fmt.Errorf("AddNewFriend failed with status code %d", resp.StatusCode)
		return
	}
	return
}

func (h *Hook) RequestChatRoomInfo(ChatRoomId, wxWxid string) error {
	conn, err := h.connWorkPhone()
	if err != nil {
		err = fmt.Errorf("RequestChatRoomInfo failed")
		return err
	}
	defer func(conn *websocket.Conn) {
		err = conn.Close()
		if err != nil {
			err = fmt.Errorf("RequestChatRoomInfo failed")
		}
	}(conn)

	message := map[string]interface{}{
		"MsgType": "RequestChatRoomInfoTask",
		"Content": map[string]interface{}{
			"WeChatId":   wxWxid,
			"Flag":       1,
			"ChatRoomId": ChatRoomId,
		},
	}
	transportMessageJSON, err := json.Marshal(message)
	if err != nil {
		return err
	}
	// 发送 JSON 消息
	err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
	if err != nil {
		return fmt.Errorf("failed to send message: %v", err)
	}

	return nil
}

func (h *Hook) TriggerChatroomPush(wxWxid string) error {
	conn, err := h.connWorkPhone()
	if err != nil {
		err = fmt.Errorf("TriggerChatroomPush failed")
		return err
	}
	defer func(conn *websocket.Conn) {
		err = conn.Close()
		if err != nil {
			err = fmt.Errorf("TriggerChatroomPush failed")
		}
	}(conn)

	message := map[string]interface{}{
		"MsgType": "TriggerChatroomPushTask",
		"Content": map[string]interface{}{
			"WeChatId": wxWxid,
			"Flag":     1,
		},
	}
	transportMessageJSON, err := json.Marshal(message)
	if err != nil {
		return err
	}
	// 发送 JSON 消息
	err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
	if err != nil {
		return fmt.Errorf("failed to send message: %v", err)
	}

	return nil
}

func (h *Hook) AddFriendInChatRoom(ChatRoomId, wxWxid, friendId, desc string) error {
	conn, err := h.connWorkPhone()
	if err != nil {
		err = fmt.Errorf("AddFriendInChatRoom failed")
		return err
	}
	defer func(conn *websocket.Conn) {
		err = conn.Close()
		if err != nil {
			err = fmt.Errorf("AddFriendInChatRoom failed")
		}
	}(conn)

	message := map[string]interface{}{
		"MsgType": "AddFriendInChatRoomTask",
		"Content": map[string]interface{}{
			"WeChatId":   wxWxid,
			"ChatroomId": ChatRoomId,
			"FriendId":   friendId,
			"Message":    desc,
		},
	}
	transportMessageJSON, err := json.Marshal(message)
	if err != nil {
		return err
	}
	// 发送 JSON 消息
	err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
	if err != nil {
		return fmt.Errorf("failed to send message: %v", err)
	}

	return nil
}
