package com.storlead.mail.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.mail.pojo.dto.EmailTemplatesDTO;
import com.storlead.mail.pojo.entity.EmailsEntity;
import com.storlead.mail.pojo.entity.SmtpPopSettingsEntity;
import com.storlead.mail.pojo.entity.UserEmailFolderEntity;
import com.storlead.mail.service.EmailsService;
import com.storlead.mail.service.UserEmailFolderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户文件夹 前端控制器
 * </p>
 *
 * @author chenkq
 * @since 2024-11-12
 */
@RestController
@RequestMapping("/user/email/folder")
@Api(tags = "邮件: 邮件自定义文件夹")
public class UserEmailFolderApiController {
    @Resource
    private UserEmailFolderService emailFolderService;

    @Resource
    private EmailsService emailsService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "获取用户文件夹(分页)" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> pageList(EmailTemplatesDTO dto) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        Page<UserEmailFolderEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<UserEmailFolderEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserEmailFolderEntity::getOwnerBy,currentUserId);
        queryWrapper.eq(UserEmailFolderEntity::getIsDelete,Integer.valueOf(0));
        IPage<UserEmailFolderEntity> pageList = emailFolderService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取用户文件夹" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> list() {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        LambdaQueryWrapper<UserEmailFolderEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserEmailFolderEntity::getOwnerBy,currentUserId);
        queryWrapper.eq(UserEmailFolderEntity::getIsDelete,Integer.valueOf(0));
        queryWrapper.eq(UserEmailFolderEntity::getEnabled,Integer.valueOf(1));
        List<UserEmailFolderEntity> ls = emailFolderService.list(queryWrapper);
        return Result.result(ls);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "绑定文件夹入站规则" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> save(UserEmailFolderEntity entity) {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        LambdaQueryWrapper<UserEmailFolderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEmailFolderEntity::getFolderName,entity.getFolderName());
        queryWrapper.eq(UserEmailFolderEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        queryWrapper.eq(UserEmailFolderEntity::getOwnerBy,currentUserId);

        if (Objects.isNull(entity.getId())) {
            Integer exit = emailFolderService.count(queryWrapper);
            if (exit > 0) {
                return Result.error("文件夹'"+ entity.getFolderName() +"'已存在!");
            }
        } else {
            UserEmailFolderEntity oldFolder = emailFolderService.getOne(queryWrapper);
            if (Objects.nonNull(oldFolder)) {
                if (!oldFolder.getId().equals(entity.getId())) {
                    return Result.error("文件夹'"+ entity.getFolderName() +"'已存在,无法修改!");
                }
            }
        }
        emailFolderService.saveOrUpdate(entity);
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping("deleteIds")
    public Result delete(Long [] ids) {
        if(Objects.isNull(ids)) {
            return Result.error("参数错误");
        }
        /**
         * 需要修改邮件文件夹标记为''
         */
        emailFolderService.lgDelete(Arrays.asList(ids));

        LambdaUpdateWrapper<EmailsEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(EmailsEntity::getCustomFolderId,Arrays.asList(ids));
        updateWrapper.set(EmailsEntity::getCustomFolderId,null);
        emailsService.update(updateWrapper);
        return Result.ok();
    }


    @ApiOperation("禁用或启用")
    @PostMapping("enabled")
    public Result enabled(Long id,Boolean enable) {
        LambdaUpdateWrapper<UserEmailFolderEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserEmailFolderEntity::getEnabled,enable);
        updateWrapper.eq(UserEmailFolderEntity::getId,id);
        emailFolderService.update(updateWrapper);
        return Result.ok();
    }
}
