package com.base.plan.service;


import com.base.plan.model.dto.StepWindowDTO;
import com.base.plan.utils.DateTool;
import com.base.plan.utils.PropertiesUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 串口冲突判断
 */
@Component
public class WindowsMutexCheckBiz {

    /**
     * 根据唯一标识和前后时间限制获取冲突集合，如果说业务中存在同一个同星同站上传/下发多个地面站的情况需要业务自己处理，这个方法只判断冲突
     *
     * @param type             1为数传站冲突集合  2为卫星冲突集合
     * @param timeMutexDTOList 需要判断冲突的集合
     * @param beforeTimeMutex  前置时间限制，单位秒
     * @param afterTimeMutex   后置时间限制，单位秒
     * @return 每个对象对应的冲突集合
     */
    public Map<String, List<StepWindowDTO>> getMutexList(Integer type, List<StepWindowDTO> timeMutexDTOList, Integer beforeTimeMutex, Integer afterTimeMutex) {
        Map<String, List<StepWindowDTO>> resMap = new HashMap<>();
        if (CollectionUtils.isEmpty(timeMutexDTOList)) {
            return resMap;
        }
        for (int i = 0; i < timeMutexDTOList.size(); i++) {
            List<StepWindowDTO> list = null;
            StepWindowDTO timeMutexDTO = timeMutexDTOList.get(i);
            //  如类型为2则重新赋值前置后置时间
            if (type == 2) {
                beforeTimeMutex = PropertiesUtil.getSatelliteConstraintInfo(timeMutexDTO.getSatelliteId());
                afterTimeMutex = beforeTimeMutex;
            }
            for (int j = 0; j < timeMutexDTOList.size(); j++) {
                StepWindowDTO timeMutexDTOCheck = timeMutexDTOList.get(j);
                if (j == i || !timeMutexDTO.getUniqueKey().equals(timeMutexDTOCheck.getUniqueKey())) {
                    continue;
                }
                if (!contraststrip(timeMutexDTO.getStartTime(), timeMutexDTO.getEndTime(), timeMutexDTOCheck.getStartTime(), timeMutexDTOCheck.getEndTime(), beforeTimeMutex, afterTimeMutex)
                        && timeMutexDTO.getSatelliteId().equals(timeMutexDTOCheck.getSatelliteId())) {
                    if (CollectionUtils.isEmpty(list)) {
                        list = new ArrayList<>();
                        list.add(timeMutexDTO);
                    }
                    list.add(timeMutexDTOCheck);
                    resMap.put(timeMutexDTO.getUniqueKey(), list);
                }
            }
        }
        return resMap;
    }

    /**
     * 卫星冲突判断
     *
     * @param checkBeginTime
     * @param checkEndTime
     * @param checkBeginTime1
     * @param checkEndTime1
     * @param beforeTimeMutex
     * @param afterTimeMutex
     * @return
     */
    private boolean contraststrip(Date checkBeginTime, Date checkEndTime, Date checkBeginTime1, Date checkEndTime1, Integer beforeTimeMutex, Integer afterTimeMutex) {
        //判断交集
        Long executeStartTime = checkBeginTime.getTime();
        Long executeEndTime = checkEndTime.getTime();
        Long finishStartTime = checkBeginTime1.getTime() - beforeTimeMutex * 60 * 1000;
        Long finishEndTime = checkEndTime1.getTime() + afterTimeMutex * 60 * 1000;
        if (DateTool.isIntersectionnew(finishStartTime, finishEndTime, executeStartTime, executeEndTime)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 检查是否有冲突
     *
     * @param checkBeginTime  参照开始时间
     * @param checkEndTime    参照结束时间
     * @param checkBeginTime1 检查的开始时间
     * @param checkEndTime1   检查的结束时间
     * @param beforeTimeMutex 前置条件
     * @param afterTimeMutex  后置条件
     * @return 检查结果 true无冲突，false有冲突
     */
    private boolean check(Date checkBeginTime, Date checkEndTime, Date checkBeginTime1, Date checkEndTime1, Integer beforeTimeMutex, Integer afterTimeMutex) {
        if (((int) (checkBeginTime.getTime() / 1000) - beforeTimeMutex) <= (int) (checkBeginTime1.getTime() / 1000) && ((int) (checkEndTime.getTime() / 1000) + afterTimeMutex) <= (int) (checkEndTime1.getTime() / 1000)) {
            return true;
        } else {
            return false;
        }
    }
}