package dingdingapi

import "strings"

const (
	MaxWorkMsgLen     = 2048
	MaxWorkContentLen = MaxWorkMsgLen - 96
)

const (
	MsgTyeText        = "txt"
	MsgTypeImage      = "image"
	MsgTypeVoice      = "voice"
	MsgTypeFile       = "file"
	MsgTypeLink       = "link"
	MsgTypeOA         = "oa"
	MsgTypeMarkdown   = "markdown"
	MsgTypeActionCard = "action_card"
)

func (a *API) CorpAsyncSend(userIDList, deptIDList []string, isToAllUser bool, msg map[string]interface{}) (err error) {
	params := map[string]interface{}{
		"agent_id": a.agentID,
		"msg":      msg,
	}
	if len(userIDList) > 0 {
		params["userid_list"] = strings.Join(userIDList, ",")
	}
	if len(deptIDList) > 0 {
		params["dept_id_list"] = strings.Join(deptIDList, ",")
	}
	if isToAllUser {
		params["to_all_user"] = isToAllUser
	}
	_, err = a.AccessAPI("topapi/message/corpconversation/asyncsend_v2", nil, params)
	return err
}

func (a *API) CorpAsyncSendSimple(userID, content string) (err error) {
	return a.CorpAsyncSend([]string{userID}, nil, false, map[string]interface{}{
		"msgtype": "text",
		"text": map[string]interface{}{
			"content": content,
		},
	})
}

func (a *API) CorpAsyncSendMarkdown(userIDList, deptIDList []string, isToAllUser bool, title, content string) (err error) {
	return a.CorpAsyncSend(userIDList, deptIDList, isToAllUser, map[string]interface{}{
		"msgtype": "markdown",
		"markdown": map[string]interface{}{
			"title": title,
			"text":  content,
		},
	})
}
