use std::{io::{Cursor, Write}, sync::Arc};

use file_format::FileFormat;
use iced_video_player::Video;
use ::image::{DynamicImage, ImageReader};
use iced::{gradient, widget::{container, image}, Color};
use tempfile::NamedTempFile;

use crate::{Assets, LauncherBackground, PossibleGames, State};

use super::img_utils::round_image;

pub fn get_game_background(state: &State) -> LauncherBackground {
    let file_path: &str = match state.selected_game {
        PossibleGames::WutheringWaves => "wutheringwaves-bg.mp4",
        PossibleGames::ZenlessZoneZero => "zenlesszonezero-bg.png",
        PossibleGames::HonkaiStarRail => "honkaistarrail-bg.png",
        PossibleGames::GenshinImpact => "genshinimpact-bg.png",
    };

    if let Some(file) = Assets::get(file_path) {
        let data = Arc::new(file.data);
        let file_format = FileFormat::from_bytes(&*data);
        if file_format.extension() == "mp4" {
            let mut temp_file = NamedTempFile::new().unwrap();
            temp_file.write_all(&data).unwrap();

            let temp_path = temp_file.path().to_str().unwrap().to_string();
            match Video::new(url::Url::from_file_path(temp_path).unwrap()) {
                Ok(mut video) => {
                    video.set_looping(true);
                    LauncherBackground::Video(video)
                },
                Err(err) => {
                    panic!("{:#?}", err)
                },
            }
        } else {
            let img = ImageReader::new(Cursor::new(&*data))
                .with_guessed_format()
                .unwrap()
                .decode()
                .unwrap();
            LauncherBackground::Image(img)
        }

    } else {
        panic!("Missing icon for {:?}, path: {}", state.selected_game, file_path)
    }
}

pub fn get_game_icon_dynamic_image(game: &PossibleGames) -> DynamicImage {
    let file_path: &str = match game {
        PossibleGames::WutheringWaves => "wutheringwaves-icon.png",
        PossibleGames::ZenlessZoneZero => "zenlesszonezero-icon.png",
        PossibleGames::HonkaiStarRail => "honkaistarrail-icon.png",
        PossibleGames::GenshinImpact => "genshinimpact-icon.png",
    };
    if let Some(img_file) = Assets::get(file_path) {
        let data_cursor = Cursor::new(img_file.data);
        
        let img = ImageReader::new(data_cursor)
        .with_guessed_format()
        .unwrap()
        .decode()
        .unwrap()
        .resize(128, 128, ::image::imageops::FilterType::Lanczos3);

        round_image(img)
            .unwrap()
    } else {
        panic!("Missing icon for {:?}, path: {}", game, file_path)
    }
}

fn rad(deg: f32) -> f32 {
    deg * std::f32::consts::PI / 180.0
}

pub fn style_container(direction: f32, use_gradient: bool) -> container::Style {
    let angle = rad(direction);
    let gradient: Option<iced::Background> = if use_gradient {            
        Some(gradient::Linear::new(angle)
        .add_stop(0.0, Color::from_rgba8(0, 0, 0, 0.0))
        .add_stop(0.75, Color::from_rgba8(0, 0, 0, 0.75)).into())
    } else {None};
    container::Style {
        text_color: Color::from_rgba8(255, 255, 255, 1.0).into(),
        background: gradient,
        ..container::Style::default()
    }
}