use taquin_lib::{self, TaquinGame};

use eframe::egui;

fn main() -> eframe::Result {
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };
    eframe::run_native(
        "Taquin Game",
        options,
        Box::new(|cc| Ok(Box::<TaquinGUI>::default())),
    )
}

struct TaquinGUI {
    first_frame: bool,
    taquin_game: TaquinGame,
}

impl Default for TaquinGUI {
    fn default() -> Self {
        Self {
            first_frame: true,
            taquin_game: TaquinGame::new(),
        }
    }
}

impl eframe::App for TaquinGUI {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        if self.first_frame {
            self.first_frame = false;
            self.taquin_game.init_grid();
        }
        egui::CentralPanel::default().show(ctx, |ui| {
            egui::Grid::new("taquin_grid").show(ui, |ui| {
                for row in self.taquin_game.grid() {
                    for column in row {
                        let text = match column {
                            0 => "".to_string(),
                            _ => column.to_string(),
                        };

                        egui::Frame::default().show(ui, |ui| {
                            ui.label(egui::RichText::new(text).strong().size(20.0));
                        });
                    }

                    ui.end_row();
                }
            });

            if ui.button("up").clicked() {
                self.taquin_game.move_to(taquin_lib::Directions::Up);
            };
            if ui.button("down").clicked() {
                self.taquin_game.move_to(taquin_lib::Directions::Down);
            };
            if ui.button("left").clicked() {
                self.taquin_game.move_to(taquin_lib::Directions::Left);
            };
            if ui.button("right").clicked() {
                self.taquin_game.move_to(taquin_lib::Directions::Right);
            };
        });
    }
}
