#if UNITY_EDITOR
namespace NiAkaDev.EzMirror.Editor
{
    using UnityEditor;
    using UnityEngine;

    [CustomEditor(typeof(EzMirrorProfileAsset))]
    [CanEditMultipleObjects]
    public sealed class EzMirrorProfileAssetEditor : UnityEditor.Editor
    {
        private SerializedProperty profileName;
        private SerializedProperty isDefault;
        private SerializedProperty mirrorEnabled;
        private SerializedProperty pcOnly;
        private SerializedProperty disablePixelLights;
        private SerializedProperty turnOffMirrorOcclusion;
        private SerializedProperty reflectLayers;
        private SerializedProperty cameraClearFlags;
        private SerializedProperty customSkybox;
        private SerializedProperty customClearColor;
        private SerializedProperty hideBackground;
        private SerializedProperty transparency;
        private SerializedProperty baseMaterial;
        private SerializedProperty iconSprite;
        private SerializedProperty iconColor;

        private void OnEnable()
        {
            profileName = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.profileName));
            isDefault = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.isDefault));
            mirrorEnabled = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.mirrorEnabled));
            pcOnly = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.pcOnly));
            disablePixelLights = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.disablePixelLights));
            turnOffMirrorOcclusion = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.turnOffMirrorOcclusion));
            reflectLayers = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.reflectLayers));
            cameraClearFlags = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.cameraClearFlags));
            customSkybox = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.customSkybox));
            customClearColor = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.customClearColor));
            hideBackground = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.hideBackground));
            transparency = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.transparency));
            baseMaterial = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.baseMaterial));
            iconSprite = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.iconSprite));
            iconColor = serializedObject.FindProperty(nameof(EzMirrorProfileAsset.iconColor));
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.LabelField(
                L("ProfileAssetEditorTitle", "EzMirror プロファイルアセット"),
                EditorStyles.boldLabel
            );
            EditorGUILayout.HelpBox(
                L(
                    "ProfileAssetEditorDescription",
                    "複数の EzMirror で共有できる設定ファイルです。プロファイル名・画質・透明度など、Sceneに依存しない設定だけを保存します。Scene内オブジェクトは保存しません。"
                ),
                MessageType.Info
            );

            DrawBasicSettings();
            DrawAppearanceSettings();
            DrawMirrorQualitySettings();
            DrawBackgroundSettings();

            serializedObject.ApplyModifiedProperties();
        }

        private void DrawBasicSettings()
        {
            DrawGroupHeader("ProfileAssetEditorSectionBasic", "基本");
            DrawField(
                profileName,
                "ProfileAssetEditorProfileName",
                "プロファイル名",
                "ProfileAssetEditorProfileNameTooltip",
                "例: HQミラー、半透明ミラー、LQミラー"
            );
            DrawField(
                isDefault,
                "ProfileAssetEditorIsDefault",
                "初期状態として使う",
                "ProfileAssetEditorIsDefaultTooltip",
                "Reset時やワールド開始時にこのプロファイルを使う場合はONにします。"
            );
            DrawField(
                mirrorEnabled,
                "ProfileAssetEditorMirrorEnabled",
                "ミラーを表示する",
                "ProfileAssetEditorMirrorEnabledTooltip",
                "OFFにすると、このプロファイル選択中はミラー本体を非表示にします。"
            );
            DrawField(
                pcOnly,
                "ProfileAssetEditorPcOnly",
                "PC専用にする",
                "ProfileAssetEditorPcOnlyTooltip",
                "Quest / Android / iOS ではこのプロファイルをスキップします。"
            );
        }

        private void DrawAppearanceSettings()
        {
            DrawGroupHeader("ProfileAssetEditorSectionAppearance", "見た目");
            DrawSlider(
                transparency,
                0f,
                1f,
                "ProfileAssetEditorTransparency",
                "透明度",
                "ProfileAssetEditorTransparencyTooltip",
                "0 は不透明、1 は透明です。半透明ミラーは 0.5 前後が目安です。"
            );
            DrawField(
                baseMaterial,
                "ProfileAssetEditorBaseMaterial",
                "基盤マテリアル",
                "ProfileAssetEditorBaseMaterialTooltip",
                "プロファイル用Materialを自動生成するときの元になるMaterialです。"
            );
            DrawField(
                iconSprite,
                "ProfileAssetEditorIconSprite",
                "UIアイコン画像",
                "ProfileAssetEditorIconSpriteTooltip",
                "切り替えUIに表示する白線Spriteです。通常は付属の標準アイコン1枚を使います。"
            );
            DrawField(
                iconColor,
                "ProfileAssetEditorIconColor",
                "UIアイコン色",
                "ProfileAssetEditorIconColorTooltip",
                "白線アイコンに乗せる色です。EzMirror 側の Image.color に反映されます。"
            );
            DrawIconPreview();
        }

        private void DrawIconPreview()
        {
            Sprite sprite =
                iconSprite.objectReferenceValue as Sprite
                ?? EzMirrorAssetResolver.LoadDefaultIconSprite(0);
            if (sprite == null)
                return;

            EditorGUILayout.LabelField(
                L("ProfileAssetEditorIconPreview", "アイコン色プレビュー"),
                EditorStyles.miniBoldLabel
            );
            Rect rect = GUILayoutUtility.GetRect(72f, 72f, GUILayout.ExpandWidth(false));
            if (Event.current.type != EventType.Repaint)
                return;

            Color backgroundColor = EditorGUIUtility.isProSkin
                ? new Color(0.12f, 0.12f, 0.14f)
                : new Color(0.98f, 0.94f, 0.97f);
            EditorGUI.DrawRect(rect, backgroundColor);
            GUI.Box(rect, GUIContent.none, EditorStyles.helpBox);
            Rect previewRect = new Rect(
                rect.x + 6f,
                rect.y + 6f,
                rect.width - 12f,
                rect.height - 12f
            );
            EzMirrorEditor.DrawSpritePreview(previewRect, sprite, iconColor.colorValue);
        }

        private void DrawMirrorQualitySettings()
        {
            DrawGroupHeader("ProfileAssetEditorSectionMirrorQuality", "ミラー画質");
            DrawField(
                reflectLayers,
                "ProfileAssetEditorReflectLayers",
                "映すレイヤー",
                "ProfileAssetEditorReflectLayersTooltip",
                "ミラーに映したいLayerを選びます。通常は既定値のままで問題ありません。"
            );
            DrawField(
                disablePixelLights,
                "ProfileAssetEditorDisablePixelLights",
                "Pixel Lightを無効化",
                "ProfileAssetEditorDisablePixelLightsTooltip",
                "重くなりやすいPixel Lightをミラー内で無効化します。"
            );
            DrawField(
                turnOffMirrorOcclusion,
                "ProfileAssetEditorTurnOffMirrorOcclusion",
                "Mirror Occlusionを無効化",
                "ProfileAssetEditorTurnOffMirrorOcclusionTooltip",
                "通常はONのままで問題ありません。"
            );
        }

        private void DrawBackgroundSettings()
        {
            DrawGroupHeader("ProfileAssetEditorSectionBackground", "背景");
            DrawField(
                cameraClearFlags,
                "ProfileAssetEditorCameraClearFlags",
                "背景の消し方",
                "ProfileAssetEditorCameraClearFlagsTooltip",
                "LQミラーで背景を消したい場合は Solid Color を使います。"
            );
            DrawField(
                customSkybox,
                "ProfileAssetEditorCustomSkybox",
                "Skyboxマテリアル",
                "ProfileAssetEditorCustomSkyboxTooltip",
                "背景に個別Skyboxを使う場合だけ設定します。"
            );
            DrawField(
                customClearColor,
                "ProfileAssetEditorCustomClearColor",
                "背景色",
                "ProfileAssetEditorCustomClearColorTooltip",
                "Solid Color の色です。背景を透明にする場合は Alpha を 0 にします。"
            );
            DrawField(
                hideBackground,
                "ProfileAssetEditorHideBackground",
                "背景を透明化",
                "ProfileAssetEditorHideBackgroundTooltip",
                "ミラー用マテリアル側でも背景を透明化する場合にONにします。"
            );

            if (hideBackground.boolValue)
            {
                EditorGUILayout.HelpBox(
                    L(
                        "ProfileAssetEditorLqBackgroundHelp",
                        "LQミラーでは、背景クリア球と組み合わせることで空や背後のワールドを映しにくくします。"
                    ),
                    MessageType.None
                );
            }
        }

        private static void DrawGroupHeader(string key, string fallback)
        {
            EditorGUILayout.Space(6);
            EditorGUILayout.LabelField(L(key, fallback), EditorStyles.miniBoldLabel);
        }

        private static void DrawField(
            SerializedProperty property,
            string labelKey,
            string labelFallback,
            string tooltipKey,
            string tooltipFallback
        )
        {
            EditorGUILayout.PropertyField(property, LC(labelKey, labelFallback, tooltipKey, tooltipFallback));
        }

        private static void DrawSlider(
            SerializedProperty property,
            float leftValue,
            float rightValue,
            string labelKey,
            string labelFallback,
            string tooltipKey,
            string tooltipFallback
        )
        {
            EditorGUILayout.Slider(
                property,
                leftValue,
                rightValue,
                LC(labelKey, labelFallback, tooltipKey, tooltipFallback)
            );
        }

        private static string L(string key, string fallback)
        {
            return EzMirrorLocalization.Get(key, fallback);
        }

        private static GUIContent LC(
            string labelKey,
            string labelFallback,
            string tooltipKey,
            string tooltipFallback
        )
        {
            return new GUIContent(L(labelKey, labelFallback), L(tooltipKey, tooltipFallback));
        }
    }
}
#endif
