package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxMergeTimeSlotsService;
import cn.estsh.i3plus.ext.mes.pojo.model.jx.JxMergeTimeSlotsModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
@Service
public class JxMergeTimeSlotsService implements IJxMergeTimeSlotsService {

    @Override
    public List<JxMergeTimeSlotsModel> mergeTimeSlots(List<JxMergeTimeSlotsModel> timeSlots) {
        if (timeSlots.size() == 1) return timeSlots;
        for (int i = 0; i < timeSlots.size(); i++) {
            for (int j = i + 1; j < timeSlots.size(); j++) {
                JxMergeTimeSlotsModel timeSlot1 = timeSlots.get(i);
                JxMergeTimeSlotsModel timeSlot2 = timeSlots.get(j);
                JxMergeTimeSlotsModel mergeSlot = compareToMarge(timeSlot1, timeSlot2);
                if (null != mergeSlot) {
                    timeSlots.remove(timeSlot1);
                    timeSlots.remove(timeSlot2);
                    timeSlots.add(mergeSlot);
                    mergeTimeSlots(timeSlots);
                }
            }
        }
        return timeSlots;
    }

    private JxMergeTimeSlotsModel compareToMarge(JxMergeTimeSlotsModel timeSlot1, JxMergeTimeSlotsModel timeSlot2) {
        if (timeSlot1.getEndDate().before(timeSlot2.getStartDate()) || timeSlot1.getStartDate().after(timeSlot2.getEndDate())) return null;
        else if (!timeSlot1.getStartDate().after(timeSlot2.getStartDate()) && !timeSlot1.getEndDate().before(timeSlot2.getEndDate())) return timeSlot1;
        else if (!timeSlot2.getStartDate().after(timeSlot1.getStartDate()) && !timeSlot2.getEndDate().before(timeSlot1.getEndDate())) return timeSlot2;
        else if (timeSlot1.getStartDate().before(timeSlot2.getStartDate()) && timeSlot1.getEndDate().before(timeSlot2.getEndDate())) return timeSlot1.mergeEnd(timeSlot2.getEndTime(), timeSlot2.getEndDate());
        else if (timeSlot2.getStartDate().before(timeSlot1.getStartDate()) && timeSlot2.getEndDate().before(timeSlot1.getEndDate())) return timeSlot2.mergeEnd(timeSlot1.getEndTime(), timeSlot1.getEndDate());
        else return null;
    }

    public /** static **/ void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<JxMergeTimeSlotsModel> timeSlots = new ArrayList<>();
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 00:00:00", "2023-01-01 01:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 02:00:00", "2023-01-01 03:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 03:00:00", "2023-01-01 04:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 05:00:00", "2023-01-01 06:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 05:10:00", "2023-01-01 05:30:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 07:00:00", "2023-01-01 08:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 07:30:00", "2023-01-01 09:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 10:20:00", "2023-01-01 10:40:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 10:00:00", "2023-01-01 11:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 13:30:00", "2023-01-01 14:30:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 12:00:00", "2023-01-01 14:00:00", simpleDateFormat));
        timeSlots.add(new JxMergeTimeSlotsModel().convertTime("2023-01-01 15:00:00", "2023-01-01 23:00:00", simpleDateFormat));
        timeSlots = mergeTimeSlots(timeSlots);
        for (JxMergeTimeSlotsModel timeSlot : timeSlots) {
            System.out.println("statTime: " + timeSlot.getStartTime() + " , endTime: " + timeSlot.getEndTime());
        }
    }
}