package cn.reghao.tnb.message.app.rpc;

import cn.reghao.jutil.jdk.db.Page;
import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.string.StringRegexp;
import cn.reghao.tnb.message.api.dto.EmailDto;
import cn.reghao.tnb.message.api.iface.EmailAccountService;
import cn.reghao.tnb.message.app.db.mapper.EmailAccountMapper;
import cn.reghao.tnb.message.app.model.po.EmailAccount;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-09-25 13:56:58
 */
@DubboService
@Service
public class EmailAccountServiceImpl implements EmailAccountService {
    private final EmailAccountMapper emailAccountMapper;

    public EmailAccountServiceImpl(EmailAccountMapper emailAccountMapper) {
        this.emailAccountMapper = emailAccountMapper;
    }

    @Override
    public Result addEmailAccount(EmailDto emailDto) {
        String email = emailDto.getUsername();
        boolean matched = StringRegexp.matchEmail(email);
        if (!matched) {
            return Result.fail("邮箱地址格式不正确, 请检查");
        }

        EmailAccount emailAccount = emailAccountMapper.findByUsername(email);
        if (emailAccount != null) {
            String errMsg = String.format("邮箱 %s 已存在", email);
            return Result.fail(errMsg);
        }

        emailAccount = new EmailAccount(emailDto);
        int total = emailAccountMapper.count();
        if (total == 0) {
            emailAccount.setDefaultSender(true);
        }

        emailAccountMapper.save(emailAccount);
        return Result.success();
    }

    @Override
    public Result updatePassword(int emailId, String password) {
        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
        if (emailAccount == null) {
            String errMsg = String.format("邮箱不存在");
            return Result.fail(errMsg);
        }

        emailAccountMapper.updatePassword(emailId, password);
        return Result.success();
    }

    @Override
    public Result updateDefaultSender(int emailId) {
        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
        if (emailAccount == null) {
            String errMsg = String.format("邮箱不存在");
            return Result.fail(errMsg);
        }

        if (!emailAccount.getDefaultSender()) {
            EmailAccount emailAccount1 = emailAccountMapper.findDefaultSender();
            int emailId1 = emailAccount1.getId();

            emailAccountMapper.updateDefaultSender(emailId, true);
            emailAccountMapper.updateDefaultSender(emailId1, false);
        }

        return Result.success();
    }

    @Override
    public Result deleteEmailAccount(int emailId) {
        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
        if (emailAccount == null) {
            String errMsg = String.format("邮箱不存在");
            return Result.fail(errMsg);
        }

        if (emailAccount.getDefaultSender()) {
            String errMsg = String.format("不能删除默认发送邮箱");
            return Result.fail(errMsg);
        }

        emailAccountMapper.deleteById(emailId);
        return Result.success();
    }

    @Override
    public EmailDto getEmailAccount(int emailId) {
        EmailAccount emailAccount = emailAccountMapper.findById(emailId);
        return getEmailDto(emailAccount);
    }

    @Override
    public EmailDto getEmailSender() {
        EmailAccount emailAccount = emailAccountMapper.findDefaultSender();
        if (emailAccount == null) {
            return null;
        }

        return getEmailDto(emailAccount);
    }

    private EmailDto getEmailDto(EmailAccount emailAccount) {
        int id = emailAccount.getId();
        String smtp = emailAccount.getSmtp();
        String username = emailAccount.getUsername();
        String password = emailAccount.getPassword();
        String personal = emailAccount.getPersonal();
        String defaultSender = emailAccount.getDefaultSender() ? "YES" : "NO";
        return new EmailDto(id, smtp, username, password, personal, defaultSender);
    }

    @Override
    public PageList<EmailDto> getEmailAccounts(int pageNumber) {
        int pageSize = 100;
        Page page = new Page(pageNumber, pageSize);
        List<EmailDto> list = emailAccountMapper.findAll().stream().map(this::getEmailDto).collect(Collectors.toList());
        return PageList.pageList(page, pageSize, list);
    }
}
