using UnityEngine;
using System.Collections;

public class Controller_Led : Widget
	{
	public string symbol;
	TrackManager trackManager;


	public Material on_material;
	public Material off_material;
    public bool on = false;
    public bool targetState = false;
    bool changed = false;


	new void Start ()
		{
		base.Start();
		//isOutput = true;
		}

	void OnDestroy ()
		{
		}

    void setState(bool newState)
    {
        this.on = newState;
        changed = true; //use this flag to trigger material change on update
        if (this.on == this.targetState)
        {
            if (this.trackToReportTo != null)
                this.trackToReportTo.handleWidgetUpdate(this);
        }
    }

    public override void handleSuccess()
    {
        //now turn the light off
        this.setState(!this.on);
    }

    public override void Update()
    {
        if (changed)
        {
            if (renderer.material != on_material && this.on)
            {
                renderer.material = on_material;
            }
            else
            {
                renderer.material = off_material;
            }
            changed = false;
        }
    }

    public override void setStateToTarget()
    {
        this.setState(this.targetState);
    }

    public override void createTargetValue(Track listeningTrack)
    {
        this.trackToReportTo = listeningTrack;
        this.targetState = !this.on;
        this.isAvailable = false;
    }

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

    public override string getTargetValueInstruction()
    {
        string text = this.symbol + " turns ";
        if (this.targetState) //target is ON
            text += "ON";
        else
            text += "OFF";

            return text;

    }

	}
