using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Letter : FunObject {
	
	TextMesh _textMesh;
	tk2dSprite _sprite;
	GameObject _lock;
	tk2dSprite _lockSprite;
	public char Character;
	public bool MakesWord = true;	
	public bool InHorizontalWord = false;
	public bool InVerticalWord = false;
	public bool Dragging = false;
	public float Hover = 0.0f;
	public float HoverVal = 0.0f;
	public Vector3 LerpDest;
	public bool DoLerp = false;
	public float Delay = 0.0f;
	public bool Locked = false;
	float _velocity = 0.0f;
	
	public bool Fade = false;
	public bool Faded = false;
	
	public bool MovingLocked = false;
	
	Letter _cloneLetter = null;
	tk2dSprite _cloneSprite;
	GameObject _letterPrefab;
	public bool IsClone = true;
	
	public List<Word> Words = new List<Word>();
		
	
	
	
	// Use this for initialization
	void Awake () {
		_textMesh = transform.FindChild("Text").GetComponent<TextMesh>();
		Character = WordListManager.shared.GetRandomLetter();
		_textMesh.text = "" + Character;
		_sprite = GetComponent<tk2dSprite>();
		
		_lock = transform.FindChild("LockSprite").gameObject;
		_lockSprite = _lock.GetComponent<tk2dSprite>();
		
		float lockChance = Globals.Clicks * 0.005f;
		if(lockChance > 0.4f)
			lockChance = 0.4f;
		
		if(Random.value < lockChance)
			Locked = true;
	}
	
	public void SetCharacter(char c)
	{
		Character = c;
		_textMesh.text = "" + Character;
	}
	
	public void GenerateCloneLetter()
	{
		GameObject letterObj = Instantiate(Globals.LetterPrefab, position, Quaternion.identity) as GameObject;
		_cloneLetter = letterObj.GetComponent<Letter>();
		_cloneLetter.transform.parent = transform.parent;
		_cloneSprite = _cloneLetter.GetComponent<tk2dSprite>();
		_cloneSprite.gameObject.SetActive(false);
		_cloneLetter.SetCharacter(Character);
		IsClone = false;
	}
	
	void OnDestroy()
	{
		if(IsClone == false)
			Destroy(_cloneLetter.gameObject);
	}
	
	public void UpdateDrag()
	{
		if(IsClone)
			return;
		
		bool enable = false;
		if(Dragging)
		{
			if(!Globals.DragVertical)
			{
				if((lx + transform.parent.localPosition.x + Globals.TileSize) >= Globals.BoardWidth * Globals.TileSize)
				{
					_cloneSprite.gameObject.SetActive(true);
					_cloneLetter.transform.parent = transform.parent;
					_cloneLetter.lx = lx - (Globals.BoardWidth * Globals.TileSize);
					_cloneLetter.ly = ly;
					enable = true;
				}
				if((lx + transform.parent.localPosition.x) <= 0)
				{
					_cloneSprite.gameObject.SetActive(true);
					_cloneLetter.transform.parent = transform.parent;
					_cloneLetter.lx = lx + (Globals.BoardWidth * Globals.TileSize);
					_cloneLetter.ly = ly;
					enable = true;
				}
			}
			else
			{
				if((ly + transform.parent.localPosition.y + Globals.TileSize) >= Globals.BoardHeight * Globals.TileSize)
				{
					_cloneSprite.gameObject.SetActive(true);
					_cloneLetter.transform.parent = transform.parent;
					_cloneLetter.ly = ly - (Globals.BoardHeight * Globals.TileSize);
					_cloneLetter.lx = lx;
					enable = true;
				}
				if((ly + transform.parent.localPosition.y) <= 0)
				{
					_cloneSprite.gameObject.SetActive(true);
					_cloneLetter.transform.parent = transform.parent;
					_cloneLetter.ly = ly + (Globals.BoardHeight * Globals.TileSize);
					_cloneLetter.lx = lx;
					enable = true;
				}	
			}
			
			
			if(lx + transform.parent.localPosition.x + 1 >= Globals.BoardWidth * Globals.TileSize)
				lx -= (Globals.BoardWidth * Globals.TileSize);
			
			if((lx + transform.parent.localPosition.x + (Globals.TileSize)) - 1 < 0)
				lx += (Globals.BoardWidth * Globals.TileSize);
			
			if(ly + transform.parent.localPosition.y + 1 >= Globals.BoardHeight * Globals.TileSize)
				ly -= (Globals.BoardHeight * Globals.TileSize);
			
			if((ly + transform.parent.localPosition.y + (Globals.TileSize)) - 1 < 0)
				ly += (Globals.BoardHeight * Globals.TileSize);
			
		}

		if(enable == false)
		{
			if(_cloneSprite != null)
				_cloneSprite.gameObject.SetActive(false);
		}	
	}
	
	public void FinishDrag(LetterBoard l)
	{
		transform.parent = l.transform;	
		_cloneLetter.transform.parent = transform.parent;
		Dragging = false;	
	}
	
	// Update is called once per frame
	void Update () {
		if(IsClone)
			return;
		
		Color c = new Color(1.0f, 1.0f, 1.0f, 1.0f);
		for(int i = 0; i < Words.Count; i++)
		{
			c *= Words[i].color;
		}
		
		c = new Color((c.r * (1.0f - HoverVal)) + (HoverVal * 0.5f), Mathf.Min((c.g * (1.0f - HoverVal)) + (HoverVal * 1.0f), 1.0f), (c.b * (1.0f - HoverVal)) + (HoverVal * 0.5f), 1.0f);
		if(MovingLocked)
		{
			c.r *= 1.0f;
			c.g *= Locked ? 0.5f : 0.7f;
			c.b *= Locked ? 0.5f : 0.7f;
		}
		
		if(Fade)
		{
			c.a = 0;
			_textMesh.renderer.material.color = new Color(_textMesh.renderer.material.color.r, _textMesh.renderer.material.color.g, _textMesh.renderer.material.color.b, Mathf.Lerp(_textMesh.renderer.material.color.a, 0.0f, Time.deltaTime * 8.0f));
		}
		
		if(_sprite.color.a <= 0.01f)
			Faded = true;
		
		_sprite.color = Color.Lerp(_sprite.color, c, Time.deltaTime * 8.0f);
		_lockSprite.color = _sprite.color;
		_cloneSprite.color = _sprite.color;
		
		HoverVal = Mathf.Lerp(HoverVal, Hover, Time.deltaTime * 16.0f);
		
		UpdateDrag();
		
		if(Hover > 0.0f)
			Hover = 0.0f;

		_lock.SetActive(Locked);
		
		if(DoLerp)
		{
			if(Delay <= 0.0f)
			{
				Delay = 0.0f;
				//lposition = Vector3.Lerp(lposition, LerpDest, Time.deltaTime * 6.0f);
				if(lx > LerpDest.x)
				{
					_velocity += 0.5f;
				}
				
				if(lx - _velocity <= LerpDest.x)
				{
					lx = LerpDest.x;
					_velocity = 0;
					lposition = LerpDest;
					DoLerp = false;
				}
				else
					lx -= _velocity;
			}
			else
			{
				Delay -= 0.5f;
			}
				
		}
	}
}
