using UnityEngine;
using System.Collections;
using Janus;

public class CubeController : MonoBehaviour {

	public float moveSpeed = 5;               // speed of the cube
	Timeline<Vector3> cubePositionTimeline;   // timeline for synchronizing the position of the cube
	bool owner = false;                       // if true, this client created this cube and can select it, move it and delete it
	bool isSelected = false;                  // if true, this cube is selected

	
	// create a cube and a timeline to synchronize the position of the cube
	public void Create(uint cubeNumber, ushort cubeCreator)
	{		
		gameObject.name = "Cube" + cubeNumber + "-" + cubeCreator;

		// create a timeline
		cubePositionTimeline = TimelineManager.Default.Get<Vector3>(gameObject.name);
		// add a send filter to reduce the number of messages
		cubePositionTimeline.AddSendFilter(TimelineUtils.BuildDeltaRateFilter <Vector3>((x,y) => Vector3.Distance(x,y), ()=>0.05f, ()=>2.0f));
		
		// check if this client was the one who initiated the creation of this cube
		if (cubeCreator == Janus.TimelineClient.Index)
		{
			owner = true;
		    gameObject.renderer.material.color = Color.red;
		}
		else
		{
		    gameObject.renderer.material.color = Color.green;			
		}
	}
	
	// remove the timeline and destroy the game object
	public void OnDestroy()
	{		
		TimelineManager.Default.Remove(this.cubePositionTimeline);
	}
	
	// select/deselect a cube by clicking on it
	// you can only select a cube if you are the owner of that cube
	void OnMouseDown() {
        if (owner)
		{
			isSelected = !isSelected;					
			if (isSelected)
			{
			    gameObject.renderer.material.color = Color.yellow;
			}
			else
			{
			    gameObject.renderer.material.color = Color.red;
			}
		}
    }

	
	// Only the owner of a cube can move a cube and only of the cube is selected (yellow)
	void Update () {
		
		// if I am the owner of the cube, update its position and set the position in the timeline
		// the position is set in the timeline every frame, even if the position has not changed
		// the send filter on the timeline will only send updates over the network if the value changes
		if (owner)
		{			
			if (isSelected)
			{
				float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
				float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
				transform.Translate(h,v,0);
				
				// pressing the X key initiates a command to delete this cube (only if I am the owner and the cube is selected)
				if (Input.GetKeyDown(KeyCode.X))
				{		
			        // a delete command is composed of two parts separated by commas
			        // 1) the word "delete"
			        // 2) the name of the cube
					TimelineManager.Default.Get<string>("command")[0] = "delete," + this.name;			
				}
			
			}
			// write the cube position to the timeline
			if (cubePositionTimeline !=null)
			{
				cubePositionTimeline[0] = transform.position;
			}
						
		}
		// if I am not the owner of the cube, get the position from the timeline
		else
		{
			transform.position = cubePositionTimeline[-0.1f];
		}		
	
	}
}
