use std::ops::{Deref, DerefMut};

use crate::utils::TILE_WIDTH;

pub struct Map {
    pub value: Vec<Vec<i32>>,
    pub is_final: bool,
    level: u32,
}
pub const MAX_HEIGHT: f32 = 11.0 * TILE_WIDTH;
pub const MAX_WIDTH: f32 = 13.0 * TILE_WIDTH;
impl Map {
    pub fn first() -> Self {
        let room_map = vec![
            vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
            vec![1, 0, 2, 2, 0, 4, 7, 0, 2, 0, 0, 7, 1],
            vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 0, 9, 0, 1],
            vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 1],
            vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 0, 5, 8, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 9, 1],
            vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 2, 2, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1],
            vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
            vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
        ];
        Self {
            value: room_map,
            is_final: false,
            level: 1,
        }
    }
    pub fn init(&mut self) {
        self.value = vec![
            vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
            vec![1, 0, 2, 2, 0, 4, 7, 0, 2, 0, 0, 7, 1],
            vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 0, 9, 0, 1],
            vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 1],
            vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 0, 5, 8, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 9, 1],
            vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 2, 2, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1],
            vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
            vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
        ];
        self.is_final = false;
        self.level = 1;
    }
    pub fn next(&mut self) {
        match self.level {
            1 => {
                self.level = 2;
                self.value = vec![
                    vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
                    vec![1, 0, 2, 2, 0, 4, 0, 0, 2, 0, 0, 7, 1],
                    vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 7, 1, 0, 1],
                    vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 7, 1, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1],
                    vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 7, 5, 8, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 9, 1],
                    vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 7, 2, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 4, 1, 1],
                    vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
                    vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
                ];
            }
            2 => {
                self.level = 3;
                self.value = vec![
                    vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
                    vec![1, 0, 2, 2, 0, 4, 0, 0, 2, 0, 0, 7, 1],
                    vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 7, 1, 0, 1],
                    vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 7, 1, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1],
                    vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 7, 5, 8, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 9, 1],
                    vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 7, 2, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 4, 1, 1],
                    vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
                    vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
                ];
            }
            3 => {
                self.level = 4;
                self.value = vec![
                    vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
                    vec![1, 0, 2, 2, 0, 4, 0, 0, 2, 0, 0, 7, 1],
                    vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 7, 1, 0, 1],
                    vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 7, 1, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 1],
                    vec![1, 0, 0, 0, 0, 0, 6, 0, 0, 7, 5, 8, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 9, 1],
                    vec![1, 0, 0, 6, 0, 6, 0, 0, 0, 7, 2, 0, 1],
                    vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 7, 4, 1, 1],
                    vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
                    vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
                ];
                self.is_final = true;
            }
            _ => self.random(),
        }
    }
    pub fn random(&mut self) {
        //TODO:Real random
        let room_map = vec![
            vec![1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1],
            vec![1, 0, 2, 2, 0, 4, 0, 0, 2, 0, 0, 7, 1],
            vec![1, 0, 9, 0, 9, 4, 9, 0, 9, 0, 1, 0, 1],
            vec![1, 0, 0, 4, 0, 0, 0, 0, 0, 7, 1, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 0, 1],
            vec![1, 0, 0, 0, 7, 0, 6, 0, 0, 0, 5, 8, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 9, 9, 1],
            vec![1, 0, 0, 6, 0, 6, 0, 7, 0, 7, 2, 0, 1],
            vec![1, 0, 9, 0, 9, 0, 9, 0, 9, 0, 4, 1, 1],
            vec![1, 3, 8, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1],
            vec![9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
        ];
        self.value = room_map;
        self.level += 1;
    }
}
impl Deref for Map {
    type Target = Vec<Vec<i32>>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for Map {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}
