use image::GenericImageView;
use std::sync::Arc;
use std::ffi::CStr;
use std::path::PathBuf;

pub fn load_raw(filename: String) -> (Vec<u8>, (u32,u32)) {

    let project_root =
        std::env::current_dir()
            .expect("failed to get root directory");

    let mut compute_path = project_root.clone();
    compute_path.push(PathBuf::from("resources/images/"));
    compute_path.push(PathBuf::from(filename.clone()));

    let img = image::open(compute_path).expect("Couldn't find image");

    let xy = img.dimensions();

    let data_length = xy.0 * xy.1 * 4;
    let pixel_count = img.raw_pixels().len();

    let mut image_buffer = Vec::new();

    if pixel_count != data_length as usize {
        println!("Creating apha channel...");
        for i in img.raw_pixels().iter() {
            if (image_buffer.len() + 1) % 4 == 0 {
                image_buffer.push(255);
            }
            image_buffer.push(*i);
        }
        image_buffer.push(255);
    } else {
        image_buffer = img.raw_pixels();
    }

    (image_buffer, xy)
}