use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Constraint, Direction, Layout},
    widgets::Paragraph,
};
use tokio::sync::mpsc::UnboundedSender;

use crate::state::{Action, State};

use super::components::{Component, ComponentRender};

enum ActivePage {
    MainPage,
}

struct Props {
    active_page: ActivePage,
}

impl From<&State> for Props {
    fn from(state: &State) -> Self {
        Self {
            active_page: ActivePage::MainPage,
        }
    }
}

pub struct AppRouter {
    props: Props,
    // ..pages
    action_tx: UnboundedSender<Action>,
}

impl Component for AppRouter {
    fn new(state: &State, action_tx: UnboundedSender<Action>) -> Self
    where
        Self: Sized,
    {
        Self {
            props: Props::from(state),
            action_tx,
        }
        .move_with_state(state)
    }

    fn move_with_state(self, state: &State) -> Self
    where
        Self: Sized,
    {
        Self {
            props: Props::from(state),
            ..self
        }
    }

    fn name(&self) -> &str {
        "Main Screen"
    }

    fn handle_key_event(&mut self, key: KeyEvent) {
        #[allow(clippy::single_match)]
        match key.code {
            KeyCode::Char('q') => {
                let _ = self.action_tx.send(Action::Exit);
            }
            _ => {}
        }
    }
}

impl ComponentRender<()> for AppRouter {
    fn render(&self, frame: &mut ratatui::prelude::Frame, _props: ()) {
        frame.render_widget(Paragraph::new("Press q to exit."), frame.size());
    }
}