/*
 * Copyright (c) 2013-2025 Hutool Team and hutool.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.hutool.v7.cron;

import cn.hutool.v7.core.map.TripleTable;
import cn.hutool.v7.core.text.StrUtil;
import cn.hutool.v7.cron.pattern.CronPattern;
import cn.hutool.v7.cron.task.Task;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 定时任务表<br>
 * 任务表将ID、表达式、任务一一对应<br>
 * 任务的添加、移除使用读写锁保证线程安全性
 *
 * @author Looly
 */
public abstract class TaskTable implements Serializable {
	@Serial
	private static final long serialVersionUID = 1L;

	/**
	 * 默认任务表大小：256
	 */
	public static final int DEFAULT_CAPACITY = 256;

	/**
	 * 读写锁，保证线程安全
	 */
	final ReadWriteLock lock;

	/**
	 * 任务表，ID、表达式、任务一一对应。使用TripleTable存储，便于快速查找和更新<br>
	 */
	final TripleTable<String, CronPattern, Task> table;

	/**
	 * 构造
	 */
	public TaskTable() {
		this(DEFAULT_CAPACITY);
	}

	/**
	 * 构造
	 *
	 * @param initialCapacity 容量，即预估的最大任务数
	 */
	public TaskTable(final int initialCapacity) {
		lock = new ReentrantReadWriteLock();
		this.table = new TripleTable<>(initialCapacity);
	}

	/**
	 * 任务表大小，加入的任务数
	 *
	 * @return 任务表大小，加入的任务数
	 * @since 4.0.2
	 */
	public int size() {
		return this.table.size();
	}

	/**
	 * 任务表是否为空
	 *
	 * @return true为空
	 * @since 4.0.2
	 */
	public boolean isEmpty() {
		return size() < 1;
	}

	/**
	 * 获取所有ID，返回不可变列表，即列表不可修改
	 *
	 * @return ID列表
	 * @since 4.6.7
	 */
	public List<String> getIds() {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return this.table.getLefts();
		} finally {
			readLock.unlock();
		}
	}

	// region ----- getTask

	/**
	 * 获取所有定时任务，返回不可变列表，即列表不可修改
	 *
	 * @return 定时任务列表
	 * @since 4.6.7
	 */
	public List<Task> getTasks() {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return this.table.getRights();
		} finally {
			readLock.unlock();
		}
	}

	/**
	 * 获得指定位置的{@link Task}
	 *
	 * @param index 位置
	 * @return {@link Task}
	 * @since 3.1.1
	 */
	public Task getTask(final int index) {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return this.table.getRight(index);
		} finally {
			readLock.unlock();
		}
	}

	/**
	 * 获得指定id的{@link Task}
	 *
	 * @param id ID
	 * @return {@link Task}
	 * @since 3.1.1
	 */
	public Task getTask(final String id) {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return table.getRightByLeft(id);
		} finally {
			readLock.unlock();
		}
	}
	// endregion

	// region ----- getPattern

	/**
	 * 获取所有定时任务表达式，返回不可变列表，即列表不可修改
	 *
	 * @return 定时任务表达式列表
	 * @since 4.6.7
	 */
	public List<CronPattern> getPatterns() {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return this.table.getMiddles();
		} finally {
			readLock.unlock();
		}
	}

	/**
	 * 获得指定id的{@link CronPattern}
	 *
	 * @param id ID
	 * @return {@link CronPattern}
	 * @since 3.1.1
	 */
	public CronPattern getPattern(final String id) {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return table.getMiddleByLeft(id);
		} finally {
			readLock.unlock();
		}
	}

	/**
	 * 获得指定位置的{@link CronPattern}
	 *
	 * @param index 位置
	 * @return {@link CronPattern}
	 * @since 3.1.1
	 */
	public CronPattern getPattern(final int index) {
		final Lock readLock = lock.readLock();
		readLock.lock();
		try {
			return table.getMiddle(index);
		} finally {
			readLock.unlock();
		}
	}
	// endregion

	/**
	 * 新增Task
	 *
	 * @param id      ID
	 * @param pattern {@link CronPattern}
	 * @param task    {@link Task}
	 * @return this
	 */
	public TaskTable add(final String id, final CronPattern pattern, final Task task) {
		final Lock writeLock = lock.writeLock();
		writeLock.lock();
		try {
			if (this.table.containLeft(id)) {
				throw new CronException("Id [{}] has been existed!", id);
			}
			this.table.put(id, pattern, task);
		} finally {
			writeLock.unlock();
		}
		return this;
	}

	/**
	 * 移除Task
	 *
	 * @param id Task的ID
	 * @return 是否成功移除，{@code false}表示未找到对应ID的任务
	 */
	public boolean remove(final String id) {
		final Lock writeLock = lock.writeLock();
		writeLock.lock();
		try {
			final int index = this.table.indexOfLeft(id);
			if (index > -1) {
				this.table.remove(index);
				return true;
			}
		} finally {
			writeLock.unlock();
		}
		return false;
	}

	/**
	 * 更新某个Task的定时规则
	 *
	 * @param id      Task的ID
	 * @param pattern 新的表达式
	 * @return 是否更新成功，如果id对应的规则不存在则不更新
	 * @since 4.0.10
	 */
	public boolean updatePattern(final String id, final CronPattern pattern) {
		final Lock writeLock = lock.writeLock();
		writeLock.lock();
		try {
			final int index = this.table.indexOfLeft(id);
			if (index > -1) {
				this.table.setMiddle(index, pattern);
				return true;
			}
		} finally {
			writeLock.unlock();
		}
		return false;
	}

	@Override
	public String toString() {
		final int size = this.size();
		final StringBuilder builder = StrUtil.builder();
		for (int i = 0; i < size; i++) {
			builder.append(StrUtil.format("[{}] [{}] [{}]\n",
				this.table.getLeft(i), this.table.getMiddle(i), this.table.getRight(i)));
		}
		return builder.toString();
	}

	/**
	 * 根据给定的时间戳匹配任务，如果匹配成功则使用调度器执行相应的Task
	 *
	 * @param scheduler 调度器
	 * @param millis    时间戳
	 */
	public abstract void execute(final Scheduler scheduler, final long millis);
}
