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

// TODO - If this is actually useful, maybe worth buying "Editor PLUS" from asset store
public class PowerSelectionHistory : EditorWindow 
{
	[System.Serializable]
	class SelectedObject
	{
		public bool m_locked = false;
		public Object m_object = null;
	}
	
	[SerializeField] SelectedObject m_selectedObject = null;
	[SerializeField] List<SelectedObject> m_selectedObjects = new List<SelectedObject>();
	
	[SerializeField] Vector2 m_scrollPosition = Vector2.zero;
	
	[System.NonSerialized]
	GUIStyle m_lockButtonStyle;
			
	// Add menu item named "Super Animation Editor" to the Window menu
	[MenuItem("Window/Powerhoof/PowerSelectionHistory")]
	public static void ShowWindow() 
	{
		//Show existing window instance. If one doesn't exist, make one.
		EditorWindow.GetWindow(typeof(PowerSelectionHistory));
	}	
	
	void OnSelectionChange()
	{
		UpdateSelection();
	}
	
	void OnGUI () 		
	{		
		m_scrollPosition = EditorGUILayout.BeginScrollView(m_scrollPosition);
		
		GUILayout.Space(5);
		
		bool processingLocked = false;
		for ( int i = m_selectedObjects.Count-1; i >= 0; --i)
		{			
			if ( m_selectedObjects[i].m_locked == false )
			{
				if ( processingLocked )
				{
					// first non-locked. so add a space
					GUILayout.Space(5);
					processingLocked = false;					
				}
			}
			else
			{
				processingLocked = true;
			}
			
			LayoutItem(i, m_selectedObjects[i] );
		}
		GUILayout.Space(15);
		if ( GUILayout.Button("Clear", EditorStyles.miniButton) )
		{
			for ( int i = m_selectedObjects.Count-1; i >= 0; --i)
			{
				if ( m_selectedObjects[i].m_locked == false )
					m_selectedObjects.RemoveAt(i);
			}
		}
		
		EditorGUILayout.EndScrollView();	
			
		
	}
	
	void LayoutItem(int i, SelectedObject obj)
	{
				
		if (m_lockButtonStyle == null)
		{
			GUIStyle temp = "IN LockButton";
			m_lockButtonStyle = new GUIStyle(temp);
			m_lockButtonStyle.margin.top = 3;
			m_lockButtonStyle.margin.left = 10;
			m_lockButtonStyle.margin.right = 10;
		}
		
		GUIStyle style = EditorStyles.miniButtonRight;
		style.alignment = TextAnchor.MiddleLeft;
		
		if (  obj != null && obj.m_object != null)
		{ 
			GUILayout.BeginHorizontal();
			
			
			bool wasLocked = obj.m_locked;
			obj.m_locked = GUILayout.Toggle( obj.m_locked, GUIContent.none, m_lockButtonStyle);		
			if ( wasLocked != obj.m_locked )
			{
				
				m_selectedObjects.Remove(obj);					
				int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );
				
				if ( firstNonLocked >= 0 )				
				{
					m_selectedObjects.Insert(firstNonLocked, obj );
				}
				else 
				{
					m_selectedObjects.Add( obj );				
				}
				
			}
			
			if ( GUILayout.Button( "^", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(20) ) )
			{				
				m_selectedObject = obj;
				Selection.activeObject = obj.m_object;		
									
				// Move to top				
				m_selectedObjects.Remove(obj);					
				int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );					
				if ( obj.m_locked == false && firstNonLocked >= 0 )				
				{
					m_selectedObjects.Insert(firstNonLocked, obj );
				}
				else 
				{
					m_selectedObjects.Add( obj );				
				}
				
			}
			if ( obj == m_selectedObject ) 
			{
				GUI.enabled = false;
			}
				
			
			if ( GUILayout.Button( obj.m_object.name, style ) )
			{							
				m_selectedObject = obj;
				Selection.activeObject = obj.m_object;
			}
			GUI.enabled = true;
					
			
			GUILayout.EndHorizontal();
			
		}
	}
	
	void UpdateSelection()
	{
		if ( Selection.activeObject == null )
		{
			m_selectedObject = null;
		}
		else if ( m_selectedObject == null || m_selectedObject.m_object != Selection.activeObject )
		{
			SelectedObject obj = m_selectedObjects.Find(item => item.m_object == Selection.activeObject);
			
			m_selectedObject = obj;			
			
			if ( obj != null )
			{
				m_selectedObjects.Remove (obj);
				int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );
				if ( firstNonLocked >= 0 )				
					m_selectedObjects.Insert(firstNonLocked, obj );
				else 
					m_selectedObjects.Add( obj );
				m_selectedObject = obj;
			}
			else 
			{
				
				// Find first non-locked item
				obj = new SelectedObject() { m_object=Selection.activeObject };
				int firstNonLocked = ( m_selectedObjects.FindIndex(item => item.m_locked == true) );
				if ( firstNonLocked >= 0 )
				{
					m_selectedObjects.Insert(firstNonLocked, obj );
				}
				else 
				{
					m_selectedObjects.Add( obj );
				}				
				m_selectedObject = obj;
				//Debug.Log("Updated Selection. Count: "+ m_selectedObjects.Count + ". Selected: "+m_selectionIndex);
			
			}
			while ( m_selectedObjects.Count > 50 )
				m_selectedObjects.RemoveAt(0);
			
			Repaint();
			
		}
		
		
	}


}
