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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.portal.modular.front.entity.PortalCalendar;
import cn.stylefeng.team.portal.modular.front.enums.PortalCalendarExceptionEnum;
import cn.stylefeng.team.portal.modular.front.mapper.PortalCalendarMapper;
import cn.stylefeng.team.portal.modular.front.pojo.request.PortalCalendarRequest;
import cn.stylefeng.team.portal.modular.front.service.PortalCalendarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PortalCalendarServiceImpl extends ServiceImpl<PortalCalendarMapper, PortalCalendar> implements PortalCalendarService {

    @Override
    public void add(PortalCalendarRequest portalCalendarRequest) {
        PortalCalendar portalCalendar = new PortalCalendar();

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

        portalCalendar.setTitle(portalCalendarRequest.getTitle());
        portalCalendar.setDescription(portalCalendarRequest.getDescription());
        portalCalendar.setCalendarDate(portalCalendarRequest.getCalendarDate());
        portalCalendar.setStartTime(portalCalendarRequest.getStartTime());
        portalCalendar.setEndTime(portalCalendarRequest.getEndTime());
        portalCalendar.setUrgencyType(portalCalendarRequest.getUrgencyType());

        this.save(portalCalendar);
    }

    @Override
    public void del(PortalCalendarRequest portalCalendarRequest) {
        PortalCalendar portalCalendar = this.queryPortalCalendar(portalCalendarRequest);
        this.removeById(portalCalendar.getId());
    }

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

    @Override
    public void edit(PortalCalendarRequest portalCalendarRequest) {
        PortalCalendar oldCalendar = this.queryPortalCalendar(portalCalendarRequest);

        oldCalendar.setTitle(portalCalendarRequest.getTitle());
        oldCalendar.setDescription(portalCalendarRequest.getDescription());
        oldCalendar.setCalendarDate(portalCalendarRequest.getCalendarDate());
        oldCalendar.setStartTime(portalCalendarRequest.getStartTime());
        oldCalendar.setEndTime(portalCalendarRequest.getEndTime());
        oldCalendar.setUrgencyType(portalCalendarRequest.getUrgencyType());

        this.updateById(oldCalendar);
    }

    @Override
    public PortalCalendar detail(PortalCalendarRequest portalCalendarRequest) {
        return this.queryPortalCalendar(portalCalendarRequest);
    }

    @Override
    public Map<String, List<PortalCalendar>> findList(PortalCalendarRequest portalCalendarRequest) {
        LambdaQueryWrapper<PortalCalendar> wrapper = this.createWrapper(portalCalendarRequest);
        List<PortalCalendar> list = this.list(wrapper);
        if (ObjectUtil.isEmpty(list)) {
            return new HashMap<>();
        }

        return list.stream().collect(
                HashMap::new,
                (map, item) -> map.computeIfAbsent(DateUtil.format(item.getCalendarDate(), "yyyy-MM-dd"), k -> new ArrayList<>()).add(item),
                HashMap::putAll
        );
    }

    private PortalCalendar queryPortalCalendar(PortalCalendarRequest portalCalendarRequest) {
        PortalCalendar portalCalendar = this.getById(portalCalendarRequest.getId());
        if (ObjectUtil.isEmpty(portalCalendar)) {
            throw new ServiceException(PortalCalendarExceptionEnum.PORTAL_CALENDAR_NOT_EXISTED);
        }
        return portalCalendar;
    }

    private LambdaQueryWrapper<PortalCalendar> createWrapper(PortalCalendarRequest portalCalendarRequest) {
        LambdaQueryWrapper<PortalCalendar> queryWrapper = new LambdaQueryWrapper<>();

        if (ObjectUtil.isNotNull(portalCalendarRequest.getUrgencyType())) {
            queryWrapper.eq(PortalCalendar::getUrgencyType, portalCalendarRequest.getUrgencyType());
        }

        Integer year = portalCalendarRequest.getYear();
        Integer month = portalCalendarRequest.getMonth();
        DateTime beginOfMonth = DateUtil.beginOfMonth(DateUtil.parse(year + "-" + month, "yyyy-MM"));
        DateTime endOfMonth = DateUtil.endOfMonth(DateUtil.parse(year + "-" + month, "yyyy-MM"));

        queryWrapper.between(PortalCalendar::getCalendarDate, beginOfMonth, endOfMonth);

        queryWrapper.orderByAsc(PortalCalendar::getCalendarDate);

        return queryWrapper;
    }

}
