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

#include "presetManager.h"

//---------------------------------------------------------------------------
extern Grid theGridView;
PresetManager presetManager;
//---------------------------------------------------------------------------


PresetManager::PresetManager(){
    timesOpened = 0;
    startupLoadAll();
    
    
    nextID = 0;
    
}
//---------------------------------------------------------------------------

int PresetManager::addPreset(const TwoVector coord, const string name){

    
    // check for same name
    vector<Preset *>::iterator iter;
    for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
        if ((*iter)->name == name){
            cout << " Preset by that name exists\n";
            
            // use exceptions!
            return -1;
        }
    }
    if(name == ""){
        cout << "Please name preset\n";
        return -2;
        
    }
    
    
    // check for same coords (!?!)
    thePresets.push_back(new Preset(coord, name,nextID));
    
    // if ok
    return nextID++;
}
//---------------------------------------------------------------------------
vector<TwoVector > PresetManager::getPresetsInRange(const TwoVector min, const TwoVector max){
    //return all the coordinates. oh and names (displayed at certain scales?).
    
    // TODO INEFFICIENT FOR LOTS OF PRESETS. CALLED EVERY GRAPHICS UPDATE!
    // so: put burden on saving rather than retrieving, make into list and index using coordinates
    
    vector<TwoVector > results;
    vector<Preset *>::iterator presetIter;
    for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
        if( ((*presetIter)->coordinates.x > min.x ) && ((*presetIter)->coordinates.y > min.y ) && ((*presetIter)->coordinates.x < max.x ) && ((*presetIter)->coordinates.y < max.y )){
            results.push_back((*presetIter)->coordinates);
        }

    }
    return results;
    
}
//---------------------------------------------------------------------------
void PresetManager::startupLoadAll(){
  // get stuff from file
    // load file
  
    string fname = ofxiPhoneGetDocumentsDirectory() + "presets.dat";
    char *fileName = (char*)fname.c_str();
    ifstream from(fileName);
    string line;

    // simply prints file
    while(from >> line){
        cout << line << "\n";
      
    }



    timesOpened++;
}
//---------------------------------------------------------------------------
void PresetManager::exitAndSaveAll(){
    ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +"presets.dat",ofFile::WriteOnly);

    cout << "Exit and save presets\n";
    vector<Preset *>::iterator presetIter;

    for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
        //vector<int> params;
        //params = theGridView.calculateParamsFromCoord((*presetIter)->coordinates);
        presetFile << **presetIter << "\n";
        cout << **presetIter << "\n";

    }
    presetFile.close();

}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------