#if UNITY_EDITOR

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(SoundEffect))]
public class SoundEffectEditor : Editor
{
    private SerializedProperty Clips;
    private SerializedProperty MinPitch;
    private SerializedProperty MaxPitch;
    private SerializedProperty MinVolume;
    private SerializedProperty MaxVolume;

    private AudioSource m_PreviewSource;

    void OnEnable()
    {
        Clips = serializedObject.FindProperty("m_Clips");
        MinPitch = serializedObject.FindProperty("m_MinPitch");
        MaxPitch = serializedObject.FindProperty("m_MaxPitch");
        MinVolume = serializedObject.FindProperty("m_MinVolume");
        MaxVolume = serializedObject.FindProperty("m_MaxVolume");

        m_PreviewSource = EditorUtility.CreateGameObjectWithHideFlags("Sound Effect Previewer", HideFlags.HideAndDontSave, typeof(AudioSource)).GetComponent<AudioSource>();
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(Clips, true);

        EditorGUILayout.Space();

        EditorGUILayout.PropertyField(MinPitch);
        EditorGUILayout.PropertyField(MaxPitch);

        EditorGUILayout.Space();

        EditorGUILayout.PropertyField(MinVolume);
        EditorGUILayout.PropertyField(MaxVolume);

        EditorGUILayout.Space();

        serializedObject.ApplyModifiedProperties();

        if(GUILayout.Button("Play")) ((SoundEffect)target).Play(m_PreviewSource);
    }
}

#endif
