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

#include "TrainingMessageOrganiser.h"

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

void TrainingMessageOrganiser::setupDefaultMapping(){
    vector<int> mappingIDsForChangeableParams = getMappingIDsFromSynths();
    
    controlPanelType cpt = REVISITABLE;
    vector<controllerType> elemList;
    for(int i = 0; i < numParamsToUse; i++){
        elemList.push_back(SLIDER);
    }
    
    vector<UIElement*> UIElemHandles = panel->generateControls(elemList, cpt);
    
    mapSlidersToParams(UIElemHandles, mappingIDsForChangeableParams);
    
    bottomPanel->show();
    
    ofColor c = ofColor::yellow;
    panel->setHintColor(c);
}

vector<int> TrainingMessageOrganiser::getMappingIDsFromSynths(){
    vector<int> index;
    for(int i = 0; i < numParamsToUse; i++){
        index.push_back(i);
    }
    vector<int> mids = candidateSynth.getMappingIDForIndices(index);
    
    return mids;
}


//-----------------------------------------------------------------------
void TrainingMessageOrganiser::displayInstructions(Step s){
    
    
    if (s.type == Step::COUNT_IN){
        instructionPanel->setText("GET READY");
    }
    if (s.type == Step::PREVIEW_DISPLAY){
        instructionPanel->setText("MEMORISE");
        panel->setColor(ofColor(0,0,0));
    }
    if (s.type == Step::PREVIEW_PLAY){
        instructionPanel->setText("MEMORISE");
        panel->setColor(ofColor(0,0,0));
    }
    if (s.type == Step::MATCHING_INTERACTION){
        instructionPanel->setText("MATCH:");
        panel->setColor(ofColor(100,0,0));
    }
    if (s.type == Step::MATCHING_RESULT){
        instructionPanel->setText("RESULT");
        panel->setColor(ofColor(0,100,0));
    }
}
//-----------------------------------------------------------------------
void TrainingMessageOrganiser::onNextTick(int tickNumber){
    cout << "TICK " << tickNumber << endl;
    
    // only first beat in bar is active one
    if ( tickNumber % 4  != 0){
        return;
    }
    // load next target preset
    
    Step newStep = sequenceController.getNextStep();
    displayInstructions(newStep);
    
    if(newStep.isLastOfAll){
        // do finished run stuff, show summary
        candidateSynth.stopMetronome();
        cout << "FINISHED BLOCK" << endl;
    }
    if(newStep.isLastOfRun){
        // do finished run stuff, show summary
        candidateSynth.stopMetronome();
        cout << "FINISHED RUN" << endl;
    }
    
    float t = newStep.getTimeBetweenTicks();
    candidateSynth.setMetroTime(t);
    
    if(newStep.type == Step::COUNT_IN){
        // count in
        return;
    }
    
    Preset * currentTargetPreset;
    
    if(newStep.presetIndex >= 0 && newStep.presetIndex <= 8){
        currentTargetPreset =  expPresetManager.getPresetAtIndex(newStep.presetIndex);
    }

    vector<int> newValues = currentTargetPreset->getValues();
    debugVals(newValues);
    
    if(newStep.showsTargetIcon){
        bool showTick = true;
        presetIconPanel->setTextAndImage(currentTargetPreset->name, currentTargetPreset->getImage(), showTick);
        presetIconPanel->show();
    }else{
        presetIconPanel->hide();
    }
    
    if(newStep.allowsCandidateControl){
        panel->setActive(true);
    }else{
        panel->setActive(false);
    }
    
    if(newStep.showsControlSettings){
        // actually alters the slider values
        targetSynth.setAllParams(newValues);
        setAllSlidersToValues(newValues);
        
    }else{
        //setSlidersToDefault();
        
    }
    

    
    if(newStep.showsControlGuides){
        // shows visual target lines on sliders (or target hand in VR)
        
        panel->setHintValues(newValues);
        panel->showHint(true);
    }else{
        panel->showHint(false);
    }
    
    
    if(newStep.showsMatchResults){
        // do something
    }
    
    if(newStep.playsTarget){
        targetSynth.setAllParams(newValues);
        targetSynth.trigger();
    }
    if(newStep.playsCandidate){
        triggerCandidateSound();
    }
    
}

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

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 == TRIGGER_CANDIDATE_ID){
        //triggerCandidateSound();
        // compare to target
        candidateSynth.setMetroTime(sequenceController.getStartTickTime());
        candidateSynth.startMetronome();
        return;
    }
    
}
//-----------------------------------------------------------------------------
