use crate::app::{Action, AppState};
use crate::components::Component;
use crate::errors::ComponentError;
use crate::input::InputEvent;
use crate::services::GitService;
use ratatui::layout::Rect;
use ratatui::Frame;
use std::sync::Arc;

mod input;
mod refresh;
mod render;

pub struct StatusPane {
    git_service: Arc<GitService>,
}

impl StatusPane {
    pub fn new(git_service: Arc<GitService>) -> Self {
        Self { git_service }
    }
}

impl Component for StatusPane {
    fn handle_event(
        &mut self,
        event: InputEvent,
        state: &AppState,
    ) -> Result<Option<Action>, ComponentError> {
        input::handle_event(event, state)
    }

    fn update(
        &mut self,
        action: Action,
        state: &mut AppState,
    ) -> Result<(), ComponentError> {
        match action {
            Action::RefreshStatus | Action::SetRefreshing(true) => {
                refresh::refresh_status(state, &self.git_service)?;
            }
            _ => {}
        }
        Ok(())
    }

    fn render(&mut self, frame: &mut Frame, area: Rect, state: &AppState) {
        render::render_status(frame, area, state);
    }

    fn name(&self) -> &'static str {
        "StatusPane"
    }
}
