#[derive(Debug, PartialEq)]
enum ImageType {
    Jpeg,
    Png,
    Gif,
    WebP,
    AnimatedWebP,
    Unsupported,
}

// fn process_image(file_path_string: String) -> Result<String, String> {
//     // convert string to path to use in rust
//     let file_path = std::path::Path::new(&file_path_string);

//     // get the data directory path
//     let data_dir = tauri::api::path::data_dir().ok_or("Failed to get data directory")?;
//     let mut destination = data_dir.clone();
//     // we can hardcode the file name since
//     // 1. we are only dealing with one wallpaper image at a time
//     // 2. we will be converting all images to the webp format
//     destination.push("wallpaper.webp");

//     // determine the file format
// }

/// Determines the image type of the file at the given path.
/// Returns `Ok(ImageType)` on success, or an `Err(String)` if there is an error.
fn determine_image_type(file_path: &std::path::Path) -> Result<ImageType, String> {
    // Open the file
    let mut file =
        std::fs::File::open(file_path).map_err(|e| format!("Failed to open file: {e}"))?;

    // Read the first few bytes to determine the format
    let mut buffer = [0; 12];
    std::io::Read::read_exact(&mut file, &mut buffer)
        .map_err(|e| format!("Failed to read file: {e}"))?;

    // Check static formats using the image crate
    match image::guess_format(&buffer) {
        Ok(image::ImageFormat::Jpeg) => Ok(ImageType::Jpeg),
        Ok(image::ImageFormat::Png) => Ok(ImageType::Png),
        Ok(image::ImageFormat::Gif) => Ok(ImageType::Gif),
        Ok(image::ImageFormat::WebP) => {
            // Additional check for animation in WebP
            is_animated_webp(file_path).map(|animated| {
                if animated {
                    ImageType::AnimatedWebP
                } else {
                    ImageType::WebP
                }
            })
        }
        _ => Ok(ImageType::Unsupported),
    }
}

/// check if the file is a valid webp image and if it is animated
/// returns `Ok(true)` if it is animated, `Ok(false)` if it is not
/// and errors out if the file cannot be read or is not a webp
fn is_animated_webp(file_path: &std::path::Path) -> Result<bool, String> {
    // open the file and read its contents into a buffer
    let mut buffer = Vec::new();
    std::fs::File::open(file_path)
        .and_then(|mut file| std::io::Read::read_to_end(&mut file, &mut buffer))
        .map_err(|e| format!("Failed to read file: {}", e))?;

    // use the webp crate to decode the image and check for animation
    webp::AnimDecoder::new(&buffer)
        .decode()
        .map(|anim| anim.has_animation())
        .map_err(|_| "File is not a valid WebP image".to_string())
}
fn main() {
    // Example usage
    let path = std::path::Path::new("/home/vomitblood/Downloads/title.keys");
    match determine_image_type(path) {
        Ok(image_type) => println!("Image type: {:?}", image_type),
        Err(e) => println!("Error determining image type: {}", e),
    }
}
