//! Shared Material authoring presentation contracts.
//!
//! World queries and scene mutations stay with callers. Widgets render a compact view model and
//! return explicit actions, which keeps Inspector, Content Browser, and Material Library behavior
//! consistent without giving UI code arbitrary world access.

use std::collections::HashSet;
use std::path::Path;

use bevy::prelude::World;
use bevy_egui::egui;
use shared::{
    standard_lit_input_schema, EditorAssetRef, MaterialAsset, MaterialInputSchema,
    MaterialInputSet, MaterialInstanceAsset, MaterialParameter, MaterialRef, MaterialShaderKind,
    MaterialTextureBinding, ShaderPropertyType, ShaderSchemaAsset,
};

use crate::asset_db::{ensure_asset_record, find_asset_by_path, save_registry};
use crate::assets::static_mesh::load_static_mesh_manifest;
use crate::assets::{
    invalidate_on_catalog_refresh, prefetch_asset_thumbnails, AssetSelection, AssetSubAssetKind,
    EditorAsset, EditorAssetKind, EditorAssets,
};
use crate::project::collaboration::{publish_authored_file, FileSnapshot, FileWriteIntent};
use crate::scene_io::SceneIo;
use crate::ui::document_status::authored_document_status_ui;

use super::theme::{ERROR, TEXT_DIM, TEXT_MUTED, WARNING};

mod asset_editor;
mod editor;
mod instance_editor;
mod instance_inputs;

pub(crate) use asset_editor::material_asset_editor;
pub(crate) use editor::{
    create_material_instance_from_base, inline_material_document_editor, material_shader_kind,
    material_shader_label, set_material_shader_kind,
};
use editor::{material_shader_settings, material_texture_candidates, shader_schema_assets};
pub(crate) use editor_ui::materials::{
    compact_text_edit, default_value_for_shader_property, ensure_material_texture_binding,
    material_input_schema_editor, material_input_schema_editor_with_inheritance,
    material_slot_panel, material_surface_options_ui, materials_section, optional_path_edit,
    parameter_value_matches_property, shader_kind_picker, MaterialHealth,
    MaterialInputEditorResponse, MaterialLayerBadge, MaterialLocateTarget, MaterialPickerCandidate,
    MaterialSlotAction, MaterialSlotPanelViewModel, MaterialTextureCandidate,
    MaterialThumbnailPresentation, MaterialsSectionViewModel,
};
pub(crate) use instance_editor::material_instance_asset_editor;

fn natural_cmp(left: &str, right: &str) -> std::cmp::Ordering {
    left.to_ascii_lowercase().cmp(&right.to_ascii_lowercase())
}

fn material_locate_selection(target: MaterialLocateTarget) -> AssetSelection {
    match target {
        MaterialLocateTarget::File(path) => AssetSelection::File(path),
        MaterialLocateTarget::SubAsset {
            parent_path,
            sub_asset_id,
            label,
            source_path,
        } => AssetSelection::SubAsset {
            parent_path,
            sub_asset_id,
            label,
            kind: AssetSubAssetKind::Texture,
            source_path,
        },
    }
}

fn material_locate_matches_selection(
    target: &MaterialLocateTarget,
    selection: &AssetSelection,
) -> bool {
    match (target, selection) {
        (MaterialLocateTarget::File(left), AssetSelection::File(right)) => left == right,
        (
            MaterialLocateTarget::SubAsset {
                parent_path: left_parent,
                sub_asset_id: left_sub_asset,
                ..
            },
            AssetSelection::SubAsset {
                parent_path: right_parent,
                sub_asset_id: right_sub_asset,
                ..
            },
        ) => left_parent == right_parent && left_sub_asset == right_sub_asset,
        _ => false,
    }
}
