
using UnityEngine;
using System.Collections;

public class Controller_Switch : Widget
{
	
	public string symbol; // Name of the symbol send by this object
	public string input_keyName = null; // (optionnal) button name to trigger this object
	
	//TrackManager trackManager;
	bool value = false;
    bool targetValue = false;

	public Material on_material;
	public Material off_material;
	
	void OnMouseDown()
	{
		audio.Play ();
		switchMe ();
	}
	
	void switchMe()
	{
		value = ! value;
		//trackManager.emit ( new Event(symbol , value ? "1" : "0" ) );
		//renderer.material = renderer.materials[ value ? 1 : 0 ];
		renderer.material = value ? on_material : off_material;
        if (this.trackToReportTo != null && this.isAvailable == false && this.value == this.targetValue)
        {
            //when the target value is met- and a track is listening, let the listening track know
            this.trackToReportTo.handleWidgetUpdate(this);
        }
        else
        {
            //Debug.Log("Reporting Invalid Widget Use in Controller_Switch.switchMe");
            gameController.reportInvalidWidgetUse();
        }
	}
	
	new void Start()
	{
		base.Start();
		//trackManager = GameObject.FindObjectOfType<TrackManager> ();
		isOutput = false;
	}

    public override void Update()
	{
		if( input_keyName != null )
		if (Input.GetKeyDown (input_keyName))
			switchMe();
	}

    public override void createTargetValue(Track listeningTrack)
    {
        this.trackToReportTo = listeningTrack;
        //look for the value opposite the current value
        this.targetValue = !this.value; 
        this.isAvailable = false;
    }

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

    public override string getTargetValueInstruction()
    {
        return "flip " + this.symbol;
    }

	
}