package cn.stylefeng.team.portal.modular.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.portal.api.enums.PortalAuthTypeEnum;
import cn.stylefeng.team.portal.modular.management.entity.PortalDesign;
import cn.stylefeng.team.portal.modular.management.entity.PortalDesignManager;
import cn.stylefeng.team.portal.modular.management.enums.PortalDesignManagerExceptionEnum;
import cn.stylefeng.team.portal.modular.management.factory.ManagerFactory;
import cn.stylefeng.team.portal.modular.management.mapper.PortalDesignManagerMapper;
import cn.stylefeng.team.portal.modular.management.pojo.request.ManagerUpdateRequest;
import cn.stylefeng.team.portal.modular.management.pojo.request.PortalDesignManagerRequest;
import cn.stylefeng.team.portal.modular.management.service.PortalDesignManagerService;
import cn.stylefeng.team.portal.modular.management.service.PortalDesignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PortalDesignManagerServiceImpl extends ServiceImpl<PortalDesignManagerMapper, PortalDesignManager> implements PortalDesignManagerService {

    @Resource
    private PortalDesignService portalDesignService;

    @Override
    public List<PortalDesignManager> getCurrentUserPortalManageList() {

        Long userId = LoginContext.me().getLoginUser().getUserId();

        LambdaQueryWrapper<PortalDesignManager> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PortalDesignManager::getManagerUserId, userId);
        List<PortalDesignManager> managerLinkList = this.list(wrapper);
        if (ObjectUtil.isEmpty(managerLinkList)) {
            managerLinkList = new ArrayList<>();
        }

        LambdaQueryWrapper<PortalDesign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseEntity::getCreateUser, userId);
        List<PortalDesign> designCreateUserList = portalDesignService.list(queryWrapper);

        List<String> createUserAuthList = new ArrayList<>();
        createUserAuthList.add(PortalAuthTypeEnum.DESIGN.getCode());
        createUserAuthList.add(PortalAuthTypeEnum.CONTENT.getCode());
        createUserAuthList.add(PortalAuthTypeEnum.ADMIN.getCode());
        for (PortalDesign portalDesign : designCreateUserList) {
            PortalDesignManager portalDesignManager = new PortalDesignManager();
            portalDesignManager.setDesignId(portalDesign.getDesignId());
            portalDesignManager.setManagerUserId(userId);
            portalDesignManager.setAuthTypeList(createUserAuthList);
            managerLinkList.add(portalDesignManager);
        }

        return managerLinkList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateManagerList(ManagerUpdateRequest managerUpdateRequest) {

        LambdaQueryWrapper<PortalDesignManager> removeWrapper = new LambdaQueryWrapper<>();
        removeWrapper.eq(PortalDesignManager::getDesignId, managerUpdateRequest.getDesignId());
        removeWrapper.in(PortalDesignManager::getManagerUserId, managerUpdateRequest.getManagerUserIdList());
        this.remove(removeWrapper);

        List<PortalDesignManager> managerList = ManagerFactory.createManagerList(managerUpdateRequest);
        this.saveBatch(managerList);
    }

    @Override
    public void del(PortalDesignManagerRequest portalDesignManagerRequest) {
        PortalDesignManager portalDesignManager = this.queryPortalDesignManager(portalDesignManagerRequest);
        this.removeById(portalDesignManager.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(PortalDesignManagerRequest portalDesignManagerRequest) {
        this.removeByIds(portalDesignManagerRequest.getBatchDeleteIdList());
    }

    @Override
    public PageResult<PortalDesignManager> findPage(PortalDesignManagerRequest portalDesignManagerRequest) {
        LambdaQueryWrapper<PortalDesignManager> wrapper = createWrapper(portalDesignManagerRequest);

        wrapper.select(PortalDesignManager::getId, PortalDesignManager::getDesignId, PortalDesignManager::getManagerUserId, PortalDesignManager::getAuthTypeList, BaseEntity::getCreateTime);

        Page<PortalDesignManager> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    private PortalDesignManager queryPortalDesignManager(PortalDesignManagerRequest portalDesignManagerRequest) {
        PortalDesignManager portalDesignManager = this.getById(portalDesignManagerRequest.getId());
        if (ObjectUtil.isEmpty(portalDesignManager)) {
            throw new ServiceException(PortalDesignManagerExceptionEnum.PORTAL_DESIGN_MANAGER_NOT_EXISTED);
        }
        return portalDesignManager;
    }

    private LambdaQueryWrapper<PortalDesignManager> createWrapper(PortalDesignManagerRequest portalDesignManagerRequest) {
        LambdaQueryWrapper<PortalDesignManager> queryWrapper = new LambdaQueryWrapper<>();

        Long designId = portalDesignManagerRequest.getDesignId();
        queryWrapper.eq(PortalDesignManager::getDesignId, designId);

        return queryWrapper;
    }

}
