package com.storlead.sales.mail.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.sales.mail.entity.EmailFolderRuleEntity;
import com.storlead.sales.mail.entity.EmailsEntity;
import com.storlead.framework.mybatis.service.MyBaseService;
import com.storlead.sales.mail.entity.MailAttachmentEntity;
import com.storlead.sales.mail.entity.SmtpPopSettingsEntity;
import com.storlead.sales.mail.enums.EmailBoxEnum;
import com.storlead.sales.mail.enums.SendStatus;
import com.storlead.sales.mail.pojo.MailDTO;
import com.storlead.sales.mail.pojo.MailListDTO;
import com.storlead.sales.mail.pojo.SendMailDTO;
import com.storlead.sales.mail.pojo.vo.CustomerEmailVo;
import com.storlead.sales.mail.pojo.vo.NewMailCountTipVO;
import com.storlead.sales.mail.pojo.vo.UserClickTitleVO;
import com.storlead.sales.mail.util.ReceiveMailQueueThreadPool;
import org.apache.ibatis.annotations.Param;

import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 邮件表 服务类
 * </p>
 *
 * @author chenkq
 * @since 2024-05-28
 */
public interface EmailsService extends MyBaseService<EmailsEntity> {

    void updateSelectBindCustomerMail();
    /**
     * 保存到已发送邮箱
     */
    SendStatus sendEmail(SendMailDTO email, SmtpPopSettingsEntity smtpPop);

    Boolean sendEmail(EmailsEntity emails, SmtpPopSettingsEntity smtpPop);

    List<UserClickTitleVO> selectUserClickTitle(Long ownerBy, String title);

    /**
     * 收件
     *
     * @param smtpPop
     * @param folderName
     * @param boxEnum
     */
    void receiveEmails(SmtpPopSettingsEntity smtpPop);

    void receiveHeadEmails(SmtpPopSettingsEntity smtpPop,Boolean isBackTask);

    void receiveEmails(SmtpPopSettingsEntity smtpPop,Boolean isBackTask);
    /**
     * 收取头邮件
     *
     * @param smtpPop
     */
    void receiveHeadEmails(SmtpPopSettingsEntity smtpPop);


    /**
     * 获取所有的邮件
     * @param smtpPop
     * @param folderName
     * @param boxEnum
     * @return
     */
    List<Message> getMessagels(SmtpPopSettingsEntity smtpPop,String folderName, EmailBoxEnum boxEnum,Boolean isBackTask);

    List<Message> getMessagels(SmtpPopSettingsEntity smtpPop,String folderName, EmailBoxEnum boxEnum);
    /**
     * 获取邮件内容
     *
     * @param smtpPop
     */
    void receiveBodyContentEmails(SmtpPopSettingsEntity smtpPop,List<EmailsEntity> emails,EmailBoxEnum boxEnum);
    /**
     * 收件
     *
     * @param smtpPop
     * @param folderName
     * @param boxEnum
     */
    InputStream loadEmailsFile(SmtpPopSettingsEntity smtpPop, EmailsEntity email, String attachmentName);

    /**
     * 下载附件
     *
     * @param smtpPop
     * @param emails
     * @param attachments
     */
    void loadEmailsFiles(SmtpPopSettingsEntity smtpPop, List<EmailsEntity> emails, List<MailAttachmentEntity> attachments);

    EmailsEntity convertMessageToEmailVo(Message message,Boolean isPullHead);

    /**
     * 绑定联系人邮箱和客户关系
     *
     * @param mailId
     * @param mailAddress
     * @param userId
     */
    void bindLiaisonMailCustomerIdByMailIdAndUserId(Long mailId, String mailAddress, Long userId,Integer inOutMark);


    Long getEmailIdByMessgeId(String messageId,Long smtpPopId);

    EmailsEntity getEmailsByMessageId(String messageId,String folder,Long smtpPopId);

    List<String> getEmailMessageIdsByMessageIds(List<String> messageIds,String folder,Long smtpPopId);

    String getEmailMessageIdsByMessageId(String messageId,String folder,Long smtpPopId);

    Integer getEmailsByMessageId(String messageId,String fromAddress);
    /**
     * 获取邮件内容
     * @param message
     * @return
     */
    String getEmailBodyContent(Message message);


    /**
     * 下载邮件内容资源
     * @param smtpPop
     */
    void addLoadMailContentTask(SmtpPopSettingsEntity smtpPop);


    /**
     * 下载邮件内容资源
     * @param smtpPop
     */
    void loadMailContentTask(SmtpPopSettingsEntity smtpPop);


    /**
     * 启动下载任务
     * @param smtpPop
     */
    void addLoadMailFilesTask(SmtpPopSettingsEntity smtpPop);
    /**
     * 附件下载
     * @param smtpPop
     */
    void loadMailFiles(SmtpPopSettingsEntity smtpPop);


    /**
     * 下载文件
     * @param mailId
     * @param recipientDate
     * @param message
     * @param folderName
     * @param smtpPop
     * @return
     */
    Boolean downloadMailAttachment(Long mailId, Date recipientDate,Object messageContent,SmtpPopSettingsEntity smtpPop);

    /**
     * 只保存信息,不下载文件
     * @param mailId
     * @param recipientDate
     * @param message
     * @param smtpPop
     * @return
     */
    Boolean pullMailAttachmentHead(Long mailId, Date recipientDate,Object messageContent,SmtpPopSettingsEntity smtpPop);


    IPage<EmailsEntity> pageWithScope(Page page, MailDTO dto);

    IPage<EmailsEntity> pageWithScopeCus(Page page, MailDTO dto);

    IPage<EmailsEntity> pageWithScopeAllCus(Page page, MailDTO dto);

    void pullHeadMail(List<Message> messages, SmtpPopSettingsEntity smtpPop, EmailBoxEnum boxEnum, Set<String> blackls,List<EmailFolderRuleEntity> folderRules,Boolean isBackTask);

    /**
     * 汇总邮件数量
     * @param stmpPopId
     * @param ownerBy
     * @return
     */
    NewMailCountTipVO countMailSummaryNum(Long smtpPopId, Long ownerBy);

    /**
     * 汇总未跟进数
     * @param stmpPopId
     * @param ownerBy
     * @return
     */
    NewMailCountTipVO countCustomerAndClueNumber(Long smtpPopId, Long ownerBy);

    /**
     * 汇总客户邮件数
     * @param stmpPopId
     * @param ownerBy
     * @return
     */
    NewMailCountTipVO countCustomerFollowUpMailNum(Long smtpPopId, Long ownerBy);

    /**
     * 获取客户最后跟进时间
     * @param customerId
     * @return
     */
    LocalDateTime getCustomerLastFollowUpTime(Long customerId);

    /**
     * 解析邮件
     * @param messages
     */
    void analysisMailEml(List<Message> messages,Set<String> cusMails,Long ownerBy);

    /**
     * 解析邮件，返回数据
     * @param messages
     */
    List<EmailsEntity> analysisMailEmlls(List<Message> messages,Set<String> cusMails,Long ownerBy);

    Boolean analysisDownLoadAttachment(EmailsEntity entity, Message message);

    List<EmailsEntity> getDelayDeleteMail();

    IPage<CustomerEmailVo> selectCustomerEmailPage(Page<CustomerEmailVo> page,MailListDTO dto);

    void updateFollowUpTime(List<EmailsEntity> entities,Long ownerBy);
}
