package com.gxwebsoft.open.service.impl;

import cn.hutool.core.date.DateUtil;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OfficeMpServiceImpl implements com.gxwebsoft.open.service.OfficeMpServiceImpl {
    @Autowired
    private WxMpService wxMpService;

    @Override
    public void setMenu() throws WxErrorException {
        WxMpMenuService mpMenuService = wxMpService.getMenuService();
        WxMenu wxMenu = new WxMenu();
        List<WxMenuButton> wxMenuButtonList = new ArrayList<>();
        WxMenuButton wxMenuButton0 = new WxMenuButton();
//        会员商城：跳转小程序首页
//        申请入驻-会员店、合作伙伴、供应商
//        我的商家：商家教程、管理后台
        wxMenuButton0.setName("会员商城");
        wxMenuButton0.setType("miniprogram");
        wxMenuButton0.setAppId("wx309c034f7556aeb1");
        wxMenuButton0.setUrl("pages/index");

        WxMenuButton wxMenuButton1 = new WxMenuButton();
        List<WxMenuButton> wxMenuButton1SubList = new ArrayList<>();
        wxMenuButton1.setName("申请入驻");
        List<Map<String, String>> wxMenuButtonSubItemList = new ArrayList<>(){{
            add(new HashMap<String, String>() {{
                put("name", "会员店");
                put("url", "packages/shopAdmin/index");
            }});
            add(new HashMap<String, String>() {{
                put("name", "合作伙伴");
                put("url", "packages/shopCoop/index");
            }});
            add(new HashMap<String, String>() {{
                put("name", "供应商");
                put("url", "packages/supplierAdmin/index");
            }});
        }};
        for (Map<String, String> item : wxMenuButtonSubItemList) {
            WxMenuButton wxMenuButtonSubItem = new WxMenuButton();
            wxMenuButtonSubItem.setName(item.get("name"));
            wxMenuButtonSubItem.setType("miniprogram");
            wxMenuButtonSubItem.setAppId("wx309c034f7556aeb1");
            wxMenuButtonSubItem.setUrl(item.get("url"));
            wxMenuButton1SubList.add(wxMenuButtonSubItem);
        }

        wxMenuButton1.setSubButtons(wxMenuButton1SubList);

        WxMenuButton wxMenuButton2 = new WxMenuButton();
        wxMenuButton2.setName("我的商家");
        List<WxMenuButton> wxMenuButton2SubList = new ArrayList<>();
        List<Map<String, String>> wxMenuButton2SubItemList = new ArrayList<>(){{
            add(new HashMap<String, String>() {{
                put("name", "商家教程");
                put("url", "packages/shopAdmin/index");
            }});
            add(new HashMap<String, String>() {{
                put("name", "管理后台");
                put("url", "packages/shopAdmin/index");
            }});
        }};
        for (Map<String, String> item : wxMenuButton2SubItemList) {
            WxMenuButton wxMenuButtonSubItem = new WxMenuButton();
            wxMenuButtonSubItem.setName(item.get("name"));
            wxMenuButtonSubItem.setType("miniprogram");
            wxMenuButtonSubItem.setAppId("wx309c034f7556aeb1");
            wxMenuButtonSubItem.setUrl(item.get("url"));
            wxMenuButton2SubList.add(wxMenuButtonSubItem);
        }
        wxMenuButton2.setSubButtons(wxMenuButton2SubList);

        wxMenuButtonList.add(wxMenuButton0);
        wxMenuButtonList.add(wxMenuButton1);
        wxMenuButtonList.add(wxMenuButton2);
        wxMenu.setButtons(wxMenuButtonList);
        String res = mpMenuService.menuCreate(wxMenu);
        System.out.println("menu res :" + res );
    }

    @Override
    public void sendTemplateMsg(String toUser, String templateId, String url, List<WxMpTemplateData> data) throws WxErrorException {
        WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();

        wxMpTemplateMessage.setToUser(toUser);
//        wxMpTemplateMessage.setToUser("o-cMC6xD_bdZsn0bpcAORn8BTPIs");
        wxMpTemplateMessage.setTemplateId(templateId);
        wxMpTemplateMessage.setData(data);
        if (url != null && !url.isEmpty()) {
            wxMpTemplateMessage.setMiniProgram(new WxMpTemplateMessage.MiniProgram() {{
                setAppid("wx309c034f7556aeb1");
                setPagePath(url);
            }});
        }
        String res = wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        System.out.println("template res: " + res);
    }

    /**
     * TV审核通知
     *
     * @param openid
     * @param title
     * @param openUserName
     * @throws WxErrorException
     */
    @Override
    public void sendTVCheckMessage(String openid, String title, String openUserName) throws WxErrorException {
        System.out.println("sendTVCheckMessage:" + openid + "," + title + "," + openUserName);
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        if (title.length() > 20) title = title.substring(0, 16) + "...";
        wxMpTemplateDataList.add(new WxMpTemplateData("thing2", title));
        wxMpTemplateDataList.add(new WxMpTemplateData("thing20", openUserName));
        wxMpTemplateDataList.add(new WxMpTemplateData("time19", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss")));
        sendTemplateMsg(openid, "wHNFxLzbXa49I5ij0hcS65DXEGpbMrIRbKj9LWyrrpU", "package/user/tvAuth", wxMpTemplateDataList);
    }

    /**
     * 入驻审核通知
     *
     * @param openid
     * @param projectName
     * @param openUserName
     * @throws WxErrorException
     */
    @Override
    public void sendShopCheckMessage(String openid, String projectName, String openUserName) throws WxErrorException {
//        System.out.println("sendCheckMessage:" + openid + "," + projectName + "," + workType + "," + openUserName + "," + url);
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        if (projectName.length() > 20) projectName = projectName.substring(0, 16) + "...";
        wxMpTemplateDataList.add(new WxMpTemplateData("thing2", projectName));
        wxMpTemplateDataList.add(new WxMpTemplateData("thing20", openUserName));
        wxMpTemplateDataList.add(new WxMpTemplateData("time19", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss")));
        sendTemplateMsg(openid, "wHNFxLzbXa49I5ij0hcS65DXEGpbMrIRbKj9LWyrrpU", null, wxMpTemplateDataList);
    }

    /**
     * 派单通知
     *
     * @param openid
     * @param projectName
     * @param workType
     * @throws WxErrorException
     */
    @Override
    public void sendSendMessage(String openid, String projectName, String workType, String url) throws WxErrorException {
        System.out.println("sendSendMessageData:" + openid + "," + projectName + "," + workType + "," + url);
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        if (projectName.length() > 20) projectName = projectName.substring(0, 16) + "...";
        wxMpTemplateDataList.add(new WxMpTemplateData("thing25", projectName));
        wxMpTemplateDataList.add(new WxMpTemplateData("thing3", workType));
        wxMpTemplateDataList.add(new WxMpTemplateData("time5", DateUtil.now()));
        sendTemplateMsg(openid, "IWXjm5rt5QZV7Ll7GN7AAxORNSfgj-DnUoFHduAcwNg", url, wxMpTemplateDataList);
    }

    /**
     *  待发货通知
     * @param openid
     * @param orderNum
     * @param goodsName
     * @param address
     * @throws WxErrorException
     */
    @Override
    public void expressSendMessage(String openid, String orderNum, String goodsName, String address) throws WxErrorException {
        if (address.length() > 20) address = address.substring(0, 16) + "...";
        List<WxMpTemplateData> wxMpTemplateDataList = new ArrayList<>();
        wxMpTemplateDataList.add(new WxMpTemplateData("character_string1", orderNum));
        wxMpTemplateDataList.add(new WxMpTemplateData("thing5", goodsName));
        wxMpTemplateDataList.add(new WxMpTemplateData("thing9", address));
        sendTemplateMsg(openid, "qE1ZUkf_FLuJ6ab6U_lGfJObf7ANoAecnL69GvQ8gBU", "package/shopAdmin/orderList", wxMpTemplateDataList);
    }
}
