using System;
using System.Collections.Generic;
using System.Text;

namespace UnityAssistant
{
    public static class ToolDispatcher
    {
        public static string Dispatch(string body)
        {
            var req = JsonUtil.ParseObject(body);
            if (req == null) return "{\"ok\":false,\"error\":\"invalid json\"}";

            string tool = JsonUtil.Get(req, "tool");
            var args = JsonUtil.GetObj(req, "args") ?? new Dictionary<string, object>();

            try
            {
                string result = tool switch
                {
                    "ping" => "{\"ok\":true}",
                    "get_pending_selection" => SelectionWatcher.DrainPending(),
                    "get_selection" => HierarchyTools.GetSelection(args),
                    "get_hierarchy" => HierarchyTools.GetHierarchy(args),
                    "select_object" => HierarchyTools.SelectObject(args),
                    "select_objects" => HierarchyTools.SelectObjects(args),
                    "create_object" => HierarchyTools.CreateObject(args),
                    "delete_object" => HierarchyTools.DeleteObject(args),
                    "rename_object" => HierarchyTools.RenameObject(args),
                    "reparent_object" => HierarchyTools.ReparentObject(args),
                    "set_transform" => HierarchyTools.SetTransform(args),
                    "duplicate_object" => HierarchyTools.DuplicateObject(args),

                    "get_inspector" => InspectorTools.GetInspector(args),
                    "get_asset_inspector" => InspectorTools.GetAssetInspector(args),
                    "set_field" => InspectorTools.SetField(args),
                    "set_asset_field" => InspectorTools.SetAssetField(args),
                    "add_component" => InspectorTools.AddComponent(args),
                    "remove_component" => InspectorTools.RemoveComponent(args),

                    "list_assets" => AssetTools.ListAssets(args),
                    "read_script" => AssetTools.ReadScript(args),
                    "create_script" => AssetTools.CreateScript(args),
                    "edit_script" => AssetTools.EditScript(args),
                    "create_material" => AssetTools.CreateMaterial(args),
                    "create_scriptable_object" => AssetTools.CreateScriptableObject(args),
                    "assign_material" => AssetTools.AssignMaterial(args),
                    "set_material_property" => AssetTools.SetMaterialProperty(args),
                    "create_prefab" => AssetTools.CreatePrefab(args),
                    "apply_prefab_instance" => AssetTools.ApplyPrefabInstance(args),
                    "instantiate_prefab" => AssetTools.InstantiatePrefab(args),
                    "move_asset" => AssetTools.MoveAsset(args),
                    "delete_asset" => AssetTools.DeleteAsset(args),
                    "import_asset" => AssetTools.ImportAsset(args),
                    "take_asset_preview" => AssetTools.TakeAssetPreview(args),

                    "get_animation_setup" => AnimationTools.GetAnimationSetup(args),
                    "create_animation_clip" => AnimationTools.CreateAnimationClip(args),
                    "set_animation_clip_settings" => AnimationTools.SetAnimationClipSettings(args),
                    "add_animation_curve" => AnimationTools.AddAnimationCurve(args),
                    "add_transform_keyframes" => AnimationTools.AddTransformKeyframes(args),
                    "add_object_reference_keyframes" => AnimationTools.AddObjectReferenceKeyframes(args),
                    "create_animator_controller" => AnimationTools.CreateAnimatorController(args),
                    "add_animator_parameter" => AnimationTools.AddAnimatorParameter(args),
                    "add_animator_state" => AnimationTools.AddAnimatorState(args),
                    "add_animator_transition" => AnimationTools.AddAnimatorTransition(args),
                    "set_animator_default_state" => AnimationTools.SetAnimatorDefaultState(args),
                    "assign_animator_controller" => AnimationTools.AssignAnimatorController(args),
                    "configure_model_animation_import" => AnimationTools.ConfigureModelAnimationImport(args),
                    "sample_animation_clip" => AnimationTools.SampleAnimationClip(args),

                    "compile" => EditorTools.Compile(args),
                    "play" => EditorTools.Play(args),
                    "stop" => EditorTools.Stop(args),
                    "get_console_summary" => EditorTools.GetConsoleSummary(args),
                    "get_console_logs" => EditorTools.GetConsoleLogs(args),
                    "clear_console" => EditorTools.ClearConsole(args),
                    "get_project_health" => EditorTools.GetProjectHealth(args),
                    "get_active_scene" => EditorTools.GetActiveScene(args),
                    "create_scene" => EditorTools.CreateScene(args),
                    "save_scene" => EditorTools.SaveScene(args),
                    "open_scene" => EditorTools.OpenScene(args),
                    "create_tag" => EditorTools.CreateTag(args),
                    "execute_menu" => EditorTools.ExecuteMenu(args),
                    "get_project_info" => EditorTools.GetProjectInfo(args),
                    "take_scene_view_image" => EditorTools.TakeSceneViewImage(args),
                    "take_game_view_image" => EditorTools.TakeGameViewImage(args),
                    "get_build_settings" => BuildTools.GetBuildSettings(args),
                    "set_build_target" => BuildTools.SetBuildTarget(args),
                    "trigger_build" => BuildTools.TriggerBuild(args),

                    "index_project" => IndexerTools.IndexProject(args),
                    "search_index" => IndexerTools.SearchIndex(args),
                    "resolve_asset" => IndexerTools.ResolveAsset(args),
                    "get_project_graph" => IndexerTools.GetProjectGraph(args),
                    "find_in_files" => IndexerTools.FindInFiles(args),

                    _ => "{\"ok\":false,\"error\":\"unknown tool: " + tool + "\"}"
                };
                return result;
            }
            catch (Exception e)
            {
                return "{\"ok\":false,\"error\":" + JsonUtil.Quote(e.Message) + "}";
            }
        }
    }
}
