﻿#if !BOXOPHOBIC_DEVELOPMENT

using UnityEngine;
using UnityEditor;
using System.IO;
using Boxophobic.Utility;

namespace AtmosphericHeightFog
{
    [InitializeOnLoad]
    public class HeightFogInstaller
    {
        static HeightFogInstaller()
        {
            EditorApplication.update += OnInit;
        }

        static void OnInit()
        {
            EditorApplication.update -= OnInit;

            var symbol = "ATMOSPHERIC_HEIGHT_FOG";
            var installer = AssetDatabase.GUIDToAssetPath("db20ad54d59bdbb48bd7d12f978efb0c");
            var version = AssetDatabase.GUIDToAssetPath("41b457a34c9fb7f45a332c79a90945b5");

            var assetFolder = version.Replace("/Core/Editor/Version.asset", "");
            var userFolder = BoxoUtils.GetUserFolder();

            var assetVersion = SettingsUtils.LoadSettingsData(version, "00");

            var projectData = BoxoUtils.GetProjectData();

            if (projectData.pipeline != "Standard")
            {
                if (projectData.isSupported)
                {
                    if (projectData.isTechRelease)
                    {
                        var message = "The current Unity version is not officially supported with the " + projectData.pipeline + " Render Pipeline! Using Atmospheric Height Fog is only recommended with LTS releases!";

                        EditorUtility.DisplayDialog("Atmospheric Height Fog", message, "Got It!");
                    }

                    var pipelinePackagePath = assetFolder + "/Core/Pipelines/" + projectData.pipeline + " " + projectData.package + ".unitypackage";

                    if (File.Exists(pipelinePackagePath))
                    {
                        AssetDatabase.ImportPackage(pipelinePackagePath, false);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();

                        Debug.Log("<b>[Atmospheric Height Fog]</b> " + projectData.pipeline + " Render Pipeline " + projectData.package + " support is imported for The Visual Engine!");
                    }
                    else
                    {
                        Debug.Log("<b>[Atmospheric Height Fog]</b> " + projectData.pipeline + " Render Pipeline " + projectData.package + " package cannot be found! Make sure to re-import the asset and keep the .unitypackage files!");
                    }
                }
                else
                {
                    var message = "The current Unity version is not supported! Atmospheric Height Fog only supports " + projectData.minimum + " or higher with the " + projectData.pipeline + " Render Pipeline!";
                        
                    EditorUtility.DisplayDialog("Atmospheric Height Fog", message, "Got It!");
                    Debug.Log("<b>[Atmospheric Height Fog]</b> " + message);

                }
            }
            else
            {
                Debug.Log("<b>[Atmospheric Height Fog]</b> " + projectData.pipeline + " Render Pipeline support is imported for Atmospheric Height Fog!");
            }

            SettingsUtils.SaveSettingsData(userFolder + "/User/Atmospheric Height Fog/Pipeline.asset", projectData.pipeline);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            BoxoUtils.SetDefineSymbol(symbol);

            AssetDatabase.DeleteAsset(installer);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}

#endif


