package com.usoftchina.dingtalk.sdk.dto;

import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;

import java.util.*;

/**
 * @author yingp
 */
public class SendMessageReq {

    private final static int MAX_USER_SIZE = 100;
    private final static int MAX_PARTY_SIZE = 20;

    private Set<String> userSet;
    private Set<String> deptSet;
    /**
     * 消息内容
     */
    private AbstractMessageBody body;
    /**
     * 企业应用的id
     */
    private Long agentId;

    public SendMessageReq toUser(String... userIds) {
        if (null == userSet) {
            userSet = new HashSet<>(userIds.length);
        }
        for (String userId : userIds) {
            userSet.add(userId);
        }
        return this;
    }

    public SendMessageReq toDept(Long... deptIds) {
        if (null == deptSet) {
            deptSet = new HashSet<>(deptIds.length);
        }
        for (Long deptId : deptIds) {
            deptSet.add(String.valueOf(deptId));
        }
        return this;
    }

    public SendMessageReq agentId(long agentId) {
        this.agentId = agentId;
        return this;
    }

    public Long getAgentId() {
        return agentId;
    }

    /**
     * 文本消息
     *
     * @param content
     * @return
     */
    public SendMessageReq text(String content) {
        this.body = new Text(content);
        return this;
    }

    /**
     * 图片消息
     *
     * @param mediaId
     * @return
     */
    public SendMessageReq image(String mediaId) {
        this.body = new Image(mediaId);
        return this;
    }

    /**
     * 文件消息
     *
     * @param mediaId
     * @return
     */
    public SendMessageReq file(String mediaId) {
        this.body = new File(mediaId);
        return this;
    }

    /**
     * 文本卡片消息
     *
     * @param title
     * @param description
     * @param url
     * @return
     */
    public SendMessageReq actionCard(String title, String description, String url) {
        this.body = new ActionCard(title, description, url);
        return this;
    }

    /**
     * markdown消息
     *
     * @param title
     * @param content
     * @return
     */
    public SendMessageReq markdown(String title, String content) {
        this.body = new Markdown(title, content);
        return this;
    }

    public OapiMessageCorpconversationAsyncsendV2Request build() {
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        if (null != userSet) {
            if (userSet.size() > MAX_USER_SIZE) {
                throw new RuntimeException(String.format("最多支持%s个用户", MAX_USER_SIZE));
            }
            request.setUseridList(String.join(",", userSet));
        } else if (null != deptSet) {
            if (deptSet.size() > MAX_PARTY_SIZE) {
                throw new RuntimeException(String.format("最多支持%s个部门", MAX_PARTY_SIZE));
            }
            request.setDeptIdList(String.join(",", deptSet));
        } else {
            request.setToAllUser(true);
        }
        request.setMsg(body.build());
        return request;
    }

    interface AbstractMessageBody {
        OapiMessageCorpconversationAsyncsendV2Request.Msg build();
    }

    class Text implements AbstractMessageBody {
        private final String content;

        public Text(String content) {
            this.content = content;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("text");
            msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
            msg.getText().setContent(content);
            return msg;
        }
    }

    class Image implements AbstractMessageBody {
        /**
         * 图片媒体文件id，可以调用上传临时素材接口获取
         */
        private final String mediaId;

        public Image(String mediaId) {
            this.mediaId = mediaId;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("image");
            msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
            msg.getImage().setMediaId(mediaId);
            return msg;
        }
    }

    class File implements AbstractMessageBody {
        /**
         * 文件id，可以调用上传临时素材接口获取
         */
        private final String mediaId;

        public File(String mediaId) {
            this.mediaId = mediaId;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("file");
            msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
            msg.getImage().setMediaId(mediaId);
            return msg;
        }
    }

    class ActionCard implements AbstractMessageBody {
        private final String title;
        private final String description;
        private final String url;

        public ActionCard(String title, String description, String url) {
            this.title = title;
            this.description = description;
            this.url = url;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
            msg.getActionCard().setTitle(title);
            msg.getActionCard().setMarkdown(description);
            msg.getActionCard().setSingleTitle("查看详情");
            msg.getActionCard().setSingleUrl(url);
            msg.setMsgtype("action_card");
            return msg;
        }
    }

    class Link implements AbstractMessageBody {
        private final String title;
        private final String text;
        private final String messageUrl;
        private final String picUrl;

        public Link(String title, String text, String messageUrl, String picUrl) {
            this.title = title;
            this.text = text;
            this.messageUrl = messageUrl;
            this.picUrl = picUrl;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("link");
            msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
            msg.getLink().setTitle(title);
            msg.getLink().setText(text);
            msg.getLink().setMessageUrl(messageUrl);
            msg.getLink().setPicUrl(picUrl);
            return msg;
        }
    }

    class Markdown implements AbstractMessageBody {
        private final String title;
        private final String text;

        public Markdown(String title, String text) {
            this.title = title;
            this.text = text;
        }

        @Override
        public OapiMessageCorpconversationAsyncsendV2Request.Msg build() {
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("markdown");
            msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
            msg.getMarkdown().setText(text);
            msg.getMarkdown().setTitle(title);
            return msg;
        }
    }
}
