use crate::app::state::{AppState, ProtocolType};
use ratatui::{
    layout::Rect,
    widgets::{Block, Paragraph, Widget},
    style::{Style, Color},
};

pub struct StatusBar<'a> {
    state: &'a AppState,
}

impl<'a> StatusBar<'a> {
    pub fn new(state: &'a AppState) -> Self {
        Self { state }
    }

    fn truncate_text(text: &str, max_width: usize) -> String {
        if max_width == 0 {
            return String::new();
        }
        
        if text.len() <= max_width {
            return text.to_string();
        }
        
        let truncate_at = max_width.saturating_sub(3);
        let truncated: String = text.chars().take(truncate_at).collect();
        format!("{}...", truncated)
    }
}

impl<'a> Widget for StatusBar<'a> {
    fn render(self, area: Rect, buf: &mut ratatui::buffer::Buffer) {
        if area.height == 0 {
            return;
        }

        let protocol_name = match self.state.protocol_type {
            ProtocolType::Http => "HTTP",
            ProtocolType::Grpc => "gRPC",
        };

        let full_text = if self.state.is_loading {
            format!(" [{}] Loading... | {}", protocol_name, self.state.loading_message)
        } else {
            match self.state.protocol_type {
                ProtocolType::Http => {
                    format!(" [{}] q: quit | ?: help | Tab: next | Enter: send | n: new | o: export | g: new gRPC | p: toggle protocol", protocol_name)
                }
                ProtocolType::Grpc => {
                    format!(" [{}] q: quit | ?: help | Tab: next | Enter: send | n: new | o: export | l: load proto | p: toggle protocol", protocol_name)
                }
            }
        };

        let status_text = Self::truncate_text(&full_text, area.width as usize);

        let paragraph = Paragraph::new(status_text)
            .style(Style::default()
                .bg(Color::DarkGray)
                .fg(Color::White))
            .block(Block::default());

        Widget::render(paragraph, area, buf);
    }
}