use std::{
    cell::Cell,
    collections::BTreeMap,
    path::PathBuf,
    process::{Child, ExitStatus},
};

use mio::{event, Events, Poll};
use slab::Slab;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Pid {
    inner: u32,
}

impl Display for Pid {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

#[derive(Debug)]
pub struct ChildInfo {
    child: Child,
    prog_path: PathBuf,
    exit_status: Cell<Option<ExitStatus>>,
}

#[derive(Debug)]
pub enum Fd {
    // ChildOut(ChildOut),
    // Wait(WaitStream),
}

#[derive(Debug)]
enum State {
    Awaiting,
    // Draining(ChildOut),
    // DrainingSignal(SignalSource),
    // DrainingWait(WaitStream),
}

pub struct Muxer {
    poll: Poll,
    events: Events,
    children: BTreeMap<Pid, ChildInfo>,
    fds: Slab<Fd>,
    state: State,
    // We don't need this field, an index into "events" would do, but
    // it only exposes an iterator over references, and self
    // referencial structs are a pain
    pending_events: Vec<event::Event>,
}
