use crate::AppState;
use crate::EventEnum;
use crate::Focus;
use cocotte::SubApp;
use cocotte::ratatui::Frame;
use cocotte::ratatui::layout::Constraint;
use cocotte::ratatui::layout::Rect;
use cocotte::ratatui::text::{Line, Span};
use cocotte::ratatui::widgets::{Block, Borders, Paragraph};

pub struct Input;

impl Input {
    pub fn new() -> Input {
        Input
    }
}

impl SubApp<EventEnum, AppState> for Input {
    fn constraints(&self) -> Constraint {
        Constraint::Length(3)
    }

    fn handle_input(&mut self, _event: &mut EventEnum, _app_state: &mut AppState) {}

    fn render(&self, frame: &mut Frame, area: Rect, app_state: &mut AppState) {
        let cwd = app_state.current_directory.to_string_lossy().to_string();

        let filter = match app_state.focus {
            Focus::Directories => &app_state.directory_filter,
            Focus::Files => &app_state.file_filter,
        };

        let displayed = if filter.is_empty() {
            vec![Span::raw(cwd)]
        } else {
            vec![
                Span::raw(cwd),
                Span::raw("  |  "),
                Span::raw(filter.as_str()),
            ]
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .title("Working directory");
        let input = Paragraph::new(Line::from(displayed)).block(block);
        frame.render_widget(input, area);
    }
}

impl Default for Input {
    fn default() -> Self {
        Self::new()
    }
}
