package com.youchain.modules.quartz.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.repository.AgvTaskRepository;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import com.youchain.utils.HttpPostUtil;
import com.youchain.utils.UrlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 料箱出库任务下发
 */
@Slf4j
@Service
public class pickTask {

    @Autowired
    public AgvTaskRepository agvTaskRepository;

    @Autowired
    public AgvTaskService agvTaskService;

    private Map<String, List<Integer>> groupedTasks = new HashMap<>();

    private long lastTaskTime = System.currentTimeMillis();

    public void pickTask() {
        //按线边分组 下发出库任务
        List<Object> objectList = agvTaskRepository.queryGroupAgvTask();
        for (Object obj : objectList) {
            List<AgvTask> agvTaskList = agvTaskRepository.queryLineAgvTask(obj.toString());
            for (AgvTask agvTask : agvTaskList) {
                List<Integer> agvTaskList1 = groupedTasks.get(obj.toString());
                if (agvTaskList1 == null) {
                    groupedTasks.computeIfAbsent(obj.toString(), k -> new CopyOnWriteArrayList<>()).add(agvTask.getId());
                } else if (agvTaskList1.size() <= 4) {
                    boolean containsElement = agvTaskList1.contains(agvTask.getId());
                    if (!containsElement) {
                        groupedTasks.computeIfAbsent(obj.toString(), k -> new CopyOnWriteArrayList<>()).add(agvTask.getId());
                    }
                }
            }
            // 检查是否满足四个为一组或者时间超过五分钟
            if ((isGroupingConditionsMet() || timeExceeded()) && isLine() ) {
                issueGroupedTasks();
                groupedTasks.clear();
                lastTaskTime = System.currentTimeMillis();
            }else{
                log.info("小件出库料箱任务未满足条件!");
            }
        }


    }

    private boolean isLine() {
        // 判断缓存输送线上的料箱总数+AGV执行中的数量 不能超过6个
        int queryCount = agvTaskService.QueryCount("6x1");

        //出库料箱AGV执行中的数量
        //List<AgvTask> agvTaskList= agvTaskService.findByAgvList(BizStatus.PICK,"PICKER_MOVE");
        if(queryCount<=6){
            return true;
        }
        return false;
    }

    private boolean isGroupingConditionsMet() {
        // 判断是否满足四个为一组的条件
        return groupedTasks.values().stream().anyMatch(tasks -> tasks.size() == 4);
    }

    private boolean timeExceeded() {
        // 判断时间是否超过3分钟
        return System.currentTimeMillis() - lastTaskTime >= 2 * 60 * 1000;
    }

    private void issueGroupedTasks() {
        // 实现任务下发的逻辑
        // 可以遍历groupedTasks，处理每个分组的任务
        for (List<Integer> tasksInGroup : groupedTasks.values()) {
           List<AgvTask> agvTaskList= agvTaskService.findById(tasksInGroup);
            String rul=agvTaskService.sendAgvTaskLXImpl(agvTaskList);
            System.out.println(rul);
        }



    }
}
