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


public class Track_DoWhen : Track
{
	Widget outputWidget = null;   
	Widget inputWidget = null;
	public WidgetManager widgetManager;
    public GameControler gameController;
    public InstructionDisplay instructionDisplay;
    bool _conditionTriggered = false;
    bool responseTimerRunning = false;
    float responseTimer = 0.0f;
    bool outputTimerRunning = false;
    float outputTimer = 0.0f;
    int outputDelay = 0;

    public Track_DoWhen(GameControler pGameController, bool pIsErrorEvent) :
        base(pGameController, pIsErrorEvent)
	{
        cognitiveLoad = 5;
        gameController = pGameController;
        this.isErrorEvent = pIsErrorEvent;
        instructionDisplay = gameController.instructionDisplay;
        widgetManager = gameController.widgetManager;
    
        outputWidget = widgetManager.getRandomAvailableOutputWidget();
		if (outputWidget != null)
		{
            outputWidget.createTargetValue(this);
            
            //Debug.Log("Output Widget Successfully returned in Track_DoWhen");
		} 
		else 
		{
            Debug.Log("Output Widget Failed to returned in Track_DoWhen");
			valid = false;
            return;
		}
		
		inputWidget = widgetManager.getRandomAvailableInputWidget();
		if (inputWidget != null)
		{
            inputWidget.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());
            }

            //pick a delay for the output occurring and start the timer running
            this.outputDelay = Random.Range(3, 20);
            outputTimerRunning = true;
            //Debug.Log("starting up outputTimer in Track_DoWhen with outputDelay of " + this.outputDelay);            
		}
		else
		{
            Debug.Log("Input Widget Failed to returned in Track_DoWhen");
			valid = false;
		}
		
		
	}
	
	 public override string getInstruction()
    {
        
         //TODO: there is possibly a bug where this is called with null values for output or input widgets
        Debug.Log("Track_DoWhen.getInstructions()");
        Debug.Log(outputWidget.getTargetValueInstruction());
        Debug.Log(inputWidget.getTargetValueInstruction());
        return "When " + outputWidget.getTargetValueInstruction() + ",\n " + inputWidget.getTargetValueInstruction();
    }

     public override void finish()
     {
         //de register widgets
         if (this.isErrorEvent)
             gameController.errorCodesDisplay.removeCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
 
         this.inputWidget.clearTargetValue();
         this.outputWidget.clearTargetValue();
         responseTimerRunning = false;
         outputTimerRunning = false;
         this.finished = true;
     }


     public override void handleWidgetUpdate(Widget signallingWidget)
     {
         if (signallingWidget == outputWidget)
         {
             this._conditionTriggered = true;
             //set timer to look for input                   
             responseTimerRunning = true;                                     
         }
         if (signallingWidget == inputWidget)
         {
             //if output has been triggered
             if (this._conditionTriggered)
             {
                 responseTimerRunning = false;
                 this.outputWidget.handleSuccess();
                 if (this.isErrorEvent)
                     gameController.errorCodesDisplay.removeCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
                 this.gameController.reportTrackSuccess(true, this);
                 this.finish();
             }
             else
             {
                 //Debug.Log("Reporting Invalid Widget Use in Track_DoWhen.handleWidgetUpdate");
                 this.gameController.reportInvalidWidgetUse();
             }
         }
 
     }

     public override void update()
     {
         
         if (responseTimerRunning)
         {
             responseTimer += Time.deltaTime;
             if (responseTimer > this.gameController.timeToWait)
             {
                 //Debug.Log("_timer_Elapsed in Track_Do");
                 gameController.reportTrackSuccess(false, this);
                 responseTimerRunning = false;
                 responseTimer = 0;
                 this.finish();
             }
             
         }

         if (outputTimerRunning)
         {
             //Debug.Log("Track_DoWhen.Update() in outputTimer");
             outputTimer += Time.deltaTime;
             if (outputTimer > this.outputDelay)
             {
                 Debug.Log("Output Timer Ended");
                 this.outputWidget.setStateToTarget();
                 outputTimerRunning = false;
                 outputTimer = 0;
             }
             
         }
     }
	
}
