package com.smppw.modaq.application.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.smppw.modaq.application.util.EmailUtil;
import com.smppw.modaq.common.conts.DateConst;
import com.smppw.modaq.domain.dto.EmailContentInfoDTO;
import com.smppw.modaq.domain.dto.EmailZipFileDTO;
import com.smppw.modaq.domain.dto.MailboxInfoDTO;
import com.smppw.modaq.domain.entity.EmailFileInfoDO;
import com.smppw.modaq.domain.entity.EmailParseInfoDO;
import com.smppw.modaq.domain.entity.MailboxInfoDO;
import com.smppw.modaq.domain.mapper.EmailFileInfoMapper;
import com.smppw.modaq.domain.mapper.EmailParseInfoMapper;
import com.smppw.modaq.domain.mapper.MailboxInfoMapper;
import com.smppw.modaq.domain.service.EmailParseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author mozuwen
 * @date 2024-09-12
 * @description 邮件解析服务对外接口实现层
 */
@Service
public class EmailParseApiServiceImpl implements EmailParseApiService {

    private static final Logger log = LoggerFactory.getLogger(EmailParseApiServiceImpl.class);

    private final MailboxInfoMapper mailboxInfoMapper;
    private final EmailParseService emailParseService;
    private final EmailParseInfoMapper emailParseInfoMapper;
    private final EmailFileInfoMapper emailFileInfoMapper;
//    private final ThreadPoolTaskExecutor asyncExecutor;
//    private final EmailTaskInfoMapper emailTaskInfoMapper;

    public EmailParseApiServiceImpl(MailboxInfoMapper mailboxInfoMapper,
                                    EmailParseService emailParseService,
                                    EmailParseInfoMapper emailParseInfoMapper,
                                    EmailFileInfoMapper emailFileInfoMapper) {
        this.mailboxInfoMapper = mailboxInfoMapper;
        this.emailParseService = emailParseService;
        this.emailParseInfoMapper = emailParseInfoMapper;
        this.emailFileInfoMapper = emailFileInfoMapper;
    }

    @Override
    public void parseEmail(Date startDate, Date endDate, List<String> folderNames, List<Integer> emailTypes) {
        List<MailboxInfoDO> mailboxList = this.mailboxInfoMapper.listMailboxInfo();
        for (MailboxInfoDO mailbox : mailboxList) {
            MailboxInfoDTO paramDTO = new MailboxInfoDTO();
            paramDTO.setAccount(mailbox.getEmail());
            paramDTO.setPassword(mailbox.getPassword());
            paramDTO.setPort(mailbox.getPort());
            paramDTO.setHost(mailbox.getServer());
            paramDTO.setProtocol(mailbox.getProtocol());
            this.parseEmail(paramDTO, startDate, endDate, folderNames, emailTypes);
        }
    }

    @Override
    public void parseEmail(MailboxInfoDTO mailboxInfoDTO, Date startDate, Date endDate, List<String> folderNames, List<Integer> emailTypes) {
//        Integer userId = null;
//        try{
//            if(UserUtils.getPrincipal() == null){
//                userId = 1;
//            }else{
//                userId = UserUtils.getLoginUser().getUserId();
//            }
//        }catch (Exception e){
//            log.error(e.getMessage());
//        }
//        Integer finalUserId = userId;
//        asyncExecutor.execute(() -> {
////                      EmailTaskInfoDO emailTaskInfoDO = startEmailTask(mailboxInfoDTO.getAccount(), 1, finalUserId);
//                    try {
//
//                    } catch (Exception e) {
//                        log.error(e.getMessage(), e);
////                          endEmailTask(emailTaskInfoDO.getId(),-1);
////                        return;
//                    }
////                      endEmailTask(emailTaskInfoDO.getId(),2);
//                }
//        );
        emailParseService.parseEmail(mailboxInfoDTO, startDate, endDate, folderNames, emailTypes);
    }

//    private void endEmailTask(Integer id,Integer taskStatus) {
//        try{
//            EmailTaskInfoDO emailTaskInfoDO = new EmailTaskInfoDO();
//            emailTaskInfoDO.setId(id);
//            emailTaskInfoDO.setTaskStatus(taskStatus);
//            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
//            emailTaskInfoDO.setEndTime(DateUtils.getNowDate());
//            emailTaskInfoMapper.updateTaskStatusById(emailTaskInfoDO);
//        }catch (Exception e){
//            log.error(e.getMessage());
//        }
//    }
//
//    private EmailTaskInfoDO startEmailTask(String email,Integer taskStatus,Integer userId) {
//        EmailTaskInfoDO  emailTaskInfoDO = new EmailTaskInfoDO();
//        try{
//            emailTaskInfoDO.setTaskName(TaskType.EMAIL_PARSE.getInfo());
//            emailTaskInfoDO.setTaskType(TaskType.EMAIL_PARSE.getType());
//            emailTaskInfoDO.setTaskStatus(taskStatus);
//            emailTaskInfoDO.setStartTime(DateUtils.getNowDate());
//            emailTaskInfoDO.setIsvalid(1);
//            emailTaskInfoDO.setEmail(email);
//            emailTaskInfoDO.setCreateTime(DateUtils.getNowDate());
//            emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
//            emailTaskInfoDO.setCreatorId(userId);
//            emailTaskInfoDO.setUpdaterId(userId);
//            emailTaskInfoMapper.insert(emailTaskInfoDO);
//        }catch (Exception e){
//            log.error(e.getMessage());
//        }
//        return emailTaskInfoDO;
//    }

    @Override
    public void reparseEmail(Integer emailId) {
        // 查询邮件信息
        EmailParseInfoDO emailParseInfoDO = emailParseInfoMapper.queryById(emailId);
        if (emailParseInfoDO == null) {
            log.info("邮件不存在 ->邮件id:{}", emailId);
            return;
        }
        //解析成功的邮件不再解析
        if (emailParseInfoDO.getParseStatus() == 1) {
            log.info("邮件解析状态为成功，不再解析 ->邮件id:{}", emailId);
            return;
        }
        List<EmailFileInfoDO> emailFileInfoDOList = emailFileInfoMapper.queryByEmailId(emailId);
        if (CollUtil.isEmpty(emailFileInfoDOList)) {
            log.info("该邮件不存在附件 -> 邮件id:{}", emailId);
            return;
        }
//        // 邮件字段识别映射表
//        Map<String, List<String>> emailFieldMap = emailParseService.getEmailFieldMapping();
//        // 邮件类型配置
//        Map<Integer, List<String>> emailTypeMap = emailParseService.getEmailType();

        // 解析流程
        List<EmailContentInfoDTO> emailContentInfoDTOList = buildEmailContentInfoDTO(emailId, emailParseInfoDO, emailFileInfoDOList);

//        List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
        Map<EmailContentInfoDTO, List<EmailZipFileDTO>> emailZipFileMap = MapUtil.newHashMap();
        for (EmailContentInfoDTO emailContentInfoDTO : emailContentInfoDTOList) {
            try {
                List<EmailZipFileDTO> emailZipFiles = emailParseService.parseZipEmail(emailContentInfoDTO);
                emailZipFileMap.put(emailContentInfoDTO, emailZipFiles);
//                    emailFundNavDTOList.addAll(fundNavDTOList);
            } catch (Exception e) {
                log.error("重新解析邮件失败,邮件id:{},堆栈信息:{}", emailId, ExceptionUtil.stacktraceToString(e));
            }
        }
        // 保存相关信息 -> 邮件信息表,邮件文件表,邮件净值表,邮件规模表,基金净值表
        emailParseService.saveRelatedTable(null, emailParseInfoDO.getEmail(), emailZipFileMap);
    }
//
//    @Override
//    public void reparseFile(List<Integer> fileIdList) {
//        if (CollUtil.isEmpty(fileIdList)) {
//            return;
//        }
//        List<EmailInfoDTO> emailParseInfoDOList = emailParseInfoMapper.queryValuationEmailByFileId(fileIdList);
//        if (CollUtil.isEmpty(emailParseInfoDOList)) {
//            return;
//        }
//        asyncExecutor.execute(() -> {
//            Map<Integer, List<EmailInfoDTO>> emailIdFileMap = emailParseInfoDOList.stream().collect(Collectors.groupingBy(EmailInfoDTO::getId));
//            for (Map.Entry<Integer, List<EmailInfoDTO>> entry : emailIdFileMap.entrySet()) {
//                Integer emailId = entry.getKey();
//                List<EmailInfoDTO> emailInfoDTOList = entry.getValue();
//                String emailAddress = emailInfoDTOList.get(0).getEmail();
//                List<EmailContentInfoDTO> emailContentInfoDTOList = emailInfoDTOList.stream().map(this::buildEmailContentInfoDTO).collect(Collectors.toList());
//
//                List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
//                Map<EmailContentInfoDTO, List<EmailFundNavDTO>> fileNameNavMap = MapUtil.newHashMap();
//                for (EmailContentInfoDTO emailContentInfoDTO : emailContentInfoDTOList) {
//                    try {
//                        log.info("开始重新解析文件 -> 文件id:{}", emailContentInfoDTO.getFileId());
//                        List<EmailFundNavDTO> fundNavDTOList = emailParseService.parseEmail(emailContentInfoDTO, MapUtil.newHashMap());
//                        fileNameNavMap.put(emailContentInfoDTO, fundNavDTOList);
//                        emailFundNavDTOList.addAll(fundNavDTOList);
//                    } catch (Exception e) {
//                        log.error("重新解析文件失败,邮件id:{},文件id:{},堆栈信息:{}", emailId, emailContentInfoDTO.getFileId(), ExceptionUtil.stacktraceToString(e));
//                    }
//                }
//                // 保存相关信息 -> 邮件信息表,邮件文件表,邮件净值表,邮件规模表,基金净值表
//                emailParseService.saveRelatedTable(emailAddress, emailContentInfoDTOList, fileNameNavMap);
//            }
//            log.info("重新解析文件结束... -> 文件id:{}", fileIdList);
//        });
//    }

//    private EmailContentInfoDTO buildEmailContentInfoDTO(EmailInfoDTO emailInfoDTO) {
//        String emailDate = DateUtils.format(emailInfoDTO.getEmailDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
//        String parseDate = DateUtils.format(new Date(), DateConst.YYYY_MM_DD_HH_MM_SS);
//        EmailContentInfoDTO contentInfoDTO = new EmailContentInfoDTO();
//        contentInfoDTO.setEmailId(emailInfoDTO.getId());
//        contentInfoDTO.setFileId(emailInfoDTO.getFileId());
//        contentInfoDTO.setEmailAddress(emailInfoDTO.getEmail());
//        contentInfoDTO.setEmailDate(emailDate);
//        contentInfoDTO.setEmailTitle(emailInfoDTO.getEmailTitle());
//        contentInfoDTO.setParseDate(parseDate);
//        contentInfoDTO.setFileName(emailInfoDTO.getFileName());
//        contentInfoDTO.setFilePath(emailInfoDTO.getFilePath());
//        contentInfoDTO.setEmailType(emailInfoDTO.getEmailType());
//        return contentInfoDTO;
//    }

    private List<EmailContentInfoDTO> buildEmailContentInfoDTO(Integer emailId,
                                                               EmailParseInfoDO emailParseInfoDO,
                                                               List<EmailFileInfoDO> emailFileInfoDOList) {
        List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
        String emailDate = DateUtil.format(emailParseInfoDO.getEmailDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
        String parseDate = DateUtil.format(new Date(), DateConst.YYYY_MM_DD_HH_MM_SS);
        for (EmailFileInfoDO fileInfoDO : emailFileInfoDOList) {
            EmailContentInfoDTO contentInfoDTO = new EmailContentInfoDTO();
            contentInfoDTO.setEmailId(emailId);
            contentInfoDTO.setFileId(fileInfoDO.getId());
            contentInfoDTO.setSenderEmail(emailParseInfoDO.getSenderEmail());
            contentInfoDTO.setEmailAddress(emailParseInfoDO.getEmail());
            contentInfoDTO.setEmailDate(emailDate);
            contentInfoDTO.setEmailTitle(emailParseInfoDO.getEmailTitle());
            contentInfoDTO.setParseDate(parseDate);
            contentInfoDTO.setFileName(fileInfoDO.getFileName());
            contentInfoDTO.setFilePath(fileInfoDO.getFilePath());
            Integer emailType = EmailUtil.getEmailTypeBySubject(emailParseInfoDO.getEmailTitle());
            contentInfoDTO.setEmailType(emailType);
            String emailContent = readHtmlFileContent(fileInfoDO.getFilePath());
            contentInfoDTO.setEmailContent(emailContent);
            contentInfoDTO.setAiFileId(fileInfoDO.getAiFileId());
            emailContentInfoDTOList.add(contentInfoDTO);
        }
        return emailContentInfoDTOList;
    }

    public static String readHtmlFileContent(String filePath) {
        if (StrUtil.isNotBlank(filePath) && filePath.endsWith("html")) {
            StringBuilder content = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    // 追加每一行到StringBuilder中
                    content.append(line).append("\n");
                }
            } catch (IOException e) {
                System.err.println("Error reading the file: " + e.getMessage());
                return null;
            }
            return content.toString();
        }
        return null;
    }

}
