#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

namespace JordanCassady
{
    public class CameraComposition : EditorWindow
    {
        #region PROPERTIES
        private GameObject compositionObject;
        private GUIStyle guiStyle;
        private int tabIndex;
        private GameObject targetCamera;
        private Vector3 revertCameraPosition;
        private Quaternion revertCameraRotation;
        #endregion

        private readonly string[] tabNames = { "Rule of Thirds", "Diagonal", "Golden Ratio", "Golden Spiral" };
        private Color greyColor = new Color(56f / 255f, 56f / 255f, 56f / 255f, 1f);

        [MenuItem("Tools/Camera Composition")]
        public static void ShowWindow()
        {
            var window = GetWindow<CameraComposition>("Camera Composition");
            window.minSize = new Vector2(370, 186);
        }

        private void OnEnable()
        {
            InitGUIStyle();
        }

        private void OnDestroy()
        {
            if (compositionObject != null)
            {
                DestroyImmediate(compositionObject);
            }
        }

        private void OnGUI()
        {
            if (compositionObject == null)
            {
                compositionObject = Instantiate(Resources.Load<GameObject>("Prefabs/Camera Composition"));
            }

            DrawTabSection();
            DrawOverrideSection();
            DrawCameraSection();
        }

        private void InitGUIStyle()
        {
            guiStyle = new GUIStyle
            {
                fontSize = 12,
                fontStyle = FontStyle.Bold,
                normal = { textColor = Color.white }
            };
        }

        private void DrawTabSection()
        {
            tabIndex = GUILayout.Toolbar(tabIndex, tabNames);
            GUILayout.Space(10);

            if (compositionObject == null) return;

            GameObject overlayObject = compositionObject.transform.GetChild(tabIndex).gameObject;
            
            DrawGridSection(overlayObject);
            DrawColorSection(overlayObject);
            DrawOpacitySection(overlayObject);

            if (tabIndex == 3) // Golden Spiral만 회전 지원
            {
                DrawRotationSection(overlayObject);
            }
        }

        private void DrawGridSection(GameObject overlayObject)
        {
            GUILayout.Label("Grid", guiStyle);
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("On", GUILayout.Height(20)))
                overlayObject.GetComponent<CompositionOverlay>().Activate(true);

            if (GUILayout.Button("Off", GUILayout.Height(20)))
                overlayObject.GetComponent<CompositionOverlay>().Activate(false);

            GUILayout.EndHorizontal();
            GUILayout.Space(10);
        }

        private void DrawRotationSection(GameObject overlayObject)
        {
            GUILayout.Label("Rotation", guiStyle);
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("↶ Top Left", GUILayout.Height(30)))
            {
                overlayObject.GetComponent<CompositionOverlay>().Position("Top Left");
                ToggleGrid(overlayObject);
            }

            if (GUILayout.Button("↷ Top Right", GUILayout.Height(30)))
            {
                overlayObject.GetComponent<CompositionOverlay>().Position("Top Right");
                ToggleGrid(overlayObject);
            }

            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("↶ Bottom Left", GUILayout.Height(30)))
            {
                overlayObject.GetComponent<CompositionOverlay>().Position("Bottom Left");
                ToggleGrid(overlayObject);
            }

            if (GUILayout.Button("↷ Bottom Right", GUILayout.Height(30)))
            {
                overlayObject.GetComponent<CompositionOverlay>().Position("Bottom Right");
                ToggleGrid(overlayObject);
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(10);
        }

        private void DrawColorSection(GameObject overlayObject)
        {
            GUILayout.Label("Color", guiStyle);
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("White", GUILayout.Height(20)))
            {
                overlayObject.GetComponent<CompositionOverlay>().InvertLineColor(false);
                ToggleGrid(overlayObject);
            }

            if (GUILayout.Button("Black", GUILayout.Height(20)))
            {
                overlayObject.GetComponent<CompositionOverlay>().InvertLineColor(true);
                ToggleGrid(overlayObject);
            }

            GUILayout.EndHorizontal();
            GUILayout.Space(10);
        }

        private void DrawOpacitySection(GameObject overlayObject)
        {
            GUILayout.Label("Opacity", guiStyle);

            var opacity = overlayObject.GetComponent<CompositionOverlay>().Opacity;
            opacity = EditorGUILayout.Slider(opacity, 0f, 1f);
            overlayObject.GetComponent<CompositionOverlay>().UpdateOpacity(opacity);
            ToggleGrid(overlayObject);

            GUILayout.Space(10);
        }

        private void DrawOverrideSection()
        {
            GUILayout.Label("Overrides", guiStyle);

            if (GUILayout.Button("Turn Off All Grids", GUILayout.Height(20)))
            {
                if (compositionObject != null)
                {
                    foreach (Transform overlayObject in compositionObject.transform)
                    {
                        overlayObject.gameObject.GetComponent<CompositionOverlay>().Activate(false);
                    }
                }
            }
            GUILayout.Space(10);
        }

        private void DrawCameraSection()
        {
            GUILayout.Label("Target Camera", guiStyle);
            targetCamera = (GameObject)EditorGUILayout.ObjectField(targetCamera, typeof(GameObject), true);

            if (targetCamera == null)
            {
                EditorGUILayout.HelpBox("Select a target camera to adjust position & rotation.", MessageType.Info);
                return;
            }

            GUILayout.Space(10);
            GUILayout.Label("Position", guiStyle);
            Undo.RecordObject(targetCamera.transform, "Camera Position Change");
            targetCamera.transform.position = EditorGUILayout.Vector3Field("", targetCamera.transform.position);

            GUILayout.Label("Rotation", guiStyle);
            Undo.RecordObject(targetCamera.transform, "Camera Rotation Change");
            Vector3 rotationVector = EditorGUILayout.Vector3Field("", targetCamera.transform.rotation.eulerAngles);
            targetCamera.transform.rotation = Quaternion.Euler(rotationVector);

            GUILayout.Space(10);
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Align Camera With Scene View", GUILayout.Height(25)))
            {
                revertCameraPosition = targetCamera.transform.position;
                revertCameraRotation = targetCamera.transform.rotation;

                SceneView sceneView = SceneView.lastActiveSceneView;
                targetCamera.transform.position = sceneView.camera.transform.position;
                targetCamera.transform.rotation = sceneView.camera.transform.rotation;
            }

            if (GUILayout.Button("Revert", GUILayout.Height(25)))
            {
                targetCamera.transform.position = revertCameraPosition;
                targetCamera.transform.rotation = revertCameraRotation;
            }

            GUILayout.EndHorizontal();
        }

        private void ToggleGrid(GameObject overlayObject)
        {
            if (overlayObject.GetComponent<CompositionOverlay>().IsActive)
            {
                overlayObject.GetComponent<CompositionOverlay>().Activate(false);
                overlayObject.GetComponent<CompositionOverlay>().Activate(true);
            }
        }
    }
}
#endif
