package com.example.iot_controlhost.utils;

import com.example.iot_controlhost.base.BaseQueue;
import com.example.iot_controlhost.model.thread.QueueIOTask;
import com.example.iot_controlhost.model.thread.QueueIOUITask;
import com.example.iot_controlhost.model.controller.Controller;

import java.util.Comparator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

import android_serialport_api.SerialPortUtil;

/**
 * 单线程队列
 * 用于控制器与传感器执行指令用
 */
public class MyQueue {
    private ExecutorService executorService;
    private BlockingQueue<BaseQueue> taskQueue;
    private static MyQueue controllerInstance;
    private static MyQueue sensorInstance;

    /**
     * 串口通信类别-控制器
     */
    public final static String TYPE_CONTROLLER = "TYPE_CONTROLLER";
    /**
     * 串口通信类别-传感器
     */
    public final static String TYPE_SENSOR = "TYPE_SENSOR";

    public static <T> void start(String type, QueueIOUITask<T> queueTask) {
        MyQueue myQueue;
        if (TYPE_CONTROLLER.equals(type) || Controller.isIntegratedController()) {
            // 控制器 | 集成控制器
            if (controllerInstance == null) {
                synchronized (MyQueue.class) {
                    if (controllerInstance == null) {
                        controllerInstance = new MyQueue();
                    }
                }
            }
            myQueue = controllerInstance;
        } else {
            // 传感器
            if (sensorInstance == null) {
                synchronized (MyQueue.class) {
                    if (sensorInstance == null) {
                        sensorInstance = new MyQueue();
                    }
                }
            }
            myQueue = sensorInstance;
        }
        myQueue.taskQueue.offer(queueTask);
    }

    public static MyQueue getInstance(String type) {
        if (TYPE_CONTROLLER.equals(type)) {
            // 控制器
            if (controllerInstance == null) {
                synchronized (MyQueue.class) {
                    if (controllerInstance == null) {
                        controllerInstance = new MyQueue();
                    }
                }
            }
            return controllerInstance;
        } else {
            // 传感器
            if (sensorInstance == null) {
                synchronized (SerialPortUtil.class) {
                    if (sensorInstance == null) {
                        sensorInstance = new MyQueue();
                    }
                }
            }
            return sensorInstance;
        }
    }

    public void addTask(QueueIOTask queueIOTask) {
        taskQueue.offer(queueIOTask);
    }

    private MyQueue() {
        // 使用单线程的ExecutorService
        executorService = Executors.newSingleThreadExecutor();
        taskQueue = new PriorityBlockingQueue<>(10, Comparator.comparingInt(BaseQueue::getPriority));
        // 启动任务执行线程
        startExecution();
    }

    public void startExecution() {
        executorService.execute(() -> {
            while (true) {
                try {
                    // 取出队列中优先级最高的任务
                    Runnable task = taskQueue.take();
                    task.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        });
    }

    public void shutdown() {
        if (executorService != null) {
            executorService.shutdown();
        }
    }

}

