//
//  ExplorePresetManager.mm
//  riftathon
//
//  Created by Robert Tubb on 16/10/2014.
//
//

#include "ExplorePresetManager.h"
ExplorePresetManager expPresetManager;

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

void ExplorePresetManager::onAppLoad(){
    // check for already saved stuff
    startLoadAll();
    initPresetSlots();

    
    makeNeutralPreset();
    
}

//----------------------------------------------------------------------------
void ExplorePresetManager::goToFirstEmptySlot(){
    currentPresetSlotIndex = 0;
    
    // loop thru slots, find first empty slot
}
//----------------------------------------------------------------------------
bool ExplorePresetManager::writeValuesToSlot(vector<int> values){
    getPresetSlotAtIndex(currentPresetSlotIndex)->setValues(values);
    
    // now put it into a real preset, so it will be saved to file
    savePreset(getPresetSlotAtIndex(currentPresetSlotIndex));
    
    if (!getPresetSlotAtIndex(currentPresetSlotIndex)->isFilled){
        filledSlots++;
    }

    if(filledSlots == MAX_PRESETS){
        cout << "FINISHED EXP STAGE!" << endl;
        currentPresetSlotIndex = 0;
        return true;
    }
    return false;
}
//--------------------------------------------------------------------------------
void ExplorePresetManager::nextSlot(){
    currentPresetSlotIndex++;
    if (currentPresetSlotIndex >= thePresetSlots.size()){
        currentPresetSlotIndex = 0;
    }
}

//--------------------------------------------------------------------------------
PresetSlot* ExplorePresetManager::getCurrentPresetSlot(){
    
    return getPresetSlotAtIndex(currentPresetSlotIndex);
}
//----------------------------------------------------------------------------

PresetSlot* ExplorePresetManager::getPresetSlotAtIndex(int index){
    
    if (index >= thePresetSlots.size()){
        cout << "ERROR: index " << index << " exceeds number of presetslots " << thePresets.size() << endl;
        return thePresetSlots.back();
        return NULL;
    }else{
        return thePresetSlots[index];
        
    }
};
//---------------------------------------------------------------------------
void ExplorePresetManager::initPresetSlots(){
    
    presetSlotFilename = ofFilePath::getAbsolutePath(ofToDataPath("presetSlots.json"));
    
    // set up preset slots with names and images
    Json::Value root;
    Json::Reader reader;
    
    
    ifstream theFile(presetSlotFilename.c_str());
    stringstream fileText;
    string line;
    if(!theFile){
        cout<<"can't find presetSlot file \n";
        return;
    }else{
        
        while(theFile){
            theFile >> line;
            // cout << line << "\n"; // lots?
            fileText << line;
            
        }
        
        theFile.close();
    }
    
    bool parsingSuccessful = reader.parse( fileText.str(), root );
    
    if ( !parsingSuccessful )
    {
        // report to the user the failure and their locations in the document.
        std::cout  << "Failed to parse preset slot JSON: \n"
        << reader.getFormattedErrorMessages();
        return;
    }
    
    // now put into variables
    const Json::Value jv = root["presetSlots"];
    
    for ( int index = 0; index < jv.size(); ++index ){
        string name = jv[index]["name"].asString();
        string imageFileName = jv[index]["imageFileName"].asString();
        vector<int> empty;
        generatePresetSlot(name, imageFileName);
    }
    
    // now look at existing presets to see if slots are filled
    fillSlotsWithLoadedPresets();
    
    filledSlots = 0;
}
//-----------------------------------------------------------------------------
vector<string> drumPresetNames(){
    vector<string> vs;
    
    vs.push_back("boom kick");
    vs.push_back("punch kick");
    vs.push_back("snare");
    vs.push_back("clap");
    
    vs.push_back("closed hihat");
    vs.push_back("open hihat");
    vs.push_back("cowbell");
    vs.push_back("bongo");
    
    
}
//-----------------------------------------------------------------------------

void ExplorePresetManager::fillSlotsWithLoadedPresets(){
    for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
        string presetName = (*pi)->name;
        
        for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
            string slotName = (*psi)->name;
            if (slotName == presetName){
                fillSlotFromLoadedPreset(psi, pi);
            }
        }
    }
}
//-----------------------------------------------------------------------------

void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator slotToFill, vector<Preset*>::iterator preset){
    (*slotToFill)->setValues((*preset)->CCValues);
    (*slotToFill)->isFilled = true;
    filledSlots++;

    if(filledSlots == MAX_PRESETS){
        cout << "ALL SLOTS ARE FILLED!" << endl;
    }
    
    
}
//-----------------------------------------------------------------------------
void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
    vector<int> values = makeVector8(int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),0,0,0); // empty
    thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
    
}
