using UnityEngine;
using System.Collections;
using System;


public class CharacterManager : MonoBehaviour {

	public Transform m_tOutPos;

	[Serializable]
	public class CharacterData
	{
		[SerializeField]
		public CHARACTER_NAME m_eCharacterName;
		private float	m_fRestStartTime;
		public bool m_bIsResting = false;
		public Transform m_tCharPos;
		public GameObject m_CharSprite;
		public AudioSource m_EnterSound;
		public AudioSource m_GOSound;
		public AudioSource[] m_ComboSound;

		public bool m_bEntering = false;
		public bool m_bExiting = false;


		public void StartRest()
		{
			m_fRestStartTime = Time.time;
			m_bIsResting = true;
		}

		public void Update()
		{

			if( GameStateManager.m_Instance.m_eGameState == GlobalVars.GAME_STATE.GAME_OVER )
				return;

			if( m_fRestStartTime + CharacterManager.m_Instance.m_RestTime <= Time.time )
			{
				if( m_bIsResting )
					Debug.Log(this.m_eCharacterName + " is available");
				m_bIsResting = false;
			}


		}
	}

	public enum CHARACTER_NAME
	{
		LE_CHINOIS, // ralentit tout
		LA_FILLE,
		LE_DALTONIEN, // 2 couleurs
		LE_VIEUX // switch la ligne selectionner toujours dans le sens  à partir d'un temps donné;
		
	}

	public CharacterData[] characterSet;

	public static CharacterManager m_Instance;
	private int m_iCurrentCharacterIndex = 0;
	public float m_RestTime;


	public float m_fFilleSpeedRatio = 1.5f;
	public float m_fFillePointsRatio = 1.5f;
	public float m_fGirlSpawnInterval = 1.5f;
	public int m_iFillePointsPerBlocks = 200;
	
	public float m_fChinoisSpeedRatio = 0.5f;
	public float m_fChinoisPointsRatio = 0.5f;
	public float m_fChinoisSpawnInterval = 3.0f;
	public int m_iChinoisPointsPerBlocks = 50;

	public float m_fCharEnteringSpeed = 20;

	public MainBoard m_tBoard;
	

	void Awake()
	{
		m_Instance = this;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if(Inputs.m_bLTIsJustPressed || Input.GetKeyDown(KeyCode.RightControl))
		{
			SwitchCharacter(GlobalVars.Direction.LEFT);
		}
		if(Inputs.m_bRTIsJustPressed || Input.GetKeyDown(KeyCode.Keypad0))
		{
			SwitchCharacter(GlobalVars.Direction.RIGHT);
		}

		//update all character state
		foreach (CharacterData item in characterSet) 
		{				
			item.Update();
		}
	
	}


	public void ActivateCharacterByIndex(int iIndex)
	{


		if( iIndex != m_iCurrentCharacterIndex )
		{
			DesactivateCharacterPassive();
		}

		m_iCurrentCharacterIndex = iIndex;

		if(characterSet[m_iCurrentCharacterIndex].m_EnterSound != null)
		{
			characterSet[m_iCurrentCharacterIndex].m_EnterSound.Play();
		}

		characterSet[m_iCurrentCharacterIndex].m_bEntering = true;
		
		switch (characterSet[m_iCurrentCharacterIndex].m_eCharacterName)
		{
		case CHARACTER_NAME.LA_FILLE :
			MainBoard.SetFallSpeedRatio(m_fFilleSpeedRatio);
			MainBoard.SetPointsPerBlocks(m_iFillePointsPerBlocks);
			MainBoard.SetSpawnInterval(m_fGirlSpawnInterval);
			break; 
			
		case CHARACTER_NAME.LE_CHINOIS :
			MainBoard.SetFallSpeedRatio(m_fChinoisSpeedRatio);
			MainBoard.SetPointsPerBlocks(m_iChinoisPointsPerBlocks);
			MainBoard.SetSpawnInterval(m_fChinoisSpawnInterval);
			break; 
			
		case CHARACTER_NAME.LE_DALTONIEN :
			MainBoard.m_bColorBlindActive = true;
			m_tBoard.SwitchToColorBlind();
			break; 
			
		case CHARACTER_NAME.LE_VIEUX :
			
			break; 
		}
	}

	void DesactivateCharacterPassive()
	{
		Debug.Log("Desactivating " + characterSet[m_iCurrentCharacterIndex].m_eCharacterName + " passive");

		MainBoard.ReinitializeRatios();

		characterSet[m_iCurrentCharacterIndex].m_bExiting = true;

		switch (characterSet[m_iCurrentCharacterIndex].m_eCharacterName)
		{
		case CHARACTER_NAME.LA_FILLE :

			break; 
			
		case CHARACTER_NAME.LE_CHINOIS :

			break; 
			
		case CHARACTER_NAME.LE_DALTONIEN :
			MainBoard.m_bColorBlindActive = false;
			MainBoard.m_tMainBoard.SwitchToNormalColor();
			

			break; 
			
		case CHARACTER_NAME.LE_VIEUX :
			
			break; 
		}
	}

	public void PlayDieSound()
	{
		if(characterSet[m_iCurrentCharacterIndex].m_GOSound != null)
		{
			characterSet[m_iCurrentCharacterIndex].m_GOSound.Play();
		}
	}

	public void PlayComboSound(int comboVal)
	{
		if( characterSet[m_iCurrentCharacterIndex].m_ComboSound.Length < 3 )
			return;
		switch (comboVal) 
		{
		
		case 2 :
			
			if( characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 0 ] != null )
				characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 0 ].Play();
			break;

		case 4 :
			
			if( characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 1 ] != null )
				characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 1 ].Play();
			break;

		case 6 :
			
			if( characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 2 ] != null )
				characterSet[m_iCurrentCharacterIndex].m_ComboSound[ 2 ].Play();
			break;
		default :
			return;
		}


	}

	public void UpdateCharacterEnter()
	{
		for(int i = 0; i < 3; i++)
		{
			if( characterSet[i].m_bEntering )
			{
				characterSet[i].m_CharSprite.transform.position = Vector3.Lerp(characterSet[i].m_CharSprite.transform.position, characterSet[i].m_tCharPos.transform.position, m_fCharEnteringSpeed * Time.deltaTime);
				if( ( characterSet[i].m_CharSprite.transform.position.x - characterSet[i].m_tCharPos.transform.position.x ) *  ( characterSet[i].m_CharSprite.transform.position.x - characterSet[i].m_tCharPos.transform.position.x ) < 0.01f)
				{
					characterSet[i].m_bEntering = false;
				}
			}
			else if( characterSet[i].m_bExiting )
			{
				characterSet[i].m_CharSprite.transform.position = Vector3.Lerp(characterSet[i].m_CharSprite.transform.position, m_tOutPos.transform.position, m_fCharEnteringSpeed * Time.deltaTime);
				if( ( characterSet[i].m_CharSprite.transform.position.x - m_tOutPos.transform.position.x) *  ( characterSet[i].m_CharSprite.transform.position.x - m_tOutPos.transform.position.x ) < 0.01f)
				{
					characterSet[i].m_bExiting = false;
				}
			}
		}
	}

	void ActivateCharacterPassive()
	{
		Debug.Log("Activating " + characterSet[m_iCurrentCharacterIndex].m_eCharacterName + " passive");

		characterSet[m_iCurrentCharacterIndex].m_bEntering = true;

		switch (characterSet[m_iCurrentCharacterIndex].m_eCharacterName)
		{
			case CHARACTER_NAME.LA_FILLE :
				MainBoard.SetFallSpeedRatio(m_fFilleSpeedRatio);
				MainBoard.SetPointsPerBlocks(m_iFillePointsPerBlocks);
				MainBoard.SetSpawnInterval(m_fGirlSpawnInterval);
				break; 

			case CHARACTER_NAME.LE_CHINOIS :
				MainBoard.SetFallSpeedRatio(m_fChinoisSpeedRatio);
				MainBoard.SetPointsPerBlocks(m_iChinoisPointsPerBlocks);
				MainBoard.SetSpawnInterval(m_fChinoisSpawnInterval);
				break; 

			case CHARACTER_NAME.LE_DALTONIEN :
				MainBoard.m_bColorBlindActive = true;
			m_tBoard.SwitchToColorBlind();
				break; 

			case CHARACTER_NAME.LE_VIEUX :
				
				break; 
		}
	}

	void SwitchCharacter(GlobalVars.Direction eDir)
	{
		int _index = m_iCurrentCharacterIndex;
		_index += (int) eDir;
		if( _index > characterSet.Length - 1 )
			_index = 0;
		if( _index < 0 )
			_index = characterSet.Length - 1;

/*		while( _index != m_iCurrentCharacterIndex && characterSet[_index].m_bIsResting )
		{
			_index++;
			if( _index > characterSet.Length - 1 )
				_index = 0;
		}		
*/		
		if( !characterSet[_index].m_bIsResting )
		{
			if( m_iCurrentCharacterIndex != _index )
			{
				characterSet[m_iCurrentCharacterIndex].StartRest();
			}
			if(_index != m_iCurrentCharacterIndex)		//si on switch reelement de character, on desactive son passif, on switch et on active le suivant
			{
				DesactivateCharacterPassive();
				m_iCurrentCharacterIndex = _index;
				ActivateCharacterPassive();

				if(characterSet[m_iCurrentCharacterIndex].m_EnterSound != null)
				{
					characterSet[m_iCurrentCharacterIndex].m_EnterSound.Play();
				}
			}

			Debug.Log(characterSet[m_iCurrentCharacterIndex].m_eCharacterName );
		}
	}

}
