use crate::ecs::{EditorWorld, RandomKind};
use crate::systems::polyhaven;
use nightshade::prelude::rand;

pub fn request_both(editor_world: &mut EditorWorld) {
    editor_world
        .resources
        .browsers
        .polyhaven_browser
        .ensure_loaded(polyhaven::Category::Models);
    editor_world
        .resources
        .browsers
        .polyhaven_browser
        .ensure_loaded(polyhaven::Category::Hdris);
    editor_world.resources.browsers.pending_random = Some(RandomKind::Both);
}

pub fn poll(editor_world: &mut EditorWorld) {
    let Some(kind) = editor_world.resources.browsers.pending_random else {
        return;
    };
    match kind {
        RandomKind::Both => {
            let hdri_ready = editor_world
                .resources
                .browsers
                .polyhaven_browser
                .entries(polyhaven::Category::Hdris)
                .is_some();
            let model_ready = editor_world
                .resources
                .browsers
                .polyhaven_browser
                .entries(polyhaven::Category::Models)
                .is_some();
            if !hdri_ready || !model_ready {
                return;
            }
            editor_world.resources.browsers.pending_random = Some(RandomKind::Model);
            poll(editor_world);
            editor_world.resources.browsers.pending_random = Some(RandomKind::HdriAfterModel);
        }
        RandomKind::HdriAfterModel => {
            if editor_world
                .resources
                .browsers
                .polyhaven_browser
                .loading_status()
                .is_some()
            {
                return;
            }
            editor_world.resources.browsers.pending_random = Some(RandomKind::Hdri);
            poll(editor_world);
        }
        RandomKind::Model => {
            let Some(entries) = editor_world
                .resources
                .browsers
                .polyhaven_browser
                .entries(polyhaven::Category::Models)
            else {
                return;
            };
            if entries.is_empty() {
                return;
            }
            let pick = rand::random::<u32>() as usize % entries.len();
            let entry = &entries[pick];
            let resolution = editor_world.resources.browsers.random_resolution;
            editor_world
                .resources
                .browsers
                .polyhaven_browser
                .set_preferred_resolution(resolution);
            editor_world
                .resources
                .browsers
                .polyhaven_browser
                .fetch_asset(polyhaven::Category::Models, &entry.slug, &entry.name);
            editor_world.resources.browsers.pending_random = None;
        }
        RandomKind::Hdri => {
            let Some(entries) = editor_world
                .resources
                .browsers
                .polyhaven_browser
                .entries(polyhaven::Category::Hdris)
            else {
                return;
            };
            if entries.is_empty() {
                return;
            }
            let pick = rand::random::<u32>() as usize % entries.len();
            let entry = &entries[pick];
            let resolution = editor_world.resources.browsers.random_resolution;
            editor_world
                .resources
                .browsers
                .polyhaven_browser
                .set_preferred_resolution(resolution);
            editor_world
                .resources
                .browsers
                .polyhaven_browser
                .fetch_asset(polyhaven::Category::Hdris, &entry.slug, &entry.name);
            editor_world.resources.browsers.pending_random = None;
        }
    }
}
