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

using Arbor;

namespace ArborEditor
{
	public class ArborFSMInternalInspector : Editor
	{
		private static readonly GUIContent _NameContent = new GUIContent( "Name" );

		ArborFSMInternal _StateMachine;

		[System.Reflection.Obfuscation(Exclude = true)]
		void OnEnable()
		{
			_StateMachine = target as ArborFSMInternal;
		}

		public override void OnInspectorGUI()
		{
			serializedObject.Update ();

			EditorGUILayout.PropertyField( serializedObject.FindProperty ("graphName"),_NameContent );
			EditorGUILayout.PropertyField( serializedObject.FindProperty ("playOnStart") );
			EditorGUILayout.PropertyField( serializedObject.FindProperty ("updateSettings") );

			serializedObject.ApplyModifiedProperties();
			
			if( GUILayout.Button ( "Open Editor" ) )
			{
				ArborEditorWindow.Open(_StateMachine);
			}
#if false
			foreach( StateBehaviour behaviour in _StateMachine.GetComponents<StateBehaviour>() )
			{
				State state = behaviour.state;
				if( state == null )
				{
					EditorGUILayout.BeginHorizontal();

					EditorGUILayout.LabelField( "Missing Behaviour : " + behaviour.GetType () );
					if( GUILayout.Button( "Delete" ) )
					{
						DestroyImmediate( behaviour );
					}

					EditorGUILayout.EndHorizontal();
				}
			}
#endif
		}

		[System.Reflection.Obfuscation(Exclude = true)]
		void OnDestroy()
		{
			if( !target && (object)_StateMachine != null && !Application.isPlaying )
			{
				_StateMachine.DestroySubComponents();
			}
		}
	}
}
