package qywxapi

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
)

const (
	BldGroupID = "wr0oKiEAAAw0osTU89uH7mtEg6YqzQ3A"
	GyGroupID  = "wr0oKiEAAAmSw5HwLHZQIb29yR6We_lg"
	CsGroupID  = "wr0oKiEAAAAi7dreYASbygbXrWtNxbNg"
)

func (a *API) GetProviderToken() (token string, err error) {
	params := map[string]interface{}{
		"corpid":          a.appID,
		"provider_secret": a.secret,
	}
	str, _ := json.Marshal(params)
	result, err := a.AccessAPI(tokenURL, string(str), true)
	if result["provider_access_token"] != "" {
		return result["provider_access_token"].(string), err
	}
	return token, err
}

func (a *API) GetToken() (token string, err error) {
	result, err := a.AccessAPI(fmt.Sprintf("cgi-bin/gettoken?corpid=%s&corpsecret=%s", a.appID, a.secret), "", false)
	if result["access_token"] != "" {
		return result["access_token"].(string), err
	}
	return token, err
}

//获取客户群列表
func (a *API) GroupchatList(cursor string, limit int) (groupchatListResult *GroupchatListResult, err error) {
	params := map[string]interface{}{
		"limit":  limit,
		"cursor": cursor,
	}
	str, _ := json.Marshal(params)
	result, err := a.AccessAPI(fmt.Sprintf("cgi-bin/externalcontact/groupchat/list?access_token=%s", a.token), string(str), true)
	if err == nil {
		utils.Map2StructByJson(result, &groupchatListResult, false)
	}
	return groupchatListResult, err
}

// 获取客户群详情
func (a *API) Groupchat(chat_id string) (groupchatListResult *GroupchatResutl, err error) {
	params := map[string]interface{}{
		"chat_id": chat_id,
	}
	str, _ := json.Marshal(params)
	result, err := a.AccessAPI(fmt.Sprintf("cgi-bin/externalcontact/groupchat/get?access_token=%s", a.token), string(str), true)
	if err == nil {
		utils.Map2StructByJson(result, &groupchatListResult, false)
	}
	return groupchatListResult, err
}
