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

namespace Onward.CustomContent.EditorTools
{
    [CustomEditor(typeof(Onward.CustomContent.Assets.LevelDescriptor))]
    public class LevelDescriptorEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            var levelDesc = target as Onward.CustomContent.Assets.LevelDescriptor;

            DrawDefaultInspector();

            GUILayout.Space(15);

            if (levelDesc.SharedSettings == null)
            {
                EditorGUILayout.HelpBox("Press button below to create shared settings file. This file will hold all settings that are shared across platforms.", MessageType.Error);
            }

            GUILayout.Space(15);

            if (GUILayout.Button("Create shared settings"))
            {
                SharedLevelSettings newSettings = ScriptableObject.CreateInstance<SharedLevelSettings>();

                // Migrate all the settings (if any)
                newSettings.DefaultReverbPreset = levelDesc.DefaultReverbPreset;

                newSettings.MapEnvType = levelDesc.MapEnvType;
                newSettings.MapSize = levelDesc.MapSize;
                newSettings.TimeOfDay = levelDesc.TimeOfDay;
                newSettings.PPProfileId = levelDesc.PPProfileId;

                newSettings.Title = levelDesc.Title;

                newSettings.Description = levelDesc.Description;
                newSettings.Thumbnail = levelDesc.Thumbnail;
                newSettings.PublishedItemId = levelDesc.PublishedItemId;
                newSettings.DisplayVersion = levelDesc.DisplayVersion;

                string path = $"Assets/Data/Shared/";

                System.IO.Directory.CreateDirectory(path);

                AssetDatabase.CreateAsset(newSettings, System.IO.Path.Combine(path, $"SharedLevelSettings_{UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name}.asset"));

                levelDesc.SharedSettings = newSettings;

                EditorUtility.SetDirty(levelDesc);

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