//
//  presetManager.mm
//  oscSenderExample
//
//  Created by Robert Tubb on 07/11/2012.
//
//

#include "presetManager.h"

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

PresetManager presetManager;
extern EventLogger eventLogger;

//---------------------------------------------------------------------------
void Preset::draw(){
    // probably just hint values on sliders
};
//---------------------------------------------------------------------------
Json::Value Preset::presetToJson(){
    // create the string for this instance of Preset object
    
    Json::Value presetVal;
    
    presetVal["creatorUserName"] = creatorUserName;
    presetVal["creatorDeviceID"] = creatorDeviceID;
    presetVal["creationTime"] =     creationTime;
    presetVal["name"] =             name;
    
    for(vector<int>::iterator i; i < CCValues.end(); i++){
        presetVal["CCValues"].append(*i);
    }

    return presetVal;
}
//---------------------------------------------------------------------------
PresetManager::PresetManager(){
    timesOpened = 0;
    nextID = 0;
   
    string ts = ofGetTimestampString();
    
    presetAlertShowing = false;

    
    cout << "ofGetTimestampString: " << ts << '\n';
}
//---------------------------------------------------------------------------
Json::Value PresetManager::allPresetsToJson(){
    Json::Value root;
 
    // use jsoncpp
    vector<Preset *>::iterator presetIter;
    
    int i = 0;
    for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
        root["presets"][i] = (*presetIter)->presetToJson();
        i++;
    }

    return root;
}
//---------------------------------------------------------------------------
void PresetManager::readJsonToPresets(const string &jsonFile){
    Json::Value root;
    Json::Reader reader;

    
    ifstream theFile(jsonFile.c_str());
    stringstream fileText;
    string line;
    if(!theFile){
        cout<<"can't find preset 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 JSON: \n"
        << reader.getFormattedErrorMessages();
        return;
    }

    // now put into variables
    const Json::Value jpresets = root["presets"];
    
    for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
    
    //printAll();
     
}
//---------------------------------------------------------------------------
void PresetManager::printAll(){
    cout << "----------------ALL PRESETS-------------: \n";
    cout << allPresetsToJson() << "\n";
}
//---------------------------------------------------------------------------
void PresetManager::showNameDialog(){
//    if(!presetAlertViewController.alertShowing){ // this is to stop wierd infinite loop in ios5 (simulator)
//        presetAlertShowing = true;
//        [presetAlertViewController showPresetNamePrompt];
//        
//    }
    
}
//---------------------------------------------------------------------------
// when save button pressed
int PresetManager::savePreset(const string name, vector<int> values){

    presetAlertShowing = false;
    // check for same name
    vector<Preset *>::iterator iter;

    for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
        if ((*iter)->name == name){
            cout << "WARNING Preset by that name exists, overwriting\n";
            // overwrite it
            (*iter)->CCValues = values;
        }
    }
     
    if(name == ""){
        cout << "Please name preset\n";
        
    }


    thePresets.push_back(new Preset(values, name, nextID, eventLogger.userName, eventLogger.deviceID));
    eventLogger.logEvent(SAVE_PRESET); // TODO need to log details?

    return nextID++;
}

vector<int> PresetManager::recallPreset(string name){
    vector<Preset *>::iterator p;
        for(p = thePresets.begin(); p < thePresets.end(); p++){
            if ( (*p)->getName() == name){
                return (*p)->getValues();
            }
    }
    
}
//----------------------------------------------cu-----------------------------
void PresetManager::startLoadAll(){
    // get stuff from file
    // load file

    string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
    
    readJsonToPresets(fname);
    
    timesOpened++;
}

//---------------------------------------------------------------------------
void PresetManager::exitAndSaveAll(){
    ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
    
    // stick all the stuff in a json value
    Json::Value root = allPresetsToJson();
    
    cout << root;
    presetFile << root;

}

//---------------------------------------------------------------------------
void PresetManager::clearAll(){
    thePresets.clear();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------