package org.dromara.mp.service.impl;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.dromara.mp.service.WxMpTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 微信公众号模板消息服务实现类
 *
 * @author pengles
 */
@Service
public class WxMpTemplateServiceImpl implements WxMpTemplateService {

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

    @Autowired
    private WxMpService wxMpService;

    @Override
    public String sendTemplateMessage(WxMpTemplateMessage templateMessage) {
        try {
            String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            logger.info("发送模板消息成功: {}", msgId);
            return msgId;
        } catch (WxErrorException e) {
            logger.error("发送模板消息失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<WxMpTemplate> getAllTemplates() {
        try {
            List<WxMpTemplate> templates = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
            logger.info("获取模板列表成功，模板数量: {}", templates.size());
            return templates;
        } catch (WxErrorException e) {
            logger.error("获取模板列表失败: {}", e.getMessage(), e);
            return new ArrayList<>();
        }
    }

    @Override
    public boolean deleteTemplate(String templateId) {
        try {
            boolean result = wxMpService.getTemplateMsgService().delPrivateTemplate(templateId);
            logger.info("删除模板成功: {}", templateId);
            return result;
        } catch (WxErrorException e) {
            logger.error("删除模板失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public String sendNotificationMessage(String openId, String templateId, String url, Map<String, String> data) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .url(url)
                .build();

        // 添加模板数据
        for (Map.Entry<String, String> entry : data.entrySet()) {
            templateMessage.addData(new WxMpTemplateData(entry.getKey(), entry.getValue(), "#173177"));
        }

        return sendTemplateMessage(templateMessage);
    }

    @Override
    public String sendOrderMessage(String openId, String orderNo, String amount, String status) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId("ORDER_TEMPLATE_ID") // 需要配置实际的模板ID
                .build();

        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", "订单状态通知", "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword1", orderNo, "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword2", amount, "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword3", status, "#173177"));
        templateMessage.addData(new WxMpTemplateData("remark", "感谢您的支持！", "#173177"));

        return sendTemplateMessage(templateMessage);
    }

    @Override
    public String sendAuditResultMessage(String openId, String result, String reason) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId("AUDIT_TEMPLATE_ID") // 需要配置实际的模板ID
                .build();

        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", "审核结果通知", "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword1", result, "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword2", reason, "#173177"));
        templateMessage.addData(new WxMpTemplateData("remark", "如有疑问，请联系客服。", "#173177"));

        return sendTemplateMessage(templateMessage);
    }

    @Override
    public String sendActivityNotificationMessage(String openId, String activityName, String startTime, String endTime) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId("ACTIVITY_TEMPLATE_ID") // 需要配置实际的模板ID
                .build();

        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", "活动通知", "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword1", activityName, "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword2", startTime, "#173177"));
        templateMessage.addData(new WxMpTemplateData("keyword3", endTime, "#173177"));
        templateMessage.addData(new WxMpTemplateData("remark", "点击查看活动详情", "#173177"));

        return sendTemplateMessage(templateMessage);
    }


}