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.support.MybatisPage;
import com.simuwang.base.common.util.DateUtils;
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.CompanyEmailSendHistoryDO;
import com.simuwang.base.pojo.dos.MailboxInfoDO;
import com.simuwang.base.pojo.dto.MailboxInfoDTO;
import com.simuwang.base.pojo.dto.query.EmailPageQuery;
import com.simuwang.base.pojo.vo.MailboxInfoTableVO;
import com.simuwang.base.pojo.vo.MailboxInfoVO;
import com.simuwang.base.pojo.vo.OpenStatusVO;
import com.simuwang.manage.service.EmailConfigService;
import com.smppw.common.pojo.ResultVo;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 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 MybatisPage<MailboxInfoTableVO> searchEmailConfigList(EmailPageQuery emailPageQuery) {
        List<MailboxInfoDO> mailboxInfoOS = emailConfigMapper.searchEmailConfigList(emailPageQuery);
        List<MailboxInfoTableVO> mailboxInfoTableVOS = mailboxInfoOS.stream().map(MailboxInfoDO::toTableVo).collect(Collectors.toList());
        long total = emailConfigMapper.countEmailConfig(emailPageQuery);
        return MybatisPage.of(total,mailboxInfoTableVOS);
    }

    @Override
    @Transactional
    public void saveEmailConfig(MailboxInfoVO mailboxInfoVO) {
        MailboxInfoDO mailboxInfoDO = toMailboxInfoDO(mailboxInfoVO);
        if(mailboxInfoVO.getId() == null){
            mailboxInfoDO.setCreateTime(new Date());
            mailboxInfoDO.setCron(EmailCron.getEmailCronByText(mailboxInfoVO.getCron()).getCron());
            emailConfigMapper.insert(mailboxInfoDO);
        }else{
            emailConfigMapper.updateById(mailboxInfoDO);
        }
    }

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

    @Override
    public void deleteEmailConfig(Integer id) {
        List<Integer> idList = new ArrayList<>();
        idList.add(id);
        emailConfigMapper.deleteEmailConfigByIds(idList);
    }

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

    @Override
    public MailboxInfoVO searchEmailConfigById(Integer id) {
        MailboxInfoDO mailboxInfoDO = emailConfigMapper.searchEmailConfigById(id);
        MailboxInfoVO mailboxInfoVO = mailboxInfoDO.toVo();
        return mailboxInfoVO;
    }

    @Override
    public void controlEmailConfig(OpenStatusVO openStatusVO) {
        MailboxInfoDO mailboxInfoDO = emailConfigMapper.searchEmailConfigById(openStatusVO.getId());
        mailboxInfoDO.setOpenStatus(openStatusVO.getOpenStatus());
        mailboxInfoDO.setUpdateTime(DateUtils.getNowDate());
        emailConfigMapper.updateById(mailboxInfoDO);
    }

    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.setOpenStatus(mailboxInfoVO.getOpenStatus());
        mailboxInfoDO.setUpdateTime(new Date());
        mailboxInfoDO.setUpdaterId(999);
        mailboxInfoDO.setCreatorId(999);
        return mailboxInfoDO;
    }


}
