package com.izouma.wenlvju.service;

import cn.hutool.core.collection.CollUtil;
import com.izouma.wenlvju.domain.Performance;
import com.izouma.wenlvju.domain.PerformanceApply;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.enums.ApplyStatus;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.PerformanceApplyRepo;
import com.izouma.wenlvju.repo.PerformanceRepo;
import com.izouma.wenlvju.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class PerformanceApplyService {

    private PerformanceApplyRepo performanceApplyRepo;
    private PerformanceRepo      performanceRepo;

    public Page<PerformanceApply> all(PageQuery pageQuery) {
        return performanceApplyRepo.findAll(JpaUtils.toSpecification(pageQuery, PerformanceApply.class), JpaUtils.toPageRequest(pageQuery));
    }

    public void audit(Long id, Boolean pass) {
        PerformanceApply apply = performanceApplyRepo.findById(id).orElseThrow(new BusinessException("无此展演申请"));
        if (pass) {
            apply.setStatus(ApplyStatus.PASS);
        } else {
            apply.setStatus(ApplyStatus.DENY);
        }
        performanceApplyRepo.save(apply);
    }

    /*
    自动编排
     */
    public void autoArrangement(Long performanceId) {
        List<PerformanceApply> applyList =
                performanceApplyRepo.findAllByStatusAndPerformanceId(ApplyStatus.PASS, performanceId);

        List<PerformanceApply> showTime = applyList.stream()
                .filter(apply -> apply.getShowTime() != null)
                .sorted((a, b) -> b.getShowTime().compareTo(a.getShowTime()))
                .collect(Collectors.toList());
        List<PerformanceApply> showTimeNull = applyList.stream()
                .filter(apply -> apply.getShowTime() == null)
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(showTimeNull)) {
            return;
        }

        if (CollUtil.isEmpty(showTime)) {
            Performance performance = performanceRepo.findById(performanceId).orElseThrow(new BusinessException("无记录"));
            final LocalDateTime[] time = {LocalDateTime.of(performance.getStartTime().plusDays(3), LocalTime.now())};
            showTimeNull.forEach(apply -> {
                apply.setShowTime(time[0]);
                performanceApplyRepo.save(apply);
                time[0] = time[0].plusMinutes(10);
            });
            return;
        }
        final LocalDateTime[] time = {showTime.get(0).getShowTime().plusMinutes(10)};
        showTimeNull.forEach(apply -> {
            apply.setShowTime(time[0]);
            performanceApplyRepo.save(apply);
            time[0] = time[0].plusMinutes(10);
        });
    }

    /*
    自动评奖
     */
    public void autoAwards(Long performanceId) {
        List<PerformanceApply> applies = performanceApplyRepo.findAllByShowTimeIsNotNullAndPerformanceId(performanceId);
        if (CollUtil.isEmpty(applies)) {
            return;
        }
        applies.forEach(apply -> {
            apply.setAwards("一等奖");
            apply.setScore(100);
            performanceApplyRepo.save(apply);
        });
    }

}
