package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.common.constants.NacosProperty;
import com.fdkankan.ucenter.common.constants.ResultCodeMsg;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.mapper.IMailTemplateMapper;
import com.fdkankan.ucenter.mq.vo.LaserSendMail;
import com.fdkankan.ucenter.service.IMailTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IScenePlusExtService;
import com.fdkankan.ucenter.util.SendMailUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-01-10
 */
@Service
@Slf4j
public class MailTemplateServiceImpl extends ServiceImpl<IMailTemplateMapper, MailTemplate> implements IMailTemplateService {

    @Autowired
    IScenePlusExtService scenePlusExtService;

    @Override
    public Boolean sendMail(String email , MailTemplate mailTemplate, String file) {
        if(mailTemplate == null){
            throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400005, ResultCodeMsg.FAILURE_MSG_400005);
        }
        return SendMailUtils.sendMail(mailTemplate.getSendMail(), mailTemplate.getSendPassword(), mailTemplate.getSendHost(),
                email, mailTemplate.getSubject(), mailTemplate.getMsg(), file);
    }

    @Override
    public MailTemplate getTemplate(Integer type,String lang) {
        if(NacosProperty.uploadType.equals("aws")){
            lang = "en";
        }else {
            lang = "zh";
        }
        LambdaQueryWrapper<MailTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MailTemplate::getType,type);
        wrapper.eq(MailTemplate::getLang,lang);
        return this.getOne(wrapper);
    }
    public MailTemplate getTemplateEn(Integer type) {
        LambdaQueryWrapper<MailTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MailTemplate::getType,type);
        wrapper.eq(MailTemplate::getLang,"en");
        return this.getOne(wrapper);
    }

    @Override
    public Boolean sendCodeMail(String email, String code,String lang) {
        MailTemplate mailTemplate = this.getTemplate(2,lang);
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(code)){
                msg = msg.replace("{code}",code);
            }
            mailTemplate.setMsg(msg);
        }
        return sendMail(email,mailTemplate,null);
    }

    @Override
    public Boolean sendPeExMail(String email,String memberLevels) {
        MailTemplate mailTemplate = this.getTemplate(3,"en");
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(memberLevels)){
                msg = msg.replace("{memberLevels}",getMemberLeversMsg(memberLevels));
                msg = msg.replace("{money}",getMoney(memberLevels));
                msg = msg.replace("{time}",getTime(memberLevels));
            }
            mailTemplate.setMsg(msg);
        }
        return sendMail(email,mailTemplate,null);
    }

    @Override
    public Boolean sendPeNoExMail(String email,Integer day,String memberLevels) {
        MailTemplate mailTemplate = this.getTemplate(4,"en");
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(memberLevels)){
                msg = msg.replace("{memberLevels}",getMemberLeversMsg(memberLevels));
                msg = msg.replace("{money}",getMoney(memberLevels));
                msg = msg.replace("{time}",getTime(memberLevels));
            }
            if(day != null && day >0){
                msg = msg.replace("{day}",day.toString());
            }

            mailTemplate.setMsg(msg);
        }
        return sendMail(email,mailTemplate,null);
    }

    @Override
    public Boolean sendPeTodayExMail(String email,String memberLevels) {
        MailTemplate mailTemplate = this.getTemplate(5,"en");
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(memberLevels)){
                msg = msg.replace("{memberLevels}",getMemberLeversMsg(memberLevels));
                msg = msg.replace("{money}",getMoney(memberLevels));
                msg = msg.replace("{time}",getTime(memberLevels));
            }
            mailTemplate.setMsg(msg);
        }
        return sendMail(email,mailTemplate,null);
    }

    @Override
    public Boolean sendSceneCooperation(List<ScenePro> proList, List<ScenePlus> plusList, String username,String lang) {
        try {
            if(proList.size()<=0 && plusList.size() <=0){
                return false;
            }
            StringBuilder webSites = new StringBuilder();
            for (ScenePro scenePro : proList) {
                webSites.append(scenePro.getWebSite()).append("<br>");
            }
            for (ScenePlus scenePlus : plusList) {
                ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
                if(scenePlusExt != null){
                    webSites.append(scenePlusExt.getWebSite()).append("<br>");
                }
            }
            if(StringUtils.isBlank(webSites)){
                return false;
            }
            MailTemplate mailTemplate = this.getTemplate(6,lang);
            if(mailTemplate != null){
                String msg = mailTemplate.getMsg();
                if(StringUtils.isNotBlank(webSites)){
                    msg = msg.replace("{sceneWebSites}",webSites.toString());
                }
                mailTemplate.setMsg(msg);
            }
            return sendMail(username,mailTemplate,null);
        }catch (Exception e){
            log.error("邮件发送失败,type:{},error:{}","协作场景邮件",e);
        }
        return false;
    }
    @Override
    public Boolean sendSceneCooperation(List<LaserSendMail> vos) {
        try {
            if(vos.isEmpty()){
                return false;
            }
            StringBuilder webSites = new StringBuilder();
            String username = null;
            for (LaserSendMail vo : vos) {
                username = vo.getUserName();
                webSites.append(vo.getWebSite()).append("<br>");
            }
            if(StringUtils.isBlank(username)){
                return false;
            }
            if(StringUtils.isBlank(webSites)){
                return false;
            }
            MailTemplate mailTemplate = this.getTemplate(6,"en");
            if(mailTemplate != null){
                String msg = mailTemplate.getMsg();
                if(StringUtils.isNotBlank(webSites)){
                    msg = msg.replace("{sceneWebSites}",webSites.toString());
                }
                mailTemplate.setMsg(msg);
            }
            return sendMail(username,mailTemplate,null);
        }catch (Exception e){
            log.error("邮件发送失败,type:{},error:{}","协作场景邮件",e);
        }
        return false;
    }

    @Override
    public Boolean sendCameraCooperation(HashMap<Long, Camera> cameraMap, String username, String lang) {
        try {
            if(cameraMap.size()<=0 ){
                return false;
            }
            StringBuilder snCodes = new StringBuilder();
            for (Long cameraId : cameraMap.keySet()) {
                if(cameraMap.get(cameraId) != null){
                    snCodes.append(cameraMap.get(cameraId).getWifiName()).append("<br>");
                }
            }
            if(StringUtils.isBlank(snCodes)){
                return false;
            }
            MailTemplate mailTemplate = this.getTemplate(8,lang);
            if(mailTemplate != null){
                String msg = mailTemplate.getMsg();
                if(StringUtils.isNotBlank(snCodes)){
                    msg = msg.replace("{snCodes}",snCodes.toString());
                }
                mailTemplate.setMsg(msg);
            }
            return sendMail(username,mailTemplate,null);
        }catch (Exception e){
            log.error("邮件发送失败,type:{},error:{}","协作场景邮件",e);
        }
        return false;
    }

    @Override
    public void sendPayErrorMail(String email,String memberLevels) {
        MailTemplate mailTemplate = this.getTemplate(9,"en");
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(memberLevels)){
                msg = msg.replace("{memberLevels}",getMemberLeversMsg(memberLevels));
                msg = msg.replace("{money}",getMoney(memberLevels));
                msg = msg.replace("{time}",getTime(memberLevels));
            }
            mailTemplate.setMsg(msg);
        }
        sendMail(email,mailTemplate,null);
    }

    @Override
    public void sendSubcriptionCancelledMail(String email, String memberLevels) {
        MailTemplate mailTemplate = this.getTemplate(10,"en");
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(memberLevels)){
                //PREMIUM/SENIOR
                msg = msg.replace("{memberLevels}",getMemberLeversMsg(memberLevels));
            }
            mailTemplate.setMsg(msg);
        }
        sendMail(email,mailTemplate,null);
    }

    @Override
    public Boolean sendContactUs(String email,String callBackUrl) {
        MailTemplate mailTemplate = this.getTemplateEn(12);
        //<a href="{callBackUrl}" target="_blank" rel="noopener">{callBackUrl}</a>
        if(mailTemplate != null){
            String msg = mailTemplate.getMsg();
            if(StringUtils.isNotBlank(callBackUrl)){
                msg = msg.replace("{callBackUrl}",callBackUrl);
            }
            mailTemplate.setMsg(msg);
        }
       return sendMail(email,mailTemplate,null);
    }

    @Override
    public void sendContactUs2(String email,Integer type) {
        MailTemplate mailTemplate = this.getTemplateEn(type);
        sendMail(email,mailTemplate,null);
    }

    public  String getMemberLeversMsg(String memberLevels){
        switch (memberLevels){
            case "PR" :return "PREMIUM";
            case "SE" :return "SENIOR";
        }
        return "PREMIUM/SENIOR";
    }

    public  String getTime(String memberLevels){
        switch (memberLevels){
            case "PR" :return "year";
            case "SE" :return "month";
        }
        return "year/month";
    }

    public  String getMoney(String memberLevels){
        switch (memberLevels){
            case "PR" :return "$980";
            case "SE" :return "$40";
        }
        return "$980/$40";
    }
}
