package cn.superdesk.uniorder.common.queue.block;

import cn.hutool.core.thread.ExecutorBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 消费者线程
 * @Author: linjinyu
 * @CreateDate: 2021/9/10 下午1:36
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/10 下午1:36
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class TaskExecutor {

	private final String TAG = "ShowTaskExecutor";

	private BlockTaskQueue taskQueue;
//	private TaskHandler    mTaskHandler;

	private boolean isRunning = true;

	private static final int MSG_EVENT_DO     = 0;
	private static final int MSG_EVENT_FINISH = 1;

	public TaskExecutor(BlockTaskQueue taskQueue) {
		this.taskQueue = taskQueue;
//		this.mTaskHandler = new TaskHandler();
	}

	// 开始遍历任务队列
	public void start() {

//		AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
//			// run 方法
//		});
		ExecutorService executor = ExecutorBuilder.create()//
				.setCorePoolSize(1)//
				.setMaxPoolSize(1)//
				.setKeepAliveTime(0)//
				.build();
		executor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					// 死循环
					while (isRunning) {

						TaskInterface iTask;


						// 取任务
						iTask = taskQueue.take();

						log.info("取任务 {}", iTask);

						if (iTask != null) {

							// 执行任务
							TaskEvent doEvent = new TaskEvent();
							doEvent.setTask(iTask);
							doEvent.setEventType(TaskEvent.EventType.DO);
//					mTaskHandler
//							.obtainMessage(MSG_EVENT_DO, doEvent)
//							.sendToTarget();

							iTask.doTask();

							// 一直阻塞,直到任务执行完
							if (iTask.getDuration() != 0) {
								TimeUnit.MICROSECONDS.sleep(iTask.getDuration());
							} else {
								iTask.blockTask();
							}

							// 完成任务
							TaskEvent finishEvent = new TaskEvent();
							finishEvent.setTask(iTask);
							finishEvent.setEventType(TaskEvent.EventType.FINISH);

							iTask.finishTask();

//					mTaskHandler
//							.obtainMessage(MSG_EVENT_FINISH, finishEvent)
//							.sendToTarget();
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});


	}


	// 根据不同的消息回调不同的方法。
//	private static class TaskHandler {
//
//		TaskHandler() {
//			super(Looper.getMainLooper());
//		}
//
//		@Override
//		public void handleMessage(Message msg) {
//
//			super.handleMessage(msg);
//
//			TaskEvent taskEvent = (TaskEvent) msg.obj;
//
//			if (msg.what == MSG_EVENT_DO && taskEvent.getEventType() == TaskEvent.EventType.DO) {
//				taskEvent.getTask().doTask();
//			}
//
//			if (msg.what == MSG_EVENT_FINISH && taskEvent.getEventType() == TaskEvent.EventType.FINISH) {
//				taskEvent.getTask().finishTask();
//			}
//		}
//	}

	public void startRunning() {
		isRunning = true;
	}

	public void pauseRunning() {
		isRunning = false;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void resetExecutor() {
		isRunning = true;
		taskQueue.clear();
	}

	public void clearExecutor() {
		pauseRunning();
		taskQueue.clear();
	}
}
