//! Implementation of [`TaskManager`]
//!
//! It is only used to manage processes and schedule process based on ready queue.
//! Other CPU process monitoring functions are in Processor.

use alloc::{
    collections::{btree_map::BTreeMap, vec_deque::VecDeque},
    sync::Arc,
};

use lazy_static::*;

use super::{TaskControlBlock, TaskStatus};
use crate::sync::UPSafeCell;
///A array of `TaskControlBlock` that is thread-safe
pub struct TaskManager {
    ready_queue: VecDeque<Arc<TaskControlBlock>>,
    block_queue: VecDeque<Arc<TaskControlBlock>>,

    /// The stopping task, leave a reference so that the kernel stack will not be recycled when switching tasks
    stop_task: Option<Arc<TaskControlBlock>>,
}

/// A simple FIFO scheduler.
impl TaskManager {
    ///Creat an empty TaskManager
    pub fn new() -> Self {
        Self {
            ready_queue: VecDeque::new(),
            block_queue: VecDeque::new(),
            stop_task:   None,
        }
    }
    /// Add process back to ready queue
    pub fn add(&mut self, task: Arc<TaskControlBlock>) {
        self.ready_queue.push_back(task);
    }
    /// add process back to block queue
    pub fn add_block(&mut self, task: Arc<TaskControlBlock>) {
        self.block_queue.push_back(task);
    }
    /// Take a process out of the ready queue
    pub fn fetch(&mut self) -> Option<Arc<TaskControlBlock>> {
        if self.ready_queue.is_empty() {
            return None;
        }
        // let mut min_idx = 0;
        // for (idx, _) in self.ready_queue.iter().enumerate() {
        //     let stride_now = self.ready_queue[idx].inner_exclusive_access(file!(), line!()).stride;
        //     let stride_min = self.ready_queue[min_idx].inner_exclusive_access(file!(), line!()).stride;
        //     if stride_now < stride_min {
        //         min_idx = idx;
        //     }
        // }
        // self.ready_queue.swap(0, min_idx);
        self.ready_queue.pop_front()
    }
    pub fn remove(&mut self, task: Arc<TaskControlBlock>) {
        if let Some((id, _)) = self
            .ready_queue
            .iter()
            .enumerate()
            .find(|(_, t)| Arc::as_ptr(t) == Arc::as_ptr(&task))
        {
            self.ready_queue.remove(id);
        }
    }
    /// Add a task to stopping task
    pub fn add_stop(&mut self, task: Arc<TaskControlBlock>) {
        // NOTE: as the last stopping task has completely stopped (not
        // using kernel stack any more, at least in the single-core
        // case) so that we can simply replace it;
        self.stop_task = Some(task);
    }
}

lazy_static! {
    /// TASK_MANAGER instance through lazy_static!
    pub static ref TASK_MANAGER: UPSafeCell<TaskManager> =
        unsafe { UPSafeCell::new(TaskManager::new()) };
    /// PID2PCB instance (map of pid to pcb)
    pub static ref PID2PCB: UPSafeCell<BTreeMap<usize, Arc<TaskControlBlock>>> =
        unsafe { UPSafeCell::new(BTreeMap::new()) };
}

/// Add a task to ready queue
pub fn add_task(task: Arc<TaskControlBlock>) {
    //trace!("kernel: TaskManager::add_task");
    TASK_MANAGER.exclusive_access(file!(), line!()).add(task);
}

/// Add a task to block queue
pub fn add_block_task(task: Arc<TaskControlBlock>) {
    //trace!("kernel: TaskManager::add_block_task");
    TASK_MANAGER
        .exclusive_access(file!(), line!())
        .add_block(task);
}

/// Wake up a task
pub fn wakeup_task(task: Arc<TaskControlBlock>) {
    trace!("kernel: TaskManager::wakeup_task");
    let mut task_inner = task.inner_exclusive_access(file!(), line!());
    task_inner.task_status = TaskStatus::Ready;
    drop(task_inner);
    add_task(task);
}

/// Remove a task from the ready queue
pub fn remove_task(task: Arc<TaskControlBlock>) {
    //trace!("kernel: TaskManager::remove_task");
    TASK_MANAGER.exclusive_access(file!(), line!()).remove(task);
}

/// Fetch a task out of the ready queue
pub fn fetch_task() -> Option<Arc<TaskControlBlock>> {
    //trace!("kernel: TaskManager::fetch_task");
    TASK_MANAGER.exclusive_access(file!(), line!()).fetch()
}

/// Set a task to stop-wait status, waiting for its kernel stack out of use.
pub fn add_stopping_task(task: Arc<TaskControlBlock>) {
    TASK_MANAGER
        .exclusive_access(file!(), line!())
        .add_stop(task);
}

/// Get process by pid
pub fn pid2process(pid: usize) -> Option<Arc<TaskControlBlock>> {
    let map = PID2PCB.exclusive_access(file!(), line!());
    map.get(&pid).map(Arc::clone)
}

/// Insert item(pid, pcb) into PID2PCB map (called by do_fork AND ProcessControlBlock::new)
pub fn insert_into_pid2process(pid: usize, task: Arc<TaskControlBlock>) {
    PID2PCB.exclusive_access(file!(), line!()).insert(pid, task);
}

/// Remove item(pid, _some_pcb) from PDI2PCB map (called by exit_current_and_run_next)
pub fn remove_from_pid2process(pid: usize) {
    let mut map = PID2PCB.exclusive_access(file!(), line!());
    if map.remove(&pid).is_none() {
        panic!("cannot find pid {} in pid2task!", pid);
    }
}

#[allow(unused)]
pub fn unblock_task(task: Arc<TaskControlBlock>) {
    // println!("[unblock_task] unblock thread");
    let mut task_manager = TASK_MANAGER.exclusive_access(file!(), line!());
    task.inner_exclusive_access(file!(), line!()).task_status = TaskStatus::Ready;
    if let Some((idx, t)) = task_manager
        .block_queue
        .iter()
        .enumerate()
        .find(|(_, t)| Arc::ptr_eq(t, &task))
    {
        task_manager.block_queue.remove(idx);
        task_manager.ready_queue.push_front(task);
    }
}
