use tokio::sync::mpsc;
use yazi_shared::{CompletionToken, Id};

use crate::{TaskIn, TaskProg, hook::HookIn};

#[derive(Debug)]
pub struct Task {
	pub id:          Id,
	pub title:       String,
	pub(crate) prog: TaskProg,
	pub(crate) hook: Option<HookIn>,
	pub done:        CompletionToken,

	pub logs:   String,
	pub logger: Option<mpsc::UnboundedSender<String>>,
}

impl Task {
	pub(super) fn new(id: Id, title: String, prog: TaskProg) -> Self {
		Self {
			id,
			title,
			prog,
			hook: None,
			done: Default::default(),

			logs: Default::default(),
			logger: Default::default(),
		}
	}

	pub(crate) fn log(&mut self, line: String) {
		self.logs.push_str(&line);
		self.logs.push('\n');

		if let Some(logger) = &self.logger {
			logger.send(line).ok();
		}
	}

	pub(super) fn with_hook(&mut self, hook: impl Into<HookIn>) -> &mut Self {
		let mut hook = hook.into();
		hook.set_id(self.id);

		self.hook = Some(hook);
		self
	}
}
