// Copyright (c) 2011-2017 Thinksquirrel Inc.

using UnityEngine;
using UnityEditor;
using Thinksquirrel.Thinkscroller;

namespace Thinksquirrel.ThinkscrollerEditor
{
    [CustomEditor(typeof(Parallax))]
    public class ParallaxInspector : Editor
    {
        private Parallax manager;

        private void OnEnable()
        {
            manager = target as Parallax;
        }

        public override void OnInspectorGUI()
        {
            CheckUndo();

            // Camera
            manager.SetParallaxCamera(
                (Camera) EditorGUILayout.ObjectField("Camera", manager.GetParallaxCamera(), typeof(Camera), true));

            // Scroll Axes
            manager.SetScrollConstraints(
                (ScrollConstraints) EditorGUILayout.EnumPopup("Scroll Axes", manager.GetScrollConstraints()));

            // Base Speed
            manager.SetBaseSpeed(EditorGUILayout.FloatField("Base Speed", manager.GetBaseSpeed()));

            // Automatic
            var auto = EditorGUILayout.Toggle("Automatic", manager.automatic);

            if (auto != manager.automatic)
            {
                manager.automatic = auto;
                Parallax.RefreshAllBillboards();
            }

            if (!manager.automatic)
            {
                manager.minSize = EditorGUILayout.Vector2Field("Min Size", manager.minSize);
                manager.maxSize = EditorGUILayout.Vector2Field("Max Size", manager.maxSize);
            }

            // Pixel size
            manager.pixelDensity = EditorGUILayout.FloatField("Pixel Density", manager.pixelDensity);

            // Autorefresh
            var autoRefresh = EditorGUILayout.Toggle("Auto-Refresh Meshes", manager.autoRefreshBillboards);

            if (autoRefresh != manager.autoRefreshBillboards)
            {
                manager.autoRefreshBillboards = autoRefresh;
                Parallax.RefreshAllBillboards();
            }

            // Reset All Layers
            GUI.color = Color.red;

            if (GUILayout.Button("Set All Layers to Auto"))
            {
                for (var s = 0; s < manager.Length; s++)
                {
                    manager[s].isAutoConfigured = true;
                }
            }

            GUI.color = Color.white;

            EditorGUILayout.Separator();
        }

        private void CheckUndo()
        {
            Undo.RecordObject(target, "Modify Parallax Manager");
        }
    }
}
