mod models;
use crate::models::taquin::*;
use left_pad::leftpad;
use std::io::{self, Write};

fn main() {
    let mut taquin_game: TaquinGame = TaquinGame::new();

    taquin_game.init_grid();
    draw_game(taquin_game);
}

fn draw_game(game: TaquinGame) {
    let grid = game.grid();

    for i in 0..ROWS * 2 + 1 {
        for j in 0..COLUMNS * 2 + 1 {
            if i == 0 {
                if j == 0 {
                    print!("╔");
                } else if j == COLUMNS * 2 {
                    print!("╗");
                } else if !is_even(j) {
                    print!("═══");
                } else {
                    print!("╦");
                }
            } else if i == ROWS * 2 {
                if j == 0 {
                    print!("╚");
                } else if j == COLUMNS * 2 {
                    print!("╝");
                } else if !is_even(j) {
                    print!("═══");
                } else {
                    print!("╩");
                }
            } else if !is_even(i) {
                if !is_even(j) {
                    print!("{}", leftpad(grid[i / 2][j / 2].to_string(), 3));
                } else {
                    print!("║");
                }
            } else {
                if j == 0 {
                    print!("╠");
                } else if j == COLUMNS * 2 {
                    print!("╣");
                } else if !is_even(j) {
                    print!("═══");
                } else {
                    print!("╬");
                }
            }
        }

        if i == 0 {
            print!("Score: {}", game.score());
        } else if i == 1 {
            print!("High score: {}", game.high_score());
        }

        let _ = io::stdout().flush();
        println!("");
    }

    println!("Arrows to move");
    println!("Q: Quit");
    println!("R: Restart");
    println!("S: Solve");
}

fn is_even(num: usize) -> bool {
    return num % 2 == 0;
}
