using UnityEngine;
using System.Collections;

public class Controller_Slider : Widget
{
	
	//TrackManager trackManager;

	int value = 1;
    public TextMesh textField;
	public int minValue = 1;
	public int maxValue = 5;
    public int targetValue = -1;

	public GameObject button;
	public string symbol;

	float last_mouse_pos = 0;
	float accum_pos = 0;
	float max_accum = 35;

	float motionRange = 4.28f;

	Vector3 initial_position;

	void OnMouseDown()
	{
		last_mouse_pos = Input.mousePosition.y;
	}

	void OnMouseUp()
	{
		audio.Play ();
		if (this.trackToReportTo != null)
		{
			if (this.targetValue != -1 && this.value == this.targetValue)
			{
				//when the target value is met, let the parent track know
				this.trackToReportTo.handleWidgetUpdate(this);
			}
		}
		else
		{
			//Debug.Log("Reporting Invalid Widget Use in Controller_Slider.setValue");
			gameController.reportInvalidWidgetUse();
		}
	}
	
	void OnMouseDrag()
	{
		
		float delta = Input.mousePosition.y - last_mouse_pos;
		last_mouse_pos = Input.mousePosition.y;
		accum_pos += delta;
		
		while( accum_pos > max_accum )
		{
			accum_pos -= max_accum;
			setValue( value + 1 );
		}
		
		while( accum_pos < - max_accum )
		{
			accum_pos += max_accum;
			setValue( value - 1 );
		}
	}
	
	new void Start ()
		{
		base.Start();
		initial_position = button.transform.localPosition;
		isOutput = false;
        this.textField.text = this.value.ToString();
		}
	
	void setValue( int v )
		{
		if( v > maxValue )
			v = maxValue;

		if( v < minValue )
			v = minValue;

		if( value == v )
			return;
		value = v;



		float av = (float)( value - minValue ) / ( maxValue - minValue );

		Vector3 newPos = new Vector3(
			  initial_position.x + ( (av < 0.5f) ? ( 0 ) : ( (av-0.5f) * 0.15f ) )
		    , initial_position.y 
			, initial_position.z + av * motionRange );

		button.transform.localPosition = newPos;

		//trackManager.emit ( new Event( symbol , value.ToString() ) );
		/* MOVED TO ONMOUSEUP
        if (this.trackToReportTo != null)
        {
            if (this.targetValue != -1 && this.value == this.targetValue)
            {
                //when the target value is met, let the parent track know
                this.trackToReportTo.handleWidgetUpdate(this);
            }
        }
        else
        {
            //Debug.Log("Reporting Invalid Widget Use in Controller_Slider.setValue");
            gameController.reportInvalidWidgetUse();
        }
		*/
        textField.text = this.value.ToString();
	}
	
	public override void Update()
    {
    }

    override public void createTargetValue(Track listeningTrack)
    {
        this.trackToReportTo = listeningTrack;
        //make sure different than current
        int candidateValue = -1;
        while (candidateValue == this.value || candidateValue == -1)
        {
            candidateValue = rnd.Next(this.minValue, this.maxValue);
        }
        this.targetValue = candidateValue;
        this.isAvailable = false;
    }

    override public void clearTargetValue()
    {
        this.trackToReportTo = null;
        this.isAvailable = true;
    }

    override public string getTargetValueInstruction()
    {
        return "set " + this.symbol + " to " + this.targetValue;
    }
	
}

