﻿#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
public class OnwardProjectSettings : EditorWindow
{
    static OnwardProjectSettings m_window;

    static OnwardProjectSettings()
    {
        EditorApplication.update += InitWindow;
    }

    private void OnEnable()
    {
        VerifyTags();
    }

    static void InitWindow()
    {
        m_window = GetWindow<OnwardProjectSettings>(true, "Onward Project Settings");
        m_window.minSize = new Vector2(320, 440);

        EditorApplication.update -= InitWindow;
    }

    public void OnGUI()
    {
        int errors = 0;

        EditorGUILayout.HelpBox("The following settings are required for your map to work properly:", MessageType.Warning);

        if (PlayerSettings.virtualRealitySupported != true)
        {
            ShowSetting("Virtual Reality Enabled: True", delegate { PlayerSettings.virtualRealitySupported = true; });

            ++errors;
        }

#if false
        if (PlayerSettings.stereoRenderingPath != StereoRenderingPath.SinglePass)
        {
            ShowSetting("Rendering Path: Single Pass", delegate { PlayerSettings.stereoRenderingPath = StereoRenderingPath.SinglePass; });
            ++errors;
        }
#endif

        if (PlayerSettings.colorSpace != ColorSpace.Linear)
        {
            ShowSetting("ColorSpace: Linear\n(Might take a while due to asset reimport!)", delegate { PlayerSettings.colorSpace = ColorSpace.Linear; });
            ++errors;
        }

#if !UNITY_ANDROID
        if (Time.fixedDeltaTime > 0.012f)
        {
            // We change this silently.
            // Unity doesn't seem to set this dirty and there doesn't seem to be a way to set this dirty manually.

            Time.fixedDeltaTime = 1f / 90f;
        }
#else
        if (Time.fixedDeltaTime > 0.014f)
        {
            // We change this silently.
            // Unity doesn't seem to set this dirty and there doesn't seem to be a way to set this dirty manually.

            Time.fixedDeltaTime = 1f / 72f;
        }
#endif

        VerifyTags();
        // Shader instancing stripping doesn't seem to be exposed :-/

        if (errors <= 0)
            Close();
    }

    void ShowSetting(string label, System.Action apply)
    {
        GUILayout.Label(label);

        GUILayout.BeginHorizontal();

        if (GUILayout.Button("Apply"))
        {
            apply.Invoke();
        }

        GUILayout.EndHorizontal();
    }

    static void VerifyTags()
    {
        AddTag("Snow");
    }

    static void AddTag(string tag)
    {
        var tagManagerAsset = AssetDatabase.LoadMainAssetAtPath("ProjectSettings/TagManager.asset");

        if (tagManagerAsset != null)
        {
            SerializedObject tagManager = new SerializedObject(tagManagerAsset);

            SerializedProperty tags = tagManager.FindProperty("tags");

            int numTags = tags.arraySize;

            for (int i = 0; i < tags.arraySize; i++)
            {
                var existingTag = tags.GetArrayElementAtIndex(i);

                if (existingTag.stringValue == tag)
                    return;
            }

            tags.InsertArrayElementAtIndex(numTags);
            tags.GetArrayElementAtIndex(numTags).stringValue = tag;

            tagManager.ApplyModifiedProperties();
            tagManager.Update();
        }
    }
}
#endif