package com.gci.boot.modules.test;


import cn.hutool.core.collection.CollectionUtil;
import com.gci.boot.modules.gis.mapper.TbGisRunningbusMapper;
import com.gci.boot.modules.vehicles.service.ITbViolationRecordService;
import com.gci.boot.modules.vehicles.vo.MsgImportFailVO;
import com.gci.boot.modules.vehicles.vo.RepairImportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private TbGisRunningbusMapper runningbusMapper;
    @Autowired
    private ITbViolationRecordService violationRecordService;

    /**
     * 添加
     */
    @PostMapping(value = "/tet")
    public Object tet(TestVO vo) {
//        List<String> msgList = new ArrayList<>();
//        msgList.add("【成都交警】川AAP8022于2023-10-30在瑞星路与琼楼路交叉路口实施机动车通过有灯控路口时，不按所需行进方向驶入导向车道的违法行为，请三十日内到交警部门接受处理。该车有18笔违法记录未处理，请及时处理。(更多请关注蓉E行)");
//        msgList.add("【四川交警】您的小型新能源汽车川A08668D于2023-11-07 13:10在成都市二环路西二段与战旗东路交叉路口，被交通技术监控设备记录了『不按导向车道行驶』的违法行为。请于收到本告知之日起30日内接受处理。");
//        msgList.add("【四川交警】您的小型新能源汽车川AAP0633于2023-08-19 09:10在成都市二环路西二段与战旗东路交叉路口，被交通技术监控设备记录了『不按导向车道行驶』的违法行为。请于收到本告知之日起30日内接受处理。");
//        msgList.add("【成都交警】川AAP8022于2023-10-30在瑞星路与琼楼路交叉路口实施机动车通过有灯控路口时，不按所需行进方向驶入导向车道的违法行为，请三十日内到交警部门接受处理。该车有18笔违法记录未处理，请及时处理。(更多请关注蓉E行)");
//        List<MsgImportFailVO> list =  violationRecordService.importViolationMessage(msgList);

        RepairImportVO v = new RepairImportVO();
        v.setPhone("12345678900");
        v.setRepairTime("2033-09-09 11:30");
        v.setQuantity("d7");
        v.validate();
        System.out.println(v);
        return v;
    }


    public static class TrafficJamSection {
        public Date startTime;
        public Date endTime;

        public TrafficJamSection(Date startTime, Date endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }

    public static int calculateTotalMinutes(List<TrafficJamSection> trafficJamList) {
        int totalMinutes = 0;
        List<TrafficJamSection> newTrafficJamList = new ArrayList<>();

        for (TrafficJamSection currentSection : trafficJamList) {
            boolean isOverlapped = false;

            for (TrafficJamSection existingSection : newTrafficJamList) {
                if (isTimeOverlapping(currentSection, existingSection)) {
                    isOverlapped = true;

                    if (currentSection.startTime.compareTo(existingSection.startTime) < 0) {
                        existingSection.startTime = currentSection.startTime;
                    }

                    if (currentSection.endTime.compareTo(existingSection.endTime) > 0) {
                        existingSection.endTime = currentSection.endTime;
                    }

                    break;
                }
            }

            if (!isOverlapped) {
                newTrafficJamList.add(currentSection);
            }
        }

        for (TrafficJamSection section : newTrafficJamList) {
            totalMinutes += calculateMinutes(section.startTime, section.endTime);
        }

        return totalMinutes;
    }

    private static boolean isTimeOverlapping(TrafficJamSection section1, TrafficJamSection section2) {
        return section1.startTime.before(section2.endTime) && section1.endTime.after(section2.startTime);
    }

    private static int calculateMinutes(Date startTime, Date endTime) {
        long durationMillis = endTime.getTime() - startTime.getTime();
        return (int) (durationMillis / (1000 * 60));
    }

    public static void main(String[] args) {
        List<TrafficJamSection> trafficJamList = new ArrayList<>();
        trafficJamList.add(new TrafficJamSection(new Date(2023, 8, 25, 10, 30), new Date(2023, 8, 25, 12, 30)));
        trafficJamList.add(new TrafficJamSection(new Date(2023, 8, 25, 9, 30), new Date(2023, 8, 25, 11, 30)));
        trafficJamList.add(new TrafficJamSection(new Date(2023, 8, 25, 7, 0), new Date(2023, 8, 26, 8, 0)));
        trafficJamList.add(new TrafficJamSection(new Date(2023, 8, 25, 9, 45), new Date(2023, 8, 25, 10, 43)));

        int totalMinutes = calculateTotalMinutes(trafficJamList);
        System.out.println("Total minutes: " + totalMinutes);
    }
}
