//#define TEMPLY_COMMENT_PARAMDEBUG
//#define TEMPLY_COMMENT
using UnityEngine;
using UnityEditor;

namespace X2Engine
{
    [CustomEditor(typeof(EnvironmentSystem))]
    public class EnvironmentEditor : Editor
    {
        [MenuItem("GameObject/Misc/Create Environment")]
        static public void CreateEnvironmentGameObject()
        {
            GameObject envGo = new GameObject();

            envGo.name = "Environment";
            var env = envGo.AddComponent<EnvironmentSystem>();
            //FogVolume.AddComponent<FogVolume>();

            GameObject sunGo = new GameObject();
            sunGo.name = "Sun";
            var light = sunGo.AddComponent<Light>();
            light.type = LightType.Directional;
            light.shadows = LightShadows.Soft;
            sunGo.transform.SetParent(envGo.transform);
            env.m_sun = light;

            //var lensFlare = sunGo.AddComponent<LensFlare>();
            //env.m_lensFlare = lensFlare;

            Selection.activeObject = envGo;
            if (UnityEditor.SceneView.currentDrawingSceneView)
                UnityEditor.SceneView.currentDrawingSceneView.MoveToView(envGo.transform);
        }

        EnvironmentSystem m_target;
        EnvProfileDrawer m_Drawer;

        void OnEnable()
        {
            m_target = target as EnvironmentSystem;
            m_Drawer = new EnvProfileDrawer(this);

            RefeshProfile();
        }

        void OnDisable()
        {
            m_Drawer = null;
        }

        void RefeshProfile()
        {
            if (m_target.sharedProfile)
                m_Drawer.Init(m_target.sharedProfile as EnvironmentProfile, new SerializedObject(m_target.sharedProfile));
        }

        public override bool RequiresConstantRepaint()
        {
            return true;
        }

        private float m_flashLightingSec;
        private float m_flashLightingIntensity;
        private int m_lightingEffectsGroup = 0;
        private float m_lightingEffectsDir = 0;
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            base.OnInspectorGUI();
            m_target.m_testDefaultFog = EditorGUILayout.Toggle("测试默认雾", m_target.m_testDefaultFog);
#if TEMPLY_COMMENT_PARAMDEBUG
            X2Engine.ParamDebugClient.OnGUI();
#endif
            int buttonWidth = 145;
            //float indentOffset = EditorGUI.indentLevel * 15f;
            var lineRect = GUILayoutUtility.GetRect(1, EditorGUIUtility.singleLineHeight);
            var buttonCopyRect = new Rect(lineRect.x, lineRect.y, buttonWidth, lineRect.height);

#if TEMPLY_COMMENT
            bool multiEdit = false;
            using (new EditorGUI.DisabledScope(multiEdit))
            {
                if (GUI.Button(buttonCopyRect, EditorUtilities.GetContent("Clone Profile")))
                {
                    // Duplicate the currently assigned profile and save it as a new profile
                    var origin = m_target.sharedProfile;
                    var asset = DuplicateProfile.Duplicate(origin);
                    m_target.sharedProfile = asset;
                }
            }
#endif
            EditorGUILayout.Space();

            m_flashLightingSec = EditorGUILayout.FloatField("Sec", m_flashLightingSec);
            m_flashLightingIntensity = EditorGUILayout.FloatField("Intensity", m_flashLightingIntensity);
            if (GUILayout.Button("测试闪电"))
            {
                m_target.TiggerFlashLighting(m_flashLightingSec, m_flashLightingIntensity);
            }
            EditorGUILayout.Space();

            m_lightingEffectsGroup = EditorGUILayout.IntField("GroupID", m_lightingEffectsGroup);
            m_lightingEffectsDir = EditorGUILayout.Slider("Direction", m_lightingEffectsDir, 0f, 360f);
            if (GUILayout.Button("测试闪电特效"))
            {
                m_target.TiggerLightingEffects(m_lightingEffectsGroup, m_lightingEffectsDir);
            }
            EditorGUILayout.Space();

            if (m_target.sharedProfile != m_Drawer.Asset)
            {
                RefeshProfile();
                m_target.sharedProfile = m_target.sharedProfile;
            }

            using (var scope = new EditorGUI.ChangeCheckScope())
            {
                m_Drawer.OnGUI();
                if (scope.changed)
                {
                    m_target.sharedProfile = m_target.sharedProfile;
                }
            }
            Event e = Event.current;
            if (e.type == EventType.KeyUp && e.control)
            {
                if (e.keyCode == KeyCode.Z || e.keyCode == KeyCode.Y)
                {
                    m_target.sharedProfile = m_target.sharedProfile;
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
    }

}
