using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Track_DoThen : Track
{
    Widget input1Widget = null;   
    Widget input2Widget = null;
    public GameControler gameController;
    public WidgetManager widgetManager;
    public InstructionDisplay instructionDisplay;
    bool _conditionTriggered = false;
    bool input1TimerRunning = false;
    float input1Timer = 0.0f;
    bool input2TimerRunning = false;
    float input2Timer = 0.0f;

	public Track_DoThen(GameControler pGameController, bool pIsErrorEvent) :
        base(pGameController, pIsErrorEvent) 
	{
        cognitiveLoad = 3;
        gameController = pGameController;
        this.isErrorEvent = pIsErrorEvent;
        instructionDisplay = gameController.instructionDisplay;
        widgetManager = gameController.widgetManager;
      
        input1Widget = widgetManager.getRandomAvailableInputWidget();
        if (input1Widget != null)
        {
            input1Widget.createTargetValue(this);
        }
        else
        {
            valid = false;
        }

        input2Widget = widgetManager.getRandomAvailableInputWidget();
        if (input2Widget != null)
        {
            input2Widget.createTargetValue(this);
            valid = true;
            bool errorCodeExists = this.gameController.errorInstructionToCodes.ContainsKey(this.getInstruction());

            if (errorCodeExists)
            {
                this.isErrorEvent = true;
                gameController.errorCodesDisplay.addCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
            }
            else
            {
                this.isErrorEvent = false;
                this.instructionDisplay.setInstructionText(getInstruction());
            }
            input1TimerRunning = true;

        }
        else
        {
            valid = false;
        }

    }

    public override string getInstruction()
    {
        string text = input1Widget.getTargetValueInstruction();
        return text.ToString().ToUpper()[0] + text.Substring(1) + ", \nthen " + input2Widget.getTargetValueInstruction();
    }



    public override void finish()
    {
        //de register widgets
        if (this.isErrorEvent)
            gameController.errorCodesDisplay.removeCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
 
        this.input1Widget.clearTargetValue();
        this.input2Widget.clearTargetValue();
        input1TimerRunning = false;
        input2TimerRunning = false;
        this.finished = true;
    }


    public override void handleWidgetUpdate(Widget signallingWidget)
    {
        if (signallingWidget == input1Widget)
        {
            this._conditionTriggered = true;
            //set timer to look for input                   
            input2TimerRunning = true;
        }
        if (signallingWidget == input2Widget)
        {
            //if input 1 has already been triggered
            if (this._conditionTriggered)
            {
                input2TimerRunning = false;
                if (this.isErrorEvent)
                    gameController.errorCodesDisplay.removeCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
 
                this.gameController.reportTrackSuccess(true, this);
                this.finish();
            }
            else
            {
                this.gameController.reportInvalidWidgetUse();
            }
        }

    }

    public override void update()
    {

        if (input1TimerRunning)
        {
            input1Timer += Time.deltaTime;
            if (input1Timer > this.gameController.timeToWait)
            {
                gameController.reportTrackSuccess(false, this);
                input1TimerRunning = false;
                input1Timer = 0;
                this.finish();
            }

        }

        if (input2TimerRunning)
        {
            //Debug.Log("Track_DoWhen.Update() in outputTimer");
            input2Timer += Time.deltaTime;
            if (input2Timer > this.gameController.timeToWait)
            {
                input2TimerRunning = false;
                input2Timer = 0;
            }

        }
    }

}
