use image::{ImageBuffer,Rgba,RgbaImage};
use local_types::to_u16;
use std::io::Read;
use section_size_reader;
use read_n;

const TILE_HEADER_LENGTH_IN_BYTES: u32 = 4;
const TILE_PIXEL_SIZE_IN_BYTES: u32 = 1;
const TILE_HEIGHT: u32 = 32;
const TILE_WIDTH: u32 = 32;

const TILE_SIZE_IN_BYTES: u32 = TILE_HEIGHT*TILE_WIDTH*TILE_PIXEL_SIZE_IN_BYTES;
const TILE_TOTAL_SIZE_IN_BYTES: u32 = TILE_SIZE_IN_BYTES+TILE_HEADER_LENGTH_IN_BYTES;

// pub struct TileFlags {
//     game_object: bool,
// }

// pub struct Tile {
//     bmp: Vec<u8>,
//     header: TileFlags,
// }

pub fn parse_section <R> (mut reader: R)
where
    R: Read,
{
    let section_size = section_size_reader(&mut reader);

    let total_tiles = section_size / TILE_TOTAL_SIZE_IN_BYTES;

    for i in 0..total_tiles {
        // println!("    Tile {}", i);
        let tile_data = read_tile_data_section(&mut reader);
        let mut file_name: String = "./tiles/tile_".to_owned();
        file_name.push_str(&i.to_string());
        file_name.push_str(".png");
        tile_data.save(file_name).unwrap();
    }
}

fn read_tile_data_section <R> (mut reader: R) -> RgbaImage
where
    R: Read,
{
    let palette = get_palette();
    let _header_raw = read_n(&mut reader, TILE_HEADER_LENGTH_IN_BYTES);
    let mut bmp = ImageBuffer::new(TILE_HEIGHT, TILE_WIDTH);

    for i in 0..TILE_SIZE_IN_BYTES {
        let pixel_data = read_n(&mut reader, 1);
        let pixel_value = to_u16(&pixel_data);
        let x = i%TILE_WIDTH;
        let i_f64 = i as f64;
        let tile_height_f64 = TILE_HEIGHT as f64;
        let y = i_f64/tile_height_f64;
        let y = y.floor() as u32;
        let r = pixel_value*4+2;
        let r = r as usize;
        let r = palette[r];
        let g = pixel_value*4+1;
        let g = g as usize;
        let g = palette[g];
        let b = pixel_value*4+0;
        let b = b as usize;
        let b = palette[b];

        let mut a = 0xff;
        if r == 0 && g == 0 && b == 0 {
            a = 0x00;
        }
        let pixel = Rgba{data: [r, g, b, a]};
        bmp.put_pixel(x, y, pixel);
    }

    bmp
}

fn get_palette () -> Vec<u8> {
    //  B,    G,    R,    A
    vec![
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0xFF, 0xFF, 0x8B, 0x00,
        0xC3, 0xCF, 0x4B, 0x00,
        0x8B, 0xA3, 0x1B, 0x00,
        0x57, 0x77, 0x00, 0x00,
        0x8B, 0xA3, 0x1B, 0x00,
        0xC3, 0xCF, 0x4B, 0x00,
        0xFB, 0xFB, 0xFB, 0x00,
        0xEB, 0xE7, 0xE7, 0x00,
        0xDB, 0xD3, 0xD3, 0x00,
        0xCB, 0xC3, 0xC3, 0x00,
        0xBB, 0xB3, 0xB3, 0x00,
        0xAB, 0xA3, 0xA3, 0x00,
        0x9B, 0x8F, 0x8F, 0x00,
        0x8B, 0x7F, 0x7F, 0x00,
        0x7B, 0x6F, 0x6F, 0x00,
        0x67, 0x5B, 0x5B, 0x00,
        0x57, 0x4B, 0x4B, 0x00,
        0x47, 0x3B, 0x3B, 0x00,
        0x33, 0x2B, 0x2B, 0x00,
        0x23, 0x1B, 0x1B, 0x00,
        0x13, 0x0F, 0x0F, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0xC7, 0x43, 0x00,
        0x00, 0xB7, 0x43, 0x00,
        0x00, 0xAB, 0x3F, 0x00,
        0x00, 0x9F, 0x3F, 0x00,
        0x00, 0x93, 0x3F, 0x00,
        0x00, 0x87, 0x3B, 0x00,
        0x00, 0x7B, 0x37, 0x00,
        0x00, 0x6F, 0x33, 0x00,
        0x00, 0x63, 0x33, 0x00,
        0x00, 0x53, 0x2B, 0x00,
        0x00, 0x47, 0x27, 0x00,
        0x00, 0x3B, 0x23, 0x00,
        0x00, 0x2F, 0x1B, 0x00,
        0x00, 0x23, 0x13, 0x00,
        0x00, 0x17, 0x0F, 0x00,
        0x00, 0x0B, 0x07, 0x00,
        0x4B, 0x7B, 0xBB, 0x00,
        0x43, 0x73, 0xB3, 0x00,
        0x43, 0x6B, 0xAB, 0x00,
        0x3B, 0x63, 0xA3, 0x00,
        0x3B, 0x63, 0x9B, 0x00,
        0x33, 0x5B, 0x93, 0x00,
        0x33, 0x5B, 0x8B, 0x00,
        0x2B, 0x53, 0x83, 0x00,
        0x2B, 0x4B, 0x73, 0x00,
        0x23, 0x4B, 0x6B, 0x00,
        0x23, 0x43, 0x5F, 0x00,
        0x1B, 0x3B, 0x53, 0x00,
        0x1B, 0x37, 0x47, 0x00,
        0x1B, 0x33, 0x43, 0x00,
        0x13, 0x2B, 0x3B, 0x00,
        0x0B, 0x23, 0x2B, 0x00,
        0xD7, 0xFF, 0xFF, 0x00,
        0xBB, 0xEF, 0xEF, 0x00,
        0xA3, 0xDF, 0xDF, 0x00,
        0x8B, 0xCF, 0xCF, 0x00,
        0x77, 0xC3, 0xC3, 0x00,
        0x63, 0xB3, 0xB3, 0x00,
        0x53, 0xA3, 0xA3, 0x00,
        0x43, 0x93, 0x93, 0x00,
        0x33, 0x87, 0x87, 0x00,
        0x27, 0x77, 0x77, 0x00,
        0x1B, 0x67, 0x67, 0x00,
        0x13, 0x5B, 0x5B, 0x00,
        0x0B, 0x4B, 0x4B, 0x00,
        0x07, 0x3B, 0x3B, 0x00,
        0x00, 0x2B, 0x2B, 0x00,
        0x00, 0x1F, 0x1F, 0x00,
        0xDB, 0xEB, 0xFB, 0x00,
        0xD3, 0xE3, 0xFB, 0x00,
        0xC3, 0xDB, 0xFB, 0x00,
        0xBB, 0xD3, 0xFB, 0x00,
        0xB3, 0xCB, 0xFB, 0x00,
        0xA3, 0xC3, 0xFB, 0x00,
        0x9B, 0xBB, 0xFB, 0x00,
        0x8F, 0xB7, 0xFB, 0x00,
        0x83, 0xB3, 0xF7, 0x00,
        0x73, 0xA7, 0xFB, 0x00,
        0x63, 0x9B, 0xFB, 0x00,
        0x5B, 0x93, 0xF3, 0x00,
        0x5B, 0x8B, 0xEB, 0x00,
        0x53, 0x8B, 0xDB, 0x00,
        0x53, 0x83, 0xD3, 0x00,
        0x4B, 0x7B, 0xCB, 0x00,
        0x9B, 0xC7, 0xFF, 0x00,
        0x8F, 0xB7, 0xF7, 0x00,
        0x87, 0xB3, 0xEF, 0x00,
        0x7F, 0xA7, 0xF3, 0x00,
        0x73, 0x9F, 0xEF, 0x00,
        0x53, 0x83, 0xCF, 0x00,
        0x3B, 0x6B, 0xB3, 0x00,
        0x2F, 0x5B, 0xA3, 0x00,
        0x23, 0x4F, 0x93, 0x00,
        0x1B, 0x43, 0x83, 0x00,
        0x13, 0x3B, 0x77, 0x00,
        0x0B, 0x2F, 0x67, 0x00,
        0x07, 0x27, 0x57, 0x00,
        0x00, 0x1B, 0x47, 0x00,
        0x00, 0x13, 0x37, 0x00,
        0x00, 0x0F, 0x2B, 0x00,
        0xFB, 0xFB, 0xE7, 0x00,
        0xF3, 0xF3, 0xD3, 0x00,
        0xEB, 0xE7, 0xC7, 0x00,
        0xE3, 0xDF, 0xB7, 0x00,
        0xDB, 0xD7, 0xA7, 0x00,
        0xD3, 0xCF, 0x97, 0x00,
        0xCB, 0xC7, 0x8B, 0x00,
        0xC3, 0xBB, 0x7F, 0x00,
        0xBB, 0xB3, 0x73, 0x00,
        0xAF, 0xA7, 0x63, 0x00,
        0x9B, 0x93, 0x47, 0x00,
        0x87, 0x7B, 0x33, 0x00,
        0x6F, 0x67, 0x1F, 0x00,
        0x5B, 0x53, 0x0F, 0x00,
        0x47, 0x43, 0x00, 0x00,
        0x37, 0x33, 0x00, 0x00,
        0xFF, 0xF7, 0xF7, 0x00,
        0xEF, 0xDF, 0xDF, 0x00,
        0xDF, 0xC7, 0xC7, 0x00,
        0xCF, 0xB3, 0xB3, 0x00,
        0xBF, 0x9F, 0x9F, 0x00,
        0xB3, 0x8B, 0x8B, 0x00,
        0xA3, 0x7B, 0x7B, 0x00,
        0x93, 0x6B, 0x6B, 0x00,
        0x83, 0x57, 0x57, 0x00,
        0x73, 0x4B, 0x4B, 0x00,
        0x67, 0x3B, 0x3B, 0x00,
        0x57, 0x2F, 0x2F, 0x00,
        0x47, 0x27, 0x27, 0x00,
        0x37, 0x1B, 0x1B, 0x00,
        0x27, 0x13, 0x13, 0x00,
        0x1B, 0x0B, 0x0B, 0x00,
        0xF7, 0xB3, 0x37, 0x00,
        0xE7, 0x93, 0x07, 0x00,
        0xFB, 0x53, 0x0B, 0x00,
        0xFB, 0x00, 0x00, 0x00,
        0xCB, 0x00, 0x00, 0x00,
        0x9F, 0x00, 0x00, 0x00,
        0x6F, 0x00, 0x00, 0x00,
        0x43, 0x00, 0x00, 0x00,
        0xBF, 0xBB, 0xFB, 0x00,
        0x8F, 0x8B, 0xFB, 0x00,
        0x5F, 0x5B, 0xFB, 0x00,
        0x93, 0xBB, 0xFF, 0x00,
        0x5F, 0x97, 0xF7, 0x00,
        0x3B, 0x7B, 0xEF, 0x00,
        0x23, 0x63, 0xC3, 0x00,
        0x13, 0x53, 0xB3, 0x00,
        0x00, 0x00, 0xFF, 0x00,
        0x00, 0x00, 0xEF, 0x00,
        0x00, 0x00, 0xE3, 0x00,
        0x00, 0x00, 0xD3, 0x00,
        0x00, 0x00, 0xC3, 0x00,
        0x00, 0x00, 0xB7, 0x00,
        0x00, 0x00, 0xA7, 0x00,
        0x00, 0x00, 0x9B, 0x00,
        0x00, 0x00, 0x8B, 0x00,
        0x00, 0x00, 0x7F, 0x00,
        0x00, 0x00, 0x6F, 0x00,
        0x00, 0x00, 0x63, 0x00,
        0x00, 0x00, 0x53, 0x00,
        0x00, 0x00, 0x47, 0x00,
        0x00, 0x00, 0x37, 0x00,
        0x00, 0x00, 0x2B, 0x00,
        0x00, 0xFF, 0xFF, 0x00,
        0x00, 0xE3, 0xF7, 0x00,
        0x00, 0xCF, 0xF3, 0x00,
        0x00, 0xB7, 0xEF, 0x00,
        0x00, 0xA3, 0xEB, 0x00,
        0x00, 0x8B, 0xE7, 0x00,
        0x00, 0x77, 0xDF, 0x00,
        0x00, 0x63, 0xDB, 0x00,
        0x00, 0x4F, 0xD7, 0x00,
        0x00, 0x3F, 0xD3, 0x00,
        0x00, 0x2F, 0xCF, 0x00,
        0x97, 0xFF, 0xFF, 0x00,
        0x83, 0xDF, 0xEF, 0x00,
        0x73, 0xC3, 0xDF, 0x00,
        0x5F, 0xA7, 0xCF, 0x00,
        0x53, 0x8B, 0xC3, 0x00,
        0x2B, 0x2B, 0x00, 0x00,
        0x23, 0x23, 0x00, 0x00,
        0x1B, 0x1B, 0x00, 0x00,
        0x13, 0x13, 0x00, 0x00,
        0xFF, 0x0B, 0x00, 0x00,
        0xFF, 0x00, 0x4B, 0x00,
        0xFF, 0x00, 0xA3, 0x00,
        0xFF, 0x00, 0xFF, 0x00,
        0x00, 0xFF, 0x00, 0x00,
        0x00, 0x4B, 0x00, 0x00,
        0xFF, 0xFF, 0x00, 0x00,
        0xFF, 0x33, 0x2F, 0x00,
        0x00, 0x00, 0xFF, 0x00,
        0x00, 0x1F, 0x97, 0x00,
        0xDF, 0x00, 0xFF, 0x00,
        0x73, 0x00, 0x77, 0x00,
        0x6B, 0x7B, 0xC3, 0x00,
        0x57, 0x57, 0xAB, 0x00,
        0x57, 0x47, 0x93, 0x00,
        0x53, 0x37, 0x7F, 0x00,
        0x4F, 0x27, 0x67, 0x00,
        0x47, 0x1B, 0x4F, 0x00,
        0x3B, 0x13, 0x3B, 0x00,
        0x27, 0x77, 0x77, 0x00,
        0x23, 0x73, 0x73, 0x00,
        0x1F, 0x6F, 0x6F, 0x00,
        0x1B, 0x6B, 0x6B, 0x00,
        0x1B, 0x67, 0x67, 0x00,
        0x1B, 0x6B, 0x6B, 0x00,
        0x1F, 0x6F, 0x6F, 0x00,
        0x23, 0x73, 0x73, 0x00,
        0x27, 0x77, 0x77, 0x00,
        0xFF, 0xFF, 0xEF, 0x00,
        0xF7, 0xF7, 0xDB, 0x00,
        0xF3, 0xEF, 0xCB, 0x00,
        0xEF, 0xEB, 0xBB, 0x00,
        0xF3, 0xEF, 0xCB, 0x00,
        0xE7, 0x93, 0x07, 0x00,
        0xE7, 0x97, 0x0F, 0x00,
        0xEB, 0x9F, 0x17, 0x00,
        0xEF, 0xA3, 0x23, 0x00,
        0xF3, 0xAB, 0x2B, 0x00,
        0xF7, 0xB3, 0x37, 0x00,
        0xEF, 0xA7, 0x27, 0x00,
        0xEB, 0x9F, 0x1B, 0x00,
        0xE7, 0x97, 0x0F, 0x00,
        0x0B, 0xCB, 0xFB, 0x00,
        0x0B, 0xA3, 0xFB, 0x00,
        0x0B, 0x73, 0xFB, 0x00,
        0x0B, 0x4B, 0xFB, 0x00,
        0x0B, 0x23, 0xFB, 0x00,
        0x0B, 0x73, 0xFB, 0x00,
        0x00, 0x13, 0x93, 0x00,
        0x00, 0x0B, 0xD3, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
        0xFF, 0xFF, 0xFF, 0x00
    ]
}
