package org.dromara.mp.service.impl;

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.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.dromara.mp.service.WxMpMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信公众号菜单管理服务实现类
 *
 * @author pengles
 */
@Service
public class WxMpMenuServiceImpl implements WxMpMenuService {

    private static final Logger logger = LoggerFactory.getLogger(WxMpMenuServiceImpl.class);

    @Autowired
    private WxMpService wxMpService;

    @Override
    public boolean createMenu(WxMenu menu) {
        try {
            String result = wxMpService.getMenuService().menuCreate(menu);
            logger.info("创建菜单成功: {}", result);
            return true;
        } catch (WxErrorException e) {
            logger.error("创建菜单失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean deleteMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
            logger.info("删除菜单成功");
            return true;
        } catch (WxErrorException e) {
            logger.error("删除菜单失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public WxMpMenu getMenu() {
        try {
            WxMpMenu menu = wxMpService.getMenuService().menuGet();
            logger.info("查询菜单成功");
            return menu;
        } catch (WxErrorException e) {
            logger.error("查询菜单失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public boolean createDefaultMenu() {
        WxMenu menu = new WxMenu();

        // 一级菜单1
        WxMenuButton button1 = new WxMenuButton();
        button1.setName("关于我们");
        button1.setType("click");
        button1.setKey("ABOUT_US");

        // 一级菜单2
        WxMenuButton button2 = new WxMenuButton();
        button2.setName("服务中心");

        // 二级菜单2-1
        WxMenuButton subButton21 = new WxMenuButton();
        subButton21.setName("在线客服");
        subButton21.setType("click");
        subButton21.setKey("ONLINE_SERVICE");

        // 二级菜单2-2
        WxMenuButton subButton22 = new WxMenuButton();
        subButton22.setName("常见问题");
        subButton22.setType("click");
        subButton22.setKey("FAQ");

        button2.getSubButtons().add(subButton21);
        button2.getSubButtons().add(subButton22);

        // 一级菜单3
        WxMenuButton button3 = new WxMenuButton();
        button3.setName("联系我们");
        button3.setType("click");
        button3.setKey("CONTACT_US");

        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        return createMenu(menu);
    }

    @Override
    public String handleMenuClick(String eventKey) {
        switch (eventKey) {
            case "ABOUT_US":
                return "欢迎了解我们！\n\n我们致力于为用户提供优质的服务和产品。";
            case "ONLINE_SERVICE":
                return "客服服务时间：\n周一至周五 9:00-18:00\n\n如需帮助，请直接回复您的问题。";
            case "FAQ":
                return "常见问题：\n\n1. 如何使用我们的服务？\n2. 如何联系客服？\n3. 服务时间是什么？\n\n更多问题请咨询在线客服。";
            case "CONTACT_US":
                return "联系方式：\n\n客服电话：400-123-4567\n客服邮箱：service@example.com\n工作时间：周一至周五 9:00-18:00";
            default:
                return "感谢您的关注！";
        }
    }
}