//
//  TrainingMessageOrganiser.mm
//  riftathon
//
//  Created by Robert Tubb on 17/10/2014.
//
//

#include "TrainingMessageOrganiser.h"

void TrainingMessageOrganiser::init( PDSynthWrapper& cs, PDSynthWrapper& ts, bool soundOnlyMode){
    
    MessageOrganiser::init(cs,ts);
    
    TickListenerFunction callback;
    callback = boost::bind(&TrainingMessageOrganiser::onNextTickAnim, this, _1);
    candidateSynth.registerForTicks(callback);
    
    numParamsToUse = TOTAL_NUM_PARAMS;

    sequenceController.init(soundOnlyMode);

    
}

void TrainingMessageOrganiser::setup(controlPanelType whichInterfaceAreWeUsing){
    
    setupDefaultMapping(whichInterfaceAreWeUsing);
    
    controlPanel->setValues(zeros<int>(TOTAL_NUM_PARAMS));
    
    doHand(true);
}
//----------------------------------------------------------------------------------------
void TrainingMessageOrganiser::setMiddlePanel(TextPanel* tp){
    middlePanel = tp;
}

//----------------------------------------------------------------------------------------
void TrainingMessageOrganiser::showMyPanels(){
    presetIconPanel->show();
    instructionPanel->show();
    bottomPanel->show();
    controlPanel->show();
    middlePanel->show();
    if (panelType == LEAP6DOF){
        okToGetMidi = true; // allow midi input to thing
    }else{
        okToGetMidi = false;
    }
}

//-----------------------------------------------------------------------
void TrainingMessageOrganiser::displayInstructions(string text){

    instructionPanel->setText(text);
    instructionPanel->show();

}

//-----------------------------------------------------------------------

void TrainingMessageOrganiser::showUserHowTheyDid(){
    // colour flash
    // distance ?
    // score
    
}


//-----------------------------------------------------------------------------

void TrainingMessageOrganiser::buttonPressCallback(int mappingID, int value){
    if(mappingID == VOLUME_CHANGE_ID){
        targetSynth.sendVolume(value);
        candidateSynth.sendVolume(value);
        return;
    }
    
    if (mappingID == START_TRAINING_SEQUENCE_ID){

        candidateSynth.setMetroTime(sequenceController.getStartTickTime());
        candidateSynth.startMetronome();
        bottomPanel->hide();
        
        vector<int> details;
        
        eventLogger.logEvent(START_NEW_RUN);
        return;
    }
    
    if(mappingID == TO_MENU_ID){
        // tell testapp to start again?
    
    }
    
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------
void TrainingMessageOrganiser::onNextTick(int tickNumber){
    static int showingCountdown = false;

    downCounter = 4 - tickNumber % 4;
    
    if (showingCountdown){
        middlePanel->setColor(ofColor::white);
        middlePanel->setText(ofToString(downCounter));
    }
    
    // only first beat in bar is active one
    if ( tickNumber % 4  != 0){
        
        return;
    }
    
}
void TrainingMessageOrganiser::lastOfRun(int which){
    candidateSynth.stopMetronome();
    stringstream s;
    
    s << "FINISHED RUN " << which+1 << " OF " << sequenceController.getTotNumRuns() << endl;
    controlPanel->hide();
    controlPanel->setColor(ofColor::black);
    middlePanel->setColor(ofColor::white);
    middlePanel->setText(s.str());
    middlePanel->show();
    //playCandidateButton->setLabel("NEXT");
    bottomPanel->show();
    seqNumPanel->hide();
    eventLogger.logEvent(FINISHED_RUN);
    eventLogger.saveSessionToFile();
}
void TrainingMessageOrganiser::lastOfAll(){
    candidateSynth.stopMetronome();
    cout << "FINISHED BLOCK" << endl;
    controlPanel->hide();
    controlPanel->setColor(ofColor::black);
    middlePanel->setColor(ofColor::white);
    middlePanel->setText("CONGRATULATIONS. FINISHED BLOCK!");
    middlePanel->show();
    bottomPanel->show();
    seqNumPanel->hide();
    eventLogger.saveSessionToFile();
}

void TrainingMessageOrganiser::logNewStep(AnimStep newStep){
    
    vector<int> details;
    
    details.push_back(int(newStep.type));
    details.push_back(newStep.whichInSequence);
    details.push_back(newStep.showsGuides);
    details.push_back(newStep.showsIcons);
    details.push_back(newStep.presetIndex);
    details.push_back(newStep.tempo);
    
    eventLogger.logEvent(NEW_STEP, details);
    
}

//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::onNextTickAnim(int tickNumber){
    static AnimStep oldStep;
    static vector<int> lastPosition = zeros<int>(TOTAL_NUM_PARAMS);
    
    //cout << "TICK " << tickNumber << endl;
    downCounter = 4 - tickNumber % 4;
    
    updateCountdown(downCounter);
    // only first beat in bar is active one (at the moment)
    //  send next target values 1 step before them play
    if ( tickNumber % 4  != 0) {
        if (tickNumber % 4  == 3){
            targetSynth.sendAllParams();
        }
        return;
    };
    
    
    Preset* previousTargetPreset;
    vector<int> curCandidateSettings = candidateSynth.getAllParamValues();
    if (oldStep.showsResultsAtEnd){
        
        // store results
        if(oldStep.presetIndex == -1){
            previousTargetPreset =  expPresetManager.getNeutralPreset();
            cout << "SHOULDNT SCORE NEUTRAL??" << endl;
        }else{
            
            previousTargetPreset =  expPresetManager.getPresetAtIndex(oldStep.presetIndex);
        }
        
        eventLogger.logEvent(FINISH_POS, curCandidateSettings);
        TrainingTestResult result =  doResults(oldStep,
                                               previousTargetPreset,
                                               lastPosition,
                                               curCandidateSettings);
        
        flashResult(result);
    }
    
    AnimStep newStep = sequenceController.getNextStep();
    
    logNewStep(newStep);
    
    if(newStep.isLastOfAll){
        lastOfAll();
        return;
    }
    if(newStep.isLastOfRun){
        lastOfRun(newStep.runNumber);
        return;
    }
    
    candidateSynth.setMetroTime(newStep.getTimeBetweenTicks());
    Preset * currentTargetPreset;
    if(newStep.presetIndex == -1){
        currentTargetPreset =  expPresetManager.getNeutralPreset();
        
    }else{
        
        currentTargetPreset =  expPresetManager.getPresetAtIndex(newStep.presetIndex);
        if (currentTargetPreset == NULL){
            displayInstructions("No stored preset!\n please restart and choose EXP");
            candidateSynth.stopMetronome();
            return;
        }
    }
    
    switch (newStep.type){
        case AnimStep::PREVIEW_NEUTRAL_COUNT :
            previewNeutralCount(newStep,currentTargetPreset);
            break;
        case AnimStep::PREVIEW_MOVE :
            previewMove(newStep,currentTargetPreset);
            break;
        case AnimStep::PREVIEW_LAST :
            previewLast( newStep,currentTargetPreset);
            break;
        case AnimStep::MATCHING_NEUTRAL_COUNT :
            //matchingNeutralCount( newStep,currentTargetPreset);
            break;
        case AnimStep::MATCHING_MOVE :
            matchingMove( newStep,currentTargetPreset);
            break;
        case AnimStep::MATCHING_LAST :
            matchingLast( newStep,currentTargetPreset);
            break;
        case AnimStep::GUIDED_MOVE :
            //guidedMove( newStep);
            break;
            
        case AnimStep::GUIDED_HIT :
            //guidedHit( newStep);
            break;
        default:
            break;
    }
    
    oldStep = newStep;
    lastPosition = curCandidateSettings;
    
}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::updateCountdown(int n){
    
}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::previewNeutralCount(AnimStep newStep, Preset * currentTargetPreset){
    okToGetMidi = false;
    candidateSynth.playDownbeatBlip();
    middlePanel->hide();

    // your 'hand' is hidden
    controlPanel->showValueIndicators(false);
    
    
    // guide is set to neutral values
    currentTargetPreset = expPresetManager.getNeutralPreset();
    vector<int> newTargetValues = currentTargetPreset->getValues();
    controlPanel->setValues(newTargetValues);
    presetIconPanel->setTextAndImage(currentTargetPreset->name, currentTargetPreset->getImage(), false);
    presetIconPanel->show();

    controlPanel->setAndShowHint(newTargetValues, currentTargetPreset->getImage());
    doHand(false);
    controlPanel->show();
    
    targetSynth.setAllParams(newTargetValues);
    //candidateSynth.setAllParams(newTargetValues);
    
    displayInstructions("Get ready for preview");
    seqNumPanel->hide();
    oldTargetValues = newTargetValues;
    controlPanel->setColor(ofColor::black);
}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::previewMove(AnimStep newStep, Preset * currentTargetPreset){

    targetSynth.trigger(); // PLAYS LAST TARGET


    vector<int> newTargetValues = currentTargetPreset->getValues();

    targetSynth.setAllParamsWithoutSend(newTargetValues);
    
    doGuides(newStep.showsGuides, currentTargetPreset);

    doHand(false, newStep.showsGuides, newStep.getTimeBetweenTicks()*4, newTargetValues);
    
    doIcons(newStep.showsIcons, currentTargetPreset);


    controlPanel->show();

    displayInstructions("PREVIEW");
    showSeqNum(newStep.whichInSequence);
    oldTargetValues = newTargetValues;
    
    controlPanel->setColor(ofColor::black);
}


//-----------------------------------------------------------------------------
// lest setep of preview seq - plays moves back to neutral pos?
void TrainingMessageOrganiser::previewLast(AnimStep newStep, Preset * currentTargetPreset){
    targetSynth.trigger();

    // wrong?
    targetSynth.setAllParamsWithoutSend(currentTargetPreset->getValues());
    
    // your hand shows, we dont bother animating back to neutral!?
    doHand(true);

    doIcons(newStep.showsIcons,currentTargetPreset);
    
    doGuides(newStep.showsGuides, currentTargetPreset);

    showSeqNum(newStep.whichInSequence);
    displayInstructions("Preview FINISHED");
    
    controlPanel->setColor(ofColor::darkMagenta);
}

//-----------------------------------------------------------------------------
//void TrainingMessageOrganiser::guidedMove(AnimStep newStep){
//    targetSynth.trigger(); // plays LAST target (?)
//    triggerCandidateSound();
//    middlePanel->hide();
//    
//    Preset * currentTargetPreset;
//    if(newStep.presetIndex == -1){
//        currentTargetPreset =  expPresetManager.getNeutralPreset();
//    }else{
//        
//        currentTargetPreset =  expPresetManager.getPresetAtIndex(newStep.presetIndex);
//    }
//
//    vector<int> newTargetValues = currentTargetPreset->getValues();
//  
//    targetSynth.setAllParamsWithoutSend(newTargetValues);
//    
//    
//    controlPanel->setActive(true);
//    // show icon and guide markers
//    presetIconPanel->setTextAndImage(currentTargetPreset->name, currentTargetPreset->getImage(), false);
//    presetIconPanel->show();
//    
//    controlPanel->setAndShowHint(newTargetValues, currentTargetPreset->getImage());
//    
//    
//    // show your hand and allow movement
//    controlPanel->showValueIndicators(true);
//    okToGetMidi = true;
//    
//    stringstream instruct;
//    instruct << "GUIDED MOVE" << endl;;
//    instruct << newStep.whichInSequence+1 << endl;
//    displayInstructions(instruct.str());
//    
//    showSeqNum(newStep.whichInSequence+1);
//
//    oldTargetValues = newTargetValues;
//}
//
////======================================================================
//void TrainingMessageOrganiser::guidedHit(AnimStep newStep){
//    Preset * currentTargetPreset;
//    currentTargetPreset =  expPresetManager.getPresetAtIndex(newStep.presetIndex);
//    vector<int> newTargetValues = currentTargetPreset->getValues();
//    eventLogger.logEvent(NEW_TARGET, newTargetValues);
//    sendSynthValuesAgain();
//    
//    // show your hand and allow movement (so you can set off to next target??)
//    
//    controlPanel->showValueIndicators(true);
//    okToGetMidi = true;
//    
//    targetSynth.setAllParamsWithoutSend(newTargetValues);
//    triggerCandidateSound();
//    targetSynth.trigger();
//    displayInstructions("GUIDED HIT");
//    vector<int> startPosition = expPresetManager.getNeutralPreset()->getValues();
//    
//    oldTargetValues = newTargetValues;
//}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//void TrainingMessageOrganiser::matchingNeutralCount(AnimStep newStep, Preset * currentTargetPreset){
//    Preset * currentTargetPreset = expPresetManager.getNeutralPreset();
//    vector<int> newTargetValues = currentTargetPreset->getValues();
//    // if showing guides show all the targets as transparent ghosts
//    if (newStep.showsGuides){
//        controlPanel->setAndShowHint(newTargetValues, currentTargetPreset->getImage());
//
//    }else{
//        controlPanel->showHint(false);
//    }
//    
//    // show your hand and allow movement
//    controlPanel->showValueIndicators(true);
//    okToGetMidi = true;
//    
//        displayInstructions("Match Count");
//    seqNumPanel->hide();
//    oldTargetValues = newTargetValues;
//}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::matchingMove(AnimStep newStep, Preset * currentTargetPreset){

    
    middlePanel->hide();

    vector<int> newTargetValues = currentTargetPreset->getValues();
    eventLogger.logEvent(NEW_TARGET, newTargetValues);
    targetSynth.setAllParamsWithoutSend(newTargetValues);

    // if showing guides show the hint hand
    doGuides(newStep.showsGuides, currentTargetPreset);
    
    doIcons(newStep.showsIcons, currentTargetPreset);
    
    doHand(true);

    
    displayInstructions("MATCH IT");
    showSeqNum(newStep.whichInSequence);
    // trigger where we're at at the moment
    triggerCandidateSound();


    oldTargetValues = newTargetValues;
    if (newStep.showsGuides){
        controlPanel->setColor(ofColor::seaGreen);
    }else{
        if (newStep.showsIcons){
            controlPanel->setColor(ofColor::darkMagenta);
        }else{
            controlPanel->setColor(ofColor::red);
            // set sliders to default texture
            controlPanel->setIndicatorTexture(NULL);
        }
    }
}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::matchingLast(AnimStep newStep, Preset * currentTargetPreset){
    // why is this different from match it?
    
    vector<int> newTargetValues = currentTargetPreset->getValues();
    eventLogger.logEvent(NEW_TARGET, newTargetValues);
    targetSynth.setAllParamsWithoutSend(newTargetValues);
    
    doGuides(newStep.showsGuides, currentTargetPreset);
    
    doIcons(newStep.showsIcons, currentTargetPreset);
    
    doHand(true);
    
    //displayInstructions("MATCHING FINISHED");
    showSeqNum(newStep.whichInSequence);

    triggerCandidateSound();
    oldTargetValues = newTargetValues;
    if (newStep.showsGuides){
        controlPanel->setColor(ofColor::seaGreen);
    }else{
        if (newStep.showsIcons){
            controlPanel->setColor(ofColor::darkMagenta);
        }else{
            controlPanel->setColor(ofColor::red);
        }
    }
}
//-----------------------------------------------------------------------------

TrainingTestResult TrainingMessageOrganiser::doResults(AnimStep step,
                                                       Preset * targetPreset,
                                                       vector<int> startingPosition,
                                                       vector<int> currentPosition){
    // targetParams, startPosition, answer, timeAllowed
    TrainingTestResult result =
    
    trainingScoreManager.getScoreForAnswer(targetPreset->getValues(),
                                           startingPosition,
                                           currentPosition,
                                           step.getTimeAllowed(),
                                           step.difficulty,
                                           step.whichInSequence,
                                           step.presetIndex,
                                           step.tempoLevel,
                                           step.runNumber);
    
    sequenceController.saveResultForCurrentStep(result);
    instructionPanel->setColor(result.colorBand);


    displayInstructions(result.displayText);
    
    showScoreNum(trainingScoreManager.getScore());
    
    return result;
}

void TrainingMessageOrganiser::flashResult(TrainingTestResult r){
    
    // stick a colored number over target position
    controlPanel->flashResultLight(r.colorBand, 30);

    
}


//------------------------------------------------------------------------------------
void TrainingMessageOrganiser::doIcons(bool showIcons, Preset * currentTargetPreset){
    
    if (showIcons){
        presetIconPanel->setTextAndImage(currentTargetPreset->name, currentTargetPreset->getImage(), false);
        presetIconPanel->show();
    }else{
        presetIconPanel->hide();
    }
}
//------------------------------------------------------------------------------------
void TrainingMessageOrganiser::doGuides(bool showGuides, Preset * currentTargetPreset){
    
    // show icon and guides
    if (showGuides){ // always show neutral one
        
        // show where we're going
        controlPanel->setAndShowHint(currentTargetPreset->getValues(), currentTargetPreset->getImage());
        
        
    }else{
        controlPanel->showHint(false);
    }
    
}
//-----------------------------------------------------------------------------
void TrainingMessageOrganiser::doHand(bool showControllableHand, bool showAnimatedHand, float animTime, vector<int> newTargetValues)
{
    // show how to get there
    if (showAnimatedHand){
        controlPanel->animateToNewValues(newTargetValues, animTime);
        controlPanel->showValueIndicators(true); // for animation
        controlPanel->setActive(false);
        
    }else if(showControllableHand){
        controlPanel->showValueIndicators(true); // for animation
        controlPanel->setActive(true); // allow user control
        if (panelType == LEAP6DOF){
            okToGetMidi = true; // allow midi input to thing
        }else{
            okToGetMidi = false;
        }
        
    }else{
        controlPanel->showValueIndicators(false); // hand/ slider bars invisible
        controlPanel->setActive(false); // cant control it
    }
    
}
