package com.example.iot_controlhost;

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

class Queue {
    private ExecutorService executorService;
    private BlockingQueue<Runnable> taskQueue;
    private static Queue instance;

    public static Queue getInstance() {
        if (instance == null) {
            synchronized (Queue.class) {
                if (instance == null) {
                    instance = new Queue();
                }
            }
        }
        return instance;
    }

    private Queue() {
        executorService = Executors.newSingleThreadExecutor(); // 使用单线程的ExecutorService
        taskQueue = new PriorityBlockingQueue<>(10, (task1, task2) -> {
            int priority1 = ((PriorityTask) task1).getPriority();
            int priority2 = ((PriorityTask) task2).getPriority();
            return Integer.compare(priority1, priority2);
        });
        // 启动任务执行线程
        startExecution();
    }


    public void addTask(Runnable task, int priority) {
        addTask(task, priority, 1000);
    }

    public void addTask(Runnable task, int priority, long timeoutMillis) {
        taskQueue.offer(new PriorityTask(task, priority));
    }

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

    public void shutdown() {
        executorService.shutdown();
    }

    public static void main(String[] args) {
        System.out.println("Start");

        Queue taskQueueExample = new Queue();

        taskQueueExample.addTask(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("Task 1 (Low Priority) executed.");
            } catch (InterruptedException e) {
            }
        }, 5);

        taskQueueExample.addTask(() -> {
            try {
                Thread.sleep(700);
                System.out.println("Task 2 (Medium Priority) executed.");
            } catch (InterruptedException e) {
            }
        }, 5);

        taskQueueExample.addTask(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("Task 3 (High Priority) executed.");
            } catch (InterruptedException e) {

            }
        }, 2);

        taskQueueExample.addTask(() -> {
            try {
                Thread.sleep(700);
                System.out.println("Task 4 (High Priority) executed.");
            } catch (InterruptedException e) {
            }
        }, 1);

        // 停止任务队列
        taskQueueExample.shutdown();
    }


    private class PriorityTask implements Runnable {
        private Runnable task;
        private int priority;

        public PriorityTask(Runnable task, int priority) {
            this.task = task;
            this.priority = priority;
        }

        @Override
        public void run() {
            task.run();
        }

        public int getPriority() {
            return priority;
        }
    }
}
