package core

import (
	"github.com/goWxHook/goWxHook/utils"
	"github.com/goWxHook/goWxHook/utils/json"
)

type GetChatRoomListResponse struct {
	Data []GetChatRoomListResponseDataItem `json:"data"`
	Type int                               `json:"type"`
}

type GetChatRoomListResponseDataItem struct {
	Avatar      string   `json:"avatar"`
	IsManager   int      `json:"is_manager"`
	ManagerWxid string   `json:"manager_wxid"`
	MemberList  []string `json:"member_list"`
	Nickname    string   `json:"nickname"`
	Remark      string   `json:"remark"`
	TotalMember int      `json:"total_member"`
	Wxid        string   `json:"wxid"`
}

// GetChatRoomList 群聊列表
func (w *WxApi) GetChatRoomList() ([]GetChatRoomListResponseDataItem, error) {
	resp, err := w.internalCall(MtDataChatroomsMsg, 10, MtDataChatroomsMsg, map[string]interface{}{"detail": 1}, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetChatRoomListResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return rdata.Data, nil
}

type GetChatRoomMemberListResponse struct {
	Data GetChatRoomMemberListResponseData `json:"data"`
	Type int                               `json:"type"`
}

type GetChatRoomMemberListResponseData struct {
	Extend     string `json:"extend"`
	GroupWxid  string `json:"group_wxid"`
	MemberList []struct {
		Account     string `json:"account"`
		Avatar      string `json:"avatar"`
		City        string `json:"city"`
		Country     string `json:"country"`
		DisplayName string `json:"display_name"`
		Nickname    string `json:"nickname"`
		Province    string `json:"province"`
		Remark      string `json:"remark"`
		Sex         int    `json:"sex"`
		Wxid        string `json:"wxid"`
	} `json:"member_list"`
	Total int `json:"total"`
}

// GetChatRoomMemberList 获取群成员列表
func (w *WxApi) GetChatRoomMemberList(roomWxid string) (*GetChatRoomMemberListResponseData, error) {
	resp, err := w.internalCall(MtDataChatroomMembersMsg, 20, MtDataChatroomMembersMsg, map[string]interface{}{"room_wxid": roomWxid}, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetChatRoomMemberListResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return &rdata.Data, nil
}

// GetRoomDetailInfoByProtocol 获取群详细信息（协议）
func (w *WxApi) GetRoomDetailInfoByProtocol(roomWxid string) ([]GetFriendDetailInfoByProtocolResponseDataContactListItem, error) {
	resp, err := w.internalCall(MtGetFriendDetailMsg, 20, MtGetFriendDetailMsg, map[string]interface{}{"wxid": roomWxid}, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetFriendDetailInfoByProtocolResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return rdata.Data.ContactList, nil
}

type GetRoomMemberInviteListResponseData struct {
	MemberList []struct {
		Avatar   string `json:"avatar"`
		Flag     int    `json:"flag"`
		InviteBy string `json:"invite_by"`
		Nickname string `json:"nickname"`
		Wxid     string `json:"wxid"`
	} `json:"member_list"`
	RoomWxid string `json:"room_wxid"`
}

type GetRoomMemberInviteListResponse struct {
	Data GetRoomMemberInviteListResponseData `json:"data"`
	Type int                                 `json:"type"`
}

// GetRoomMemberInviteList 获取群成员邀请关系(协议)
func (w *WxApi) GetRoomMemberInviteList(roomWxid string) (*GetRoomMemberInviteListResponseData, error) {
	resp, err := w.internalCall(MtGetChatRoomInviteRelationMsg, 20, MtGetChatRoomInviteRelationMsg, map[string]interface{}{"room_wxid": roomWxid}, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetRoomMemberInviteListResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return &rdata.Data, nil
}

type CreateRoomRequest struct {
	Wxids []string `json:"wxids"`
}

// CreateChatRoom 创建群聊
func (w *WxApi) CreateChatRoom(request CreateRoomRequest) error {
	_, err := w.internalCall(0, -1, MtCreateRoomMsg, request.Wxids, nil)
	return err
}

type CreateChatRoomByProtocolResponseData struct {
	BaseResponse struct {
		ErrMsg struct {
			String string `json:"string"`
		} `json:"errMsg"`
		Ret int `json:"ret"`
	} `json:"baseResponse"`
	BigHeadImgUrl string `json:"bigHeadImgUrl"`
	ChatRoomName  struct {
		String string `json:"string"`
	} `json:"chatRoomName"`
	ImgBuf struct {
		Buffer string `json:"buffer"`
		ILen   int    `json:"iLen"`
	} `json:"imgBuf"`
	MemberCount int `json:"memberCount"`
	MemberList  []struct {
		City        string `json:"city"`
		ContactType int    `json:"contactType"`
		Country     string `json:"country"`
		MemberName  struct {
			String string `json:"string"`
		} `json:"memberName"`
		MemberStatus int `json:"memberStatus"`
		NickName     struct {
			String string `json:"string"`
		} `json:"nickName"`
		PersonalCard int    `json:"personalCard"`
		Province     string `json:"province"`
		PyInitial    struct {
			String string `json:"string"`
		} `json:"pyInitial"`
		QuanPin struct {
			String string `json:"string"`
		} `json:"quanPin"`
		Remark struct {
		} `json:"remark"`
		RemarkPYInitial struct {
		} `json:"remarkPYInitial"`
		RemarkQuanPin struct {
		} `json:"remarkQuanPin"`
		Sex        int    `json:"sex"`
		Signature  string `json:"signature"`
		VerifyFlag int    `json:"verifyFlag"`
		VerifyInfo string `json:"verifyInfo"`
	} `json:"memberList"`
	PyInitial struct {
	} `json:"pyInitial"`
	QuanPin struct {
	} `json:"quanPin"`
	SmallHeadImgUrl string `json:"smallHeadImgUrl"`
	Topic           struct {
	} `json:"topic"`
}

type CreateChatRoomByProtocolResponse struct {
	Data CreateChatRoomByProtocolResponseData `json:"data"`
	Type int                                  `json:"type"`
}

// CreateChatRoomByProtocol 创建群聊(协议)
func (w *WxApi) CreateChatRoomByProtocol(request CreateRoomRequest) (*CreateChatRoomByProtocolResponseData, error) {
	resp, err := w.internalCall(MtCreateRoomByProtocolMsg, 20, MtCreateRoomByProtocolMsg, request.Wxids, nil)
	if err != nil {
		return nil, err
	}
	var rdata CreateChatRoomByProtocolResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return &rdata.Data, nil
}

type InviteToRoomRequest struct {
	RoomWxid   string   `json:"room_wxid"`
	MemberList []string `json:"member_list"`
}

type InviteToRoomResponseData struct {
	BaseResponse struct {
		ErrMsg struct {
			String string `json:"string"`
		} `json:"errMsg"`
		Ret int `json:"ret"`
	} `json:"baseResponse"`
	MemberCount int `json:"memberCount"`
	MemberList  []struct {
		City        string `json:"city"`
		ContactType int    `json:"contactType"`
		Country     string `json:"country"`
		MemberName  struct {
			String string `json:"string"`
		} `json:"memberName"`
		MemberStatus int `json:"memberStatus"`
		NickName     struct {
			String string `json:"string"`
		} `json:"nickName"`
		PersonalCard int    `json:"personalCard"`
		Province     string `json:"province"`
		PyInitial    struct {
			String string `json:"string"`
		} `json:"pyInitial"`
		QuanPin struct {
			String string `json:"string"`
		} `json:"quanPin"`
		Remark struct {
		} `json:"remark"`
		RemarkPYInitial struct {
		} `json:"remarkPYInitial"`
		RemarkQuanPin struct {
		} `json:"remarkQuanPin"`
		Sex        int    `json:"sex"`
		Signature  string `json:"signature"`
		VerifyFlag int    `json:"verifyFlag"`
		VerifyInfo string `json:"verifyInfo"`
	} `json:"memberList"`
}

type InviteToRoomResponse struct {
	Data InviteToRoomResponseData `json:"data"`
	Type int                      `json:"type"`
}

// InviteToRoom 批量拉人进入群聊
func (w *WxApi) InviteToRoom(request InviteToRoomRequest) (*InviteToRoomResponseData, error) {
	rr := utils.Ifs(len(request.MemberList) > 40, MtInviteToRoomReqMsg, MtInviteToRoomMsg)
	resp, err := w.internalCall(rr, 30, rr, request, nil)
	if err != nil {
		return nil, err
	}
	var rdata InviteToRoomResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return &rdata.Data, nil
}

type DelChatRoomMemberRequest struct {
	RoomWxid   string   `json:"room_wxid"`
	MemberList []string `json:"member_list"`
}

// DelChatRoomMember 删除群成员
func (w *WxApi) DelChatRoomMember(request DelChatRoomMemberRequest) error {
	_, err := w.internalCall(MtDelRoomMemberMsg, 30, MtDelRoomMemberMsg, request, nil)
	return err
}

type EditChatRoomMemberRequest struct {
	RoomWxid string `json:"room_wxid"`
	Name     string `json:"name"`
}

// EditChatRoomName 修改群聊名称
func (w *WxApi) EditChatRoomName(request EditChatRoomMemberRequest) error {
	_, err := w.internalCall(0, -1, MtModRoomNameMsg, request, nil)
	return err
}

type EditChatRoomNoticeRequest struct {
	RoomWxid string `json:"room_wxid"`
	Notice   string `json:"notice"`
}

// EditChatRoomNotice 修改群公告
func (w *WxApi) EditChatRoomNotice(request EditChatRoomNoticeRequest) error {
	_, err := w.internalCall(0, -1, MtModRoomNoticeMsg, request, nil)
	return err
}

type EditRoomMineNickNameRequest struct {
	RoomWxid string `json:"room_wxid"`
	Nickname string `json:"nickname"`
}

// EditRoomMineNickName 修改我在本群的昵称
func (w *WxApi) EditRoomMineNickName(request EditRoomMineNickNameRequest) error {
	_, err := w.internalCall(0, -1, MtModRoomMemberNameMsg, request, nil)
	return err
}

type RoomShowNickNameRequest struct {
	RoomWxid string `json:"room_wxid"`
	Status   int    `json:"status"` // 1显示 0隐藏
}

// RoomShowNickName 群聊是否显示昵称
func (w *WxApi) RoomShowNickName(request RoomShowNickNameRequest) error {
	_, err := w.internalCall(0, -1, MtModRoomShowNameMsg, request, nil)
	return err
}

type SaveRoomSaveToContactRequest struct {
	RoomWxid string `json:"room_wxid"`
	Status   int    `json:"status"` // 1保存 2移出
}

// SaveRoomSaveToContact 保存到/移出通讯录
func (w *WxApi) SaveRoomSaveToContact(request SaveRoomSaveToContactRequest) error {
	_, err := w.internalCall(0, -1, MtSaveRoomToContactMsg, request, nil)
	return err
}

type DelChatRoomRequest struct {
	RoomWxid string `json:"room_wxid"`
}

// DelChatRoom 退出并删除群聊
func (w *WxApi) DelChatRoom(request DelChatRoomRequest) error {
	_, err := w.internalCall(0, -1, MtQuitDelRoomMsg, request, nil)
	return err
}

type AutoAcceptRoomRequest struct {
	Auto int `json:"auto"` // 0不自动 1自动
}

// AutoAcceptRoom 自动接收群邀请
func (w *WxApi) AutoAcceptRoom(request AutoAcceptRoomRequest) error {
	_, err := w.internalCall(0, -1, MtAutoAcceptRoomMsg, request, nil)
	return err
}
