using ImpactCFX.Decals.EditorScripts;
using ImpactCFX.Decals.HDRP;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

namespace ImpactCFX.Decals.URP.EditorScripts
{
    [CustomEditor(typeof(ImpactDecalHDRP))]
    [CanEditMultipleObjects]
    public class ImpactDecalHDRPEditor : ImpactDecalEditor
    {
        private SerializedProperty decalProjectorProperty;

        private SerializedProperty useAtlasProperty;
        private SerializedProperty atlasUVRegionProperty;
        private SerializedProperty atlasTilesProperty;

        private SerializedProperty fadeOverTimeProperty;
        private SerializedProperty lifeTimeProperty;
        private SerializedProperty fadeTimeProperty;
        private SerializedProperty fadeCurveProperty;

        protected override void OnEnable()
        {
            base.OnEnable();

            decalProjectorProperty = serializedObject.FindProperty("decalProjector");

            useAtlasProperty = serializedObject.FindProperty("useAtlas");
            atlasUVRegionProperty = serializedObject.FindProperty("atlasUVRegion");
            atlasTilesProperty = serializedObject.FindProperty("atlasTiles");

            fadeOverTimeProperty = serializedObject.FindProperty("fadeOverTime");
            lifeTimeProperty = serializedObject.FindProperty("lifeTime");
            fadeTimeProperty = serializedObject.FindProperty("fadeTime");
            fadeCurveProperty = serializedObject.FindProperty("fadeCurve");
        }

        protected override void drawAdditionalProperties()
        {
            EditorGUILayout.PropertyField(decalProjectorProperty);

            EditorGUILayout.Space();

            base.drawAdditionalProperties();
        }

        protected override void drawModifiers()
        {
            base.drawModifiers();

            useAtlasProperty.boolValue = EditorGUILayout.ToggleLeft(new GUIContent(useAtlasProperty.displayName, useAtlasProperty.tooltip), useAtlasProperty.boolValue);

            if (useAtlasProperty.boolValue)
            {
                EditorGUI.indentLevel++;

                EditorGUILayout.PropertyField(atlasUVRegionProperty);
                EditorGUILayout.PropertyField(atlasTilesProperty);

                EditorGUI.indentLevel--;
            }

            fadeOverTimeProperty.boolValue = EditorGUILayout.ToggleLeft(new GUIContent(fadeOverTimeProperty.displayName, fadeOverTimeProperty.tooltip), fadeOverTimeProperty.boolValue);

            if (fadeOverTimeProperty.boolValue)
            {
                EditorGUI.indentLevel++;

                EditorGUILayout.PropertyField(lifeTimeProperty);
                EditorGUILayout.PropertyField(fadeTimeProperty);
                EditorGUILayout.PropertyField(fadeCurveProperty);

                EditorGUI.indentLevel--;
            }
        }

        private void OnSceneGUI()
        {
            DecalProjector decalProjector = decalProjectorProperty.objectReferenceValue as DecalProjector;

            if (useAtlasProperty.boolValue && decalProjector != null)
            {
                Rect rect = atlasUVRegionProperty.rectValue;

                Vector3 p = new Vector3(
                    decalProjector.size.x * decalProjector.transform.localScale.x - decalProjector.pivot.x * 2f,
                    decalProjector.size.y * decalProjector.transform.localScale.y - decalProjector.pivot.y * 2f,
                    decalProjector.size.z - decalProjector.pivot.z * 2f);

                p.x -= rect.x * (decalProjector.size.x * 2 * decalProjector.transform.localScale.x) / decalProjector.uvScale.x - (decalProjector.uvBias.x * (decalProjector.size.x * 2 * decalProjector.transform.localScale.x)) / decalProjector.uvScale.x;
                p.y -= rect.y * (decalProjector.size.y * 2 * decalProjector.transform.localScale.y) / decalProjector.uvScale.y - (decalProjector.uvBias.y * (decalProjector.size.y * 2 * decalProjector.transform.localScale.y)) / decalProjector.uvScale.y;

                Vector3 origin = decalProjector.transform.position - decalProjector.transform.rotation * p / 2f;
                Vector3 c1 = origin + decalProjector.transform.up * decalProjector.size.y * decalProjector.transform.localScale.y * rect.height / decalProjector.uvScale.y;
                Vector3 c2 = c1 + decalProjector.transform.right * decalProjector.size.x * decalProjector.transform.localScale.x * rect.width / decalProjector.uvScale.x;
                Vector3 c3 = c2 - decalProjector.transform.up * decalProjector.size.y * decalProjector.transform.localScale.y * rect.height / decalProjector.uvScale.y;

                Handles.color = Color.white;

                Handles.DrawLine(origin, c1, 1);
                Handles.DrawLine(c1, c2, 1);
                Handles.DrawLine(c2, c3, 1);
                Handles.DrawLine(c3, origin, 1);

                Handles.color = Color.red;

                Handles.DrawDottedLine(origin, c1, 4);
                Handles.DrawDottedLine(c1, c2, 4);
                Handles.DrawDottedLine(c2, c3, 4);
                Handles.DrawDottedLine(c3, origin, 4);

                Handles.color = Color.white;
                Vector2Int tiles = atlasTilesProperty.vector2IntValue;
                for (int x = 1; x < tiles.x; x++)
                {
                    float tx = (float)x / tiles.x;

                    Vector3 x1 = Vector3.Lerp(origin, c3, tx);
                    Vector3 x2 = Vector3.Lerp(c1, c2, tx);

                    Handles.DrawDottedLine(x1, x2, 4);
                }

                for (int y = 1; y < tiles.y; y++)
                {
                    float ty = (float)y / tiles.y;

                    Vector3 y1 = Vector3.Lerp(origin, c1, ty);
                    Vector3 y2 = Vector3.Lerp(c3, c2, ty);

                    Handles.DrawDottedLine(y1, y2, 4);

                }
            }
        }
    }
}

