use eframe::egui;
use crate::ui::theme::Theme;

pub struct Channel {
    pub name: &'static str,
    pub children: &'static [Channel],
}

pub const CHANNEL_TREE: &[Channel] = &[
    Channel {
        name: "General",
        children: &[
            Channel { name: "Lobby", children: &[] },
            Channel { name: "Meeting Room", children: &[] },
        ],
    },
    Channel {
        name: "Gaming",
        children: &[
            Channel { name: "Competitive", children: &[] },
            Channel { name: "Casual", children: &[] },
        ],
    },
    Channel { name: "AFK", children: &[] },
];

pub struct ChannelTreeView {
    pub selected_channel: String,
}

impl ChannelTreeView {
    pub fn new() -> Self {
        Self {
            selected_channel: "Lobby".to_string(),
        }
    }

    pub fn show(&mut self, ui: &mut egui::Ui) {
        ui.vertical(|ui| {
            ui.add_space(8.0);
            ui.horizontal(|ui| {
                ui.label(egui::RichText::new("✦").size(20.0).color(Theme::ACCENT_VIBRANT));
                ui.label(
                    egui::RichText::new("SERVER BEYOND")
                        .size(14.0)
                        .strong()
                        .extra_letter_spacing(1.2)
                        .color(Theme::TEXT_PRIMARY),
                );
            });
            ui.add_space(12.0);
            
            egui::ScrollArea::vertical()
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    for channel in CHANNEL_TREE {
                        self.draw_channel_entry(ui, channel, 0);
                    }
                });
        });
    }

    fn draw_channel_entry(&mut self, ui: &mut egui::Ui, channel: &Channel, depth: usize) {
        let indent = depth as f32 * 18.0;
        let is_selected = self.selected_channel == channel.name;
        let has_children = !channel.children.is_empty();

        ui.horizontal(|ui| {
            ui.add_space(indent);

            let icon = if has_children { "📁" } else { "🔊" };
            
            let text_color = if is_selected { Theme::ACCENT } else { Theme::TEXT_PRIMARY };
            let bg_color = if is_selected { Theme::ACCENT.linear_multiply(0.1) } else { egui::Color32::TRANSPARENT };

            let frame = egui::Frame::NONE
                .inner_margin(egui::Margin::symmetric(8, 4))
                .corner_radius(6.0)
                .fill(bg_color);

            let response = frame.show(ui, |ui| {
                ui.horizontal(|ui| {
                    ui.label(egui::RichText::new(icon).size(14.0).color(text_color));
                    ui.add_space(4.0);
                    ui.label(egui::RichText::new(channel.name).size(13.0).color(text_color));
                })
            }).response;

            let response = ui.interact(response.rect, ui.id().with(channel.name), egui::Sense::click());
            
            if response.clicked() {
                self.selected_channel = channel.name.to_string();
            }
            
            if response.hovered() && !is_selected {
                ui.painter().rect_filled(response.rect, 6.0, egui::Color32::from_white_alpha(5));
            }
        });

        if has_children {
            for child in channel.children {
                self.draw_channel_entry(ui, child, depth + 1);
            }
        }
    }
}
