package core

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

type GetWorkWxUserListResponseDataItem struct {
	Avatar   string `json:"avatar"`
	Nickname string `json:"nickname"`
	Sex      int    `json:"sex"`
	Wxid     string `json:"wxid"`
}

type GetWorkWxUserListResponse struct {
	Data []GetWorkWxUserListResponseDataItem `json:"data"`
	Type int                                 `json:"type"`
}

// GetWorkWxUserList 获取企业微信用户列表
func (w *WxApi) GetWorkWxUserList() ([]GetWorkWxUserListResponseDataItem, error) {
	resp, err := w.internalCall(MtGetWorkWxUserListMsg, 20, MtGetWorkWxUserListMsg, nil, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetWorkWxUserListResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return rdata.Data, nil
}

type GetWorkWxRoomListResponseDataItem struct {
	Avatar      string `json:"avatar"`
	ManagerWxid string `json:"manager_wxid"`
	Nickname    string `json:"nickname"`
	RoomWxid    string `json:"room_wxid"`
	TotalMember int    `json:"total_member"`
}

type GetWorkWxRoomListResponse struct {
	Data []GetWorkWxRoomListResponseDataItem `json:"data"`
	Type int                                 `json:"type"`
}

// GetWorkWxRoomList 获取企业微信群列表
func (w *WxApi) GetWorkWxRoomList() ([]GetWorkWxRoomListResponseDataItem, error) {
	resp, err := w.internalCall(MtGetWorkWxChatRoomMsg, 20, MtGetWorkWxChatRoomMsg, nil, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetWorkWxRoomListResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return rdata.Data, nil
}

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

type GetWorkWxChatRoomMemberResponseData struct {
	MemberList []struct {
		Avatar   string `json:"avatar"`
		Nickname string `json:"nickname"`
		Sex      int    `json:"sex"`
		Wxid     string `json:"wxid"`
	} `json:"member_list"`
	RoomWxid string `json:"room_wxid"`
}

type GetWorkWxChatRoomMemberResponse struct {
	Data GetWorkWxChatRoomMemberResponseData `json:"data"`
	Type int                                 `json:"type"`
}

// GetWorkWxChatRoomMember 获取企业微信群成员列表
func (w *WxApi) GetWorkWxChatRoomMember(request GetWorkWxChatRoomMemberRequest) (*GetWorkWxChatRoomMemberResponseData, error) {
	resp, err := w.internalCall(MtGetWorkWxChatRoomMemberMsg, 20, MtGetWorkWxChatRoomMemberMsg, request, nil)
	if err != nil {
		return nil, err
	}
	var rdata GetWorkWxChatRoomMemberResponse
	err = json.Unmarshal([]byte(resp), &rdata)
	if err != nil {
		return nil, WxError{-1, err.Error()}
	}
	return &rdata.Data, nil
}
