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



public class Track_Do : Track
{ 
    Widget inputWidget = null;
    public WidgetManager widgetManager;
    public InstructionDisplay instructionDisplay;
    public GameControler gameController;
    bool responseTimerRunning = false;
    float responseTimer = 0.0f;    
   


	public Track_Do(GameControler pGameController, bool pIsErrorEvent) :
        base(pGameController,pIsErrorEvent)
	{
        cognitiveLoad = 1;
        this.isErrorEvent = pIsErrorEvent;
        gameController = pGameController;
        
        this.instructionDisplay = gameController.instructionDisplay;
        
        widgetManager = gameController.widgetManager;

        
    
        inputWidget = this.widgetManager.getRandomAvailableInputWidget();
        if (inputWidget != null)
        {            
            inputWidget.createTargetValue(this);
            //Debug.Log("Widget Successfully returned in Track_Do");
            valid = true;            
            responseTimerRunning = 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());
                }
            
        }
        else
        {
            Debug.Log("Null Widget returned in Track_Do");
            valid = false;
        }

        
    }

    public override string getInstruction()
    {   
        string text = inputWidget.getTargetValueInstruction();
        return text.ToString().ToUpper()[0] + text.Substring(1) + " NOW";
    }

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


    public override void handleWidgetUpdate(Widget signallingWidget)
    {
        responseTimerRunning = false;
        this.gameController.reportTrackSuccess(true, this);
        if (this.isErrorEvent)
            gameController.errorCodesDisplay.removeCode(this.gameController.errorInstructionToCodes[this.getInstruction()]);
        this.finish();
    }

    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;
        }
    }

}
