package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.biz.service.TenantInfoSendMsgService;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.ym.mec.biz.dal.enums.MessageTypeEnum.*;

@Service
public class TenantInfoSendMsgServiceImpl implements TenantInfoSendMsgService {
    private static final Logger log = LoggerFactory.getLogger(TenantInfoSendMsgServiceImpl.class);

    @Autowired
    private SysMessageService sysMessageService;

    //开通
    public static final String OPEN = "open";
    //续费
    public static final String RENEW = "renew";
    //即将到期提醒
    public static final String EXPIRATION = "expiration";
    //云教室余额不足
    public static final String INSUFFICIENT = "insufficient";

    //开通
    private static final Map<MessageSenderPluginContext.MessageSender, MessageTypeEnum> OPEN_MSG_ENUM = new HashMap<>();
    //续费
    private static final Map<MessageSenderPluginContext.MessageSender, MessageTypeEnum> RENEW_MSG_ENUM = new HashMap<>();
    //即将到期提醒
    private static final Map<MessageSenderPluginContext.MessageSender, MessageTypeEnum> EXPIRATION_MSG_ENUM = new HashMap<>();
    //云教室余额不足
    private static final Map<MessageSenderPluginContext.MessageSender, MessageTypeEnum> INSUFFICIENT_MSG_ENUM = new HashMap<>();
    //init
    private static final Map<String, Map<MessageSenderPluginContext.MessageSender, MessageTypeEnum>> PLATFORM_ALL_MSG_ENUM = new HashMap<>();

    static {
        OPEN_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.EMAIL, EMAIL_TENANT_ACTIVATION_SUCCESSFUL);
        OPEN_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.AWSMS, SMS_TENANT_ACTIVATION_SUCCESSFUL);
        //续费
        RENEW_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.EMAIL, EMAIL_TENANT_RENEWAL_SUCCESSFUL);
        RENEW_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.AWSMS, SMS_TENANT_RENEWAL_SUCCESSFUL);
        //即将到期提醒
        EXPIRATION_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.EMAIL, EMAIL_TENANT_EXPIRATION_REMINDERS);
        EXPIRATION_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.AWSMS, SMS_TENANT_EXPIRATION_REMINDERS);
        //云教室余额不足
        INSUFFICIENT_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.EMAIL, EMAIL_TENANT_INSUFFICIENT_BALANCE);
        INSUFFICIENT_MSG_ENUM.put(MessageSenderPluginContext.MessageSender.AWSMS, SMS_TENANT_INSUFFICIENT_BALANCE);

        //init
        PLATFORM_ALL_MSG_ENUM.put(OPEN, OPEN_MSG_ENUM);
        PLATFORM_ALL_MSG_ENUM.put(RENEW, RENEW_MSG_ENUM);
        PLATFORM_ALL_MSG_ENUM.put(EXPIRATION, EXPIRATION_MSG_ENUM);
        PLATFORM_ALL_MSG_ENUM.put(INSUFFICIENT, INSUFFICIENT_MSG_ENUM);
    }

    /**
     * 以平台的名义发送邮件及短信
     *
     * @param receiveUserId 接收者userId
     * @param email         接收email的地址
     * @param phone         接收短信的电话
     * @param objs          发送的信息
     */
    @Override
    public void platformSendToAll(String type, Integer receiveUserId, String email, String phone, Object[] objs) {
        if (Objects.isNull(receiveUserId)) {
            return;
        }

        if (StringUtils.isNotBlank(email)) {
            Map<Integer, String> sendPar = new HashMap<>();
            sendPar.put(receiveUserId, email);
            log.info("platformSendToAll>>> receiveUserId {} email {} objs {} sendPar {}", receiveUserId, email, objs, sendPar);
            sysMessageService.batchSendMessage(-1, MessageSenderPluginContext.MessageSender.EMAIL,
                    PLATFORM_ALL_MSG_ENUM.get(type).get(MessageSenderPluginContext.MessageSender.EMAIL),
                    sendPar, null, 0, null,
                    "SYSTEM", objs);
        }

        if (StringUtils.isNotBlank(phone)) {
            Map<Integer, String> sendPar2 = new HashMap<>();
            sendPar2.put(receiveUserId, phone);
            log.info("platformSendToAll>>> receiveUserId {} phone {} objs {} sendPar {}", receiveUserId, phone, objs, sendPar2);
            sysMessageService.batchSendMessage(-1, MessageSenderPluginContext.MessageSender.AWSMS,
                    PLATFORM_ALL_MSG_ENUM.get(type).get(MessageSenderPluginContext.MessageSender.AWSMS),
                    sendPar2, null, 0, null,
                    "SYSTEM", objs);
        }
    }

}