use editor::ACLEditor;
use eframe::egui::{self};
use tree::Tree;

mod editor;
mod tree;

pub struct App {
    tree: Tree,
    editor: ACLEditor,
}

impl App {
    pub fn new(ctx: &egui::Context) -> Self {
        let mut fonts = egui::FontDefinitions::default();
        fonts.font_data.insert(
            "FiraCode".to_owned(),
            egui::FontData::from_static(include_bytes!("../../fonts/FiraCodeNerdFont-Regular.ttf")),
        );

        fonts
            .families
            .entry(egui::FontFamily::Monospace)
            .or_default()
            .insert(0, "FiraCode".to_owned());

        fonts
            .families
            .entry(egui::FontFamily::Proportional)
            .or_default()
            .insert(0, "FiraCode".to_owned());

        ctx.set_fonts(fonts);

        App {
            tree: Tree::new(),
            editor: ACLEditor::new(),
        }
    }
}

impl eframe::App for App {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        egui::SidePanel::right("side_panel")
            .min_width(300.0)
            .max_width(ctx.available_rect().width() - 100.0)
            .resizable(true)
            .show(ctx, |ui| {
                self.editor.show(ui, &self.tree.selected);
            });

        egui::CentralPanel::default().show(ctx, |ui| {
            egui::ScrollArea::both()
                .auto_shrink([false; 2])
                .show(ui, |ui| {
                    self.tree.show(ui);
                });
        });
    }
}
