package com.usoftchina.qywx.sdk.test;

import com.usoftchina.qywx.sdk.dto.CreateChatReq;
import com.usoftchina.qywx.sdk.dto.SendChatReq;
import com.usoftchina.qywx.sdk.dto.SendMessageReq;
import com.usoftchina.qywx.sdk.MessageSdk;
import com.usoftchina.qywx.sdk.util.UrlUtils;
import org.junit.Test;

import java.net.URLEncoder;
import java.util.*;

/**
 * @author yingp
 */
public class MessageSdkTest extends BaseTest {

    private static MessageSdk sdk;

    static {
        sdk = new MessageSdk(properties);
    }

    @Test
    public void sendTaskCard() throws Exception {
        SendMessageReq.Btn btn1 = new SendMessageReq.Btn("yes", "批准", "已批准");
        SendMessageReq.Btn btn2 = new SendMessageReq.Btn("no", "驳回", "已驳回");
        String oauthUrl = UrlUtils.generateOAuthUrl("wwbb7e27c4decb7872", "N_USOFTSYS", "UasAudit", "http://2788e5c924.qicp.vip/uas",
                "http://2788e5c924.qicp.vip/qywx/api/authorize", "/jsps/common/flow.jsp?whoami=JProcess!Me&formCondition=jp_nodeId=44382369&gridCondition=null=44382369&_noc=1");
        sdk.send("UasAudit", new SendMessageReq()
                .taskCard("工作协助", "吕全明向你发起了工作协助<div class=\"highlight\">2019050003</div>",
                        oauthUrl, "2019050003", Arrays.asList(btn1, btn2))
                .toUser("U0308"));
    }

    @Test
    public void sendTextCard() throws Exception {
        String oauthUrl = UrlUtils.generateOAuthUrl("wwd42c39382ee6298e", "N_USOFTSYS", "Uas", "http://2788e5c924.qicp.vip/uas",
                "http://2788e5c924.qicp.vip/qywx/api/authorize","/jsps/common/flow.jsp?whoami=JProcess!Me&formCondition=jp_nodeId=44382369&gridCondition=null=44382369&_noc=1");
        sdk.send("UasAudit", new SendMessageReq()
                .textCard("工作协助", "吕全明向你发起了工作协助<div class=\"highlight\">2019050002</div>", oauthUrl, "详情")
                .toUser("U0308"));
    }

    @Test
    public void createChat() {
        String chatId = sdk.createChat("Uas", new CreateChatReq().name("微信项目讨论组")
                .owner("YingPeng")
                .user("YingPeng", "ZhouYuan"));
        System.out.println(chatId);
        sdk.sendChat("Uas", new SendChatReq(chatId).text("Hello World"));
    }

    @Test
    public void sendProcessMessage() throws Exception {
        String paramsStr = "{\"master\":\"N_USOFTSYS\",\"nodeId\":53840602,\"baseUrl\":\"" + URLEncoder.encode("http://erp.yitoa.com:8888/ERP/", "utf-8") + "\"}";
        String msgUrl = UrlUtils.generateOAuthUrl("wwd42c39382ee6298e", "N_USOFTSYS", "Uas", "http://erp.yitoa.com:8888/ERP/",
                "http://erp.yitoa.com:8888/office/api/authorize", "uas/approval/" + URLEncoder.encode(paramsStr, "utf-8"));
        sdk.send("Uas", new SendMessageReq()
                .textCard("应鹏的工作日报流程", "单据编号:2020020258", msgUrl, "查看详情")
                .toUser("U0301"));
    }
}
