package com.simuwang.manage.service.impl;

import com.simuwang.base.common.conts.UserConstants;
import com.simuwang.base.common.enums.EmailCron;
import com.simuwang.base.common.enums.ResultCode;
import com.simuwang.base.common.util.EmailUtil;
import com.simuwang.base.common.util.StringUtil;
import com.simuwang.base.mapper.MailboxInfoMapper;
import com.simuwang.base.pojo.dos.MailboxInfoDO;
import com.simuwang.base.pojo.dto.MailboxInfoDTO;
import com.simuwang.base.pojo.vo.MailboxInfoTableVO;
import com.simuwang.base.pojo.vo.MailboxInfoVO;
import com.simuwang.manage.service.EmailConfigService;
import jakarta.mail.MessagingException;
import jakarta.mail.Store;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * FileName: EmailConfigServiceImpl
 * Author:   chenjianhua
 * Date:     2024/9/9 13:40
 * Description: ${DESCRIPTION}
 */
@Service
public class EmailConfigServiceImpl implements EmailConfigService {
    @Autowired
    private MailboxInfoMapper emailConfigMapper;

    @Autowired
    private Scheduler scheduler;
    @Override
    public List<MailboxInfoTableVO> searchEmailConfigList(String email) {
        List<MailboxInfoTableVO> mailboxInfoTableVOS = emailConfigMapper.searchEmailConfigList(email);
        for(MailboxInfoTableVO mailboxInfoTableVO : mailboxInfoTableVOS){  //cron表达式转文本描述
            mailboxInfoTableVO.setCron(EmailCron.getEmailCronByCron(mailboxInfoTableVO.getCron()).getText());
        }
        return mailboxInfoTableVOS;
    }

    @Override
    @Transactional
    public void saveEmailConfig(MailboxInfoVO mailboxInfoVO) {
        MailboxInfoDO mailboxInfoDO = toMailboxInfoDO(mailboxInfoVO);
        if(mailboxInfoVO.getId() == null){
            mailboxInfoDO.setCreateTime(new Date());
            emailConfigMapper.insert(mailboxInfoDO);
        }else{
            emailConfigMapper.updateById(mailboxInfoDO);
        }
        //添加定时任务

    }

    @Override
    public String connectTest(MailboxInfoVO mailboxInfoVO) {
        MailboxInfoDTO mailboxInfoDTO = new MailboxInfoDTO();
        mailboxInfoDTO.setAccount(mailboxInfoVO.getEmail());
        mailboxInfoDTO.setPassword(mailboxInfoVO.getPassword());
        mailboxInfoDTO.setPort(mailboxInfoVO.getPort());
        mailboxInfoDTO.setHost(mailboxInfoVO.getServer());
        Store store = EmailUtil.getStoreNew(mailboxInfoDTO);
        if(store != null){
            try {
                store.close();
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
            return ResultCode.CONNECT_SUCCESS.getMsg();
        }
        return ResultCode.CONNECT_ERROR.getMsg();
    }

    @Override
    public void deleteEmailConfig(String ids) {
        emailConfigMapper.deleteEmailConfigByIds(ids.split(","));
    }

    @Override
    public boolean checkEmailUnique(String email) {
        MailboxInfoDO mailboxInfoDO = emailConfigMapper.checkEmailUnique(email);
        if (StringUtil.isNotNull(mailboxInfoDO))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    private MailboxInfoDO toMailboxInfoDO(MailboxInfoVO mailboxInfoVO) {
        MailboxInfoDO mailboxInfoDO = new MailboxInfoDO();
        mailboxInfoDO.setIsvalid(1);
        mailboxInfoDO.setId(mailboxInfoVO.getId());
        mailboxInfoDO.setEmail(mailboxInfoVO.getEmail());
        mailboxInfoDO.setCron(EmailCron.getEmailCronByText(mailboxInfoVO.getCron()).getCron());
        mailboxInfoDO.setServer(mailboxInfoVO.getServer());
        mailboxInfoDO.setPort(mailboxInfoVO.getPort());
        mailboxInfoDO.setDescription(mailboxInfoVO.getDescription());
        mailboxInfoDO.setPassword(mailboxInfoVO.getPassword());
        mailboxInfoDO.setType(mailboxInfoVO.getType());
        mailboxInfoDO.setProtocol(mailboxInfoVO.getProtocol());
        mailboxInfoDO.setUpdateTime(new Date());
        mailboxInfoDO.setUpdaterId(999);
        mailboxInfoDO.setCreatorId(999);
        return mailboxInfoDO;
    }


}
