package group

import (
	"gitee.ltd/lxh/xybot/base"
	"gitee.ltd/lxh/xybot/core"
	"strings"
)

// AddChatroomMember
// @description: 添加群聊成员(群聊最多40人)
// @receiver s
// @param chatroom
// @param inviteWxId
// @return err
func (s service) AddChatroomMember(chatroom string, inviteWxId string) (err error) {
	var result base.Response[base.EmptyResponse]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":        core.WxId,
			"Chatroom":    chatroom,
			"InviteWxids": inviteWxId,
		}).Post("/AddChatroomMember")
	err = result.CheckError(err)
	return
}

// InviteChatroomMember
// @description: 邀请群聊成员(群聊大于40人)
// @receiver s
// @param chatroom
// @param inviteWxIds
// @return err
func (s service) InviteChatroomMember(chatroom string, inviteWxIds []string) (err error) {
	var result base.Response[base.EmptyResponse]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":        core.WxId,
			"Chatroom":    chatroom,
			"InviteWxids": strings.Join(inviteWxIds, ","),
		}).Post("/InviteChatroomMember")
	err = result.CheckError(err)
	return
}

// GetChatroomInfo
// @description: 获取群聊详情 这个接口不建议用,没多大卵用
// @receiver s
// @param chatroom
// @return info
// @return err
func (s service) GetChatroomInfo(chatroom string) (info GetChatroomInfoResponse, err error) {
	var result base.Response[GetChatroomInfoResponse]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":     core.WxId,
			"Chatroom": chatroom,
		}).Post("/GetChatroomInfo")
	if err = result.CheckError(err); err != nil {
		return
	}
	info = result.Data
	return
}

// GetChatroomInfoNoAnnounce
// @description: 获取群聊详情(不包含群公告)
// @receiver s
// @param chatroom
// @return info
// @return err
func (s service) GetChatroomInfoNoAnnounce(chatroom []string) (info []ChatroomInfoItem, err error) {
	var result base.Response[GetChatroomInfoNoAnnounce]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":     core.WxId,
			"Chatroom": strings.Join(chatroom, ","),
		}).Post("/GetChatroomInfoNoAnnounce")
	if err = result.CheckError(err); err != nil {
		return
	}
	info = result.Data.ContactList
	return
}

// GetChatroomMemberDetail
// @description: 获取群聊成员详情
// @receiver s
// @param chatroom
// @return members
// @return err
func (s service) GetChatroomMemberDetail(chatroom string) (members []ChatRoomMemberItem, err error) {
	var result base.Response[GetChatroomMemberDetailResponse]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":     core.WxId,
			"Chatroom": chatroom,
		}).Post("/GetChatroomMemberDetail")
	if err = result.CheckError(err); err != nil {
		return
	}
	members = result.Data.NewChatroomData.ChatRoomMember
	return
}

// GetChatroomQRCode
// @description: 获取群二维码
// @receiver s
// @param chatroom
// @return imgBase64Str
// @return notify
// @return err
func (s service) GetChatroomQRCode(chatroom string) (imgBase64Str, notify string, err error) {
	var result base.Response[GetChatroomQRCodeResponse]
	_, err = s.client.R().
		SetResult(&result).
		SetBody(map[string]any{
			"Wxid":     core.WxId,
			"Chatroom": chatroom,
		}).Post("/GetChatroomQRCode")
	if err = result.CheckError(err); err != nil {
		return
	}
	imgBase64Str = result.Data.Qrcode.Buffer
	notify = result.Data.RevokeQrcodeWording
	return
}