//! Asset browser UI state and view enums.

use std::collections::HashSet;

use bevy::prelude::*;
use shared::{MaterialAsset, MaterialInstanceAsset};

use crate::asset_db::ImportSettings;
use crate::assets::{AssetSelection, ASSETS_ROOT, BUILTINS_FOLDER};
use crate::project::collaboration::FileSnapshot;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AssetBrowserView {
    Grid,
    List,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AssetSort {
    Name,
    Kind,
    Modified,
    Size,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum AssetKindFilter {
    All,
    Model,
    Texture,
    Material,
    Audio,
    Level,
    Prefab,
    Builtin,
}

#[derive(Resource, Debug)]
pub struct AssetBrowserUiState {
    pub(crate) search: String,
    pub(crate) view: AssetBrowserView,
    pub(crate) sort: AssetSort,
    pub(crate) kind_filter: AssetKindFilter,
    pub(crate) thumbnail_size: f32,
    pub(crate) recursive: bool,
    pub(crate) show_details: bool,
    pub(crate) expanded_folders: HashSet<String>,
    pub(crate) expanded_assets: HashSet<String>,
    pub(crate) pending_delete: Option<AssetDeleteRequest>,
    pub(crate) import_draft: Option<ImportSettingsDraft>,
    pub(crate) material_draft: Option<MaterialAssetDraft>,
    pub(crate) material_instance_draft: Option<MaterialInstanceAssetDraft>,
}

#[derive(Debug, Clone)]
pub(crate) struct AssetDeleteRequest {
    pub(crate) selection: AssetSelection,
    pub(crate) label: String,
    pub(crate) files: Vec<String>,
    pub(crate) warnings: Vec<String>,
}

#[derive(Debug, Clone)]
pub(crate) struct ImportSettingsDraft {
    pub(crate) path: String,
    pub(crate) settings: ImportSettings,
}

#[derive(Debug, Clone)]
pub(crate) struct MaterialAssetDraft {
    pub(crate) path: String,
    pub(crate) asset: MaterialAsset,
    pub(crate) disk_snapshot: FileSnapshot,
    pub(crate) error: Option<String>,
}

#[derive(Debug, Clone)]
pub(crate) struct MaterialInstanceAssetDraft {
    pub(crate) path: String,
    pub(crate) asset: MaterialInstanceAsset,
    pub(crate) disk_snapshot: FileSnapshot,
    pub(crate) error: Option<String>,
}

impl Default for AssetBrowserUiState {
    fn default() -> Self {
        let mut expanded_folders = HashSet::new();
        expanded_folders.insert(ASSETS_ROOT.to_string());
        expanded_folders.insert(BUILTINS_FOLDER.to_string());
        Self {
            search: String::new(),
            view: AssetBrowserView::Grid,
            sort: AssetSort::Name,
            kind_filter: AssetKindFilter::All,
            thumbnail_size: 72.0,
            recursive: false,
            show_details: true,
            expanded_folders,
            expanded_assets: HashSet::new(),
            pending_delete: None,
            import_draft: None,
            material_draft: None,
            material_instance_draft: None,
        }
    }
}
