/*
Author:		Jan Lygnos
Date:		12.8.2019
Last Edit:  13.8.2019
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

/// <summary>
/// Audio Managers editor layout.
/// </summary>
[CustomEditor(typeof(AudioManager))]
public class AudioManagerEditor : Editor {

    private AudioManager audioManager;
    private SerializedObject audioManagerSerialized;
    private SerializedProperty playingSources;
    private SerializedProperty fadeDuration;
    private SerializedProperty volumeChangePerSecond;

    private void OnEnable() {
        audioManager = (AudioManager)target;
        audioManagerSerialized = new SerializedObject(audioManager);
    }

    public override void OnInspectorGUI() {
        audioManagerSerialized.Update();
        playingSources = audioManagerSerialized.FindProperty("PlayingAudioSources");
        fadeDuration = audioManagerSerialized.FindProperty("FadeDuration");
        volumeChangePerSecond = audioManagerSerialized.FindProperty("VolumeChangePerSecond");
        EditorGUILayout.Space();
        GUIStyle style = new GUIStyle(GUI.skin.label);
        style.fontStyle = FontStyle.Bold;
        GUILayout.Label("Currently playing:", style);
        EditorGUILayout.Space();
        DisplayElements(playingSources);
        audioManagerSerialized.ApplyModifiedProperties();
        serializedObject.ApplyModifiedProperties();
    }

    private void DisplayElements(SerializedProperty list) {
        GUIStyle style = new GUIStyle(GUI.skin.label);
        int size = list.arraySize;
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                SerializedProperty index = list.GetArrayElementAtIndex(i);

                PlayingAudio playingAudio = audioManager.PlayingAudioSources[i];
                SerializedProperty sourceProperty = index.FindPropertyRelative("Source");
                AudioSource source = playingAudio.Source;

                if (source) {
                    Rect horizontal = EditorGUILayout.BeginHorizontal();

                    EditorGUI.ProgressBar(
                    new Rect(horizontal.x + 90, horizontal.y, horizontal.x + 90, 15f),
                    source.loop ? 1 : (Time.realtimeSinceStartup - playingAudio.Time) / source.clip.length,
                    source.loop ? (playingAudio.IsAtmospheric ? "atmospheric" : "looping") : "");

                    EditorGUILayout.PropertyField(sourceProperty, GUILayout.Width(500));
                    EditorGUILayout.EndHorizontal();
                }
            }
        } else {
            style.fontStyle = FontStyle.Normal;
            GUILayout.Label("Nothing is playing...", style);
        }


        EditorGUILayout.Space();
        style.fontStyle = FontStyle.Bold;
        GUILayout.Label("Atmospheric Audio Settings", style);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.Slider(fadeDuration, -5f, 5f);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.IntSlider(volumeChangePerSecond, 0, 60);
        EditorGUILayout.EndHorizontal();
    }
}