use std::{
    io,
    os::{
        fd::{AsRawFd, RawFd},
        yggdrasil::io::poll::PollChannel,
    },
    time::Duration,
};

use crate::sys::Poll;

pub struct PollImpl(PollChannel);

impl Poll for PollImpl {
    fn new() -> io::Result<Self> {
        PollChannel::new().map(Self)
    }

    fn add<F: AsRawFd>(&mut self, interest: &F) -> io::Result<()> {
        self.0.add(interest.as_raw_fd())
    }

    fn remove<F: AsRawFd>(&mut self, interest: &F) -> io::Result<()> {
        self.0.remove(interest.as_raw_fd())
    }

    fn wait(&mut self, timeout: Option<Duration>) -> io::Result<Option<RawFd>> {
        match self.0.wait(timeout, true)? {
            Some((fd, Ok(_))) => Ok(Some(fd)),
            Some((_, Err(e))) => Err(e),
            None => Ok(None),
        }
    }
}

impl AsRawFd for PollImpl {
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}
