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

#include "presetManager.h"

//---------------------------------------------------------------------------
extern Grid theGridView;
PresetManager presetManager;
//---------------------------------------------------------------------------
void Preset::draw(){
    //
    int sz = 2;
    TwoVector pos = theGridView.coordToPixel(coordinates); // euch -rely on grid view?
    
    if(pixVals.size() < 9){
        cout << "NO PRESET PIC\n";
        return;
    }
    //middle row
    // centre
    ofSetColor(pixVals[0]);
    ofRect(pos.x, pos.y,sz,sz);
    // right
    ofSetColor(pixVals[1]);
    ofRect(pos.x+sz, pos.y,sz,sz);
    // left
    ofSetColor(pixVals[2]);
    ofRect(pos.x-sz, pos.y,sz,sz);
    
    //top
    // centre
    ofSetColor(pixVals[3]);
    ofRect(pos.x, pos.y+sz,sz,sz);
    // right
    ofSetColor(pixVals[4]);
    ofRect(pos.x+sz, pos.y+sz,sz,sz);
    // left
    ofSetColor(pixVals[5]);
    ofRect(pos.x-sz, pos.y+sz,sz,sz);
    
    // bottom
    // centre
    ofSetColor(pixVals[6]);
    ofRect(pos.x, pos.y-sz,sz,sz);
    // right
    ofSetColor(pixVals[7]);
    ofRect(pos.x+sz, pos.y-sz,sz,sz);
    // left
    ofSetColor(pixVals[8]);
    ofRect(pos.x-sz, pos.y-sz,sz,sz);
};
//---------------------------------------------------------------------------
PresetManager::PresetManager(){
    timesOpened = 0;
    nextID = 0;

    string ts = ofGetTimestampString();
    cout << "ofGetTimestampString: " << ts << '\n';
}

//---------------------------------------------------------------------------
void PresetManager::printAll(){
    cout << "ALL PRESETS: \n";
    vector<Preset *>::iterator presetIter;
    for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
        cout << **presetIter;
        
    }
}
//---------------------------------------------------------------------------

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;
        
    }
    
    // make piccy
    vector<ofColor> presetPicture = makePresetPicture(coord);
    thePresets.push_back(new Preset(coord, name,nextID, presetPicture));
    
    // if ok
    return nextID++;
}
//---------------------------------------------------------------------------
vector<ofColor> PresetManager::makePresetPicture(TwoVector coord){
    // convert midi parameters to a nice piccy
    vector<int> params = theGridView.calculateParamsFromCoord(coord);
    vector<ofColor> pixCols;
    
    ofColor col(255,0,0);
    for(int i=0; i<9;i++){
        col.setHue(params[i]*2);
        pixCols.push_back(col);
    }
    return pixCols;
}
//---------------------------------------------------------------------------

int PresetManager::loadPreset(const TwoVector coord, const string name, long long stime){
    vector<ofColor> presetPicture = makePresetPicture(coord); // or just store it??
    thePresets.push_back(new Preset(coord, name,nextID, stime, presetPicture));
   
     // if ok
    return nextID++;
}
//---------------------------------------------------------------------------
vector<Preset *> 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<Preset *> 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);
        }

    }
    return results;
    
}


//---------------------------------------------------------------------------
void PresetManager::drawPresetsInRange(const TwoVector min, const TwoVector max){
    vector<Preset *> pInRange = getPresetsInRange(min, max);
    vector<Preset *>::iterator piter;
    int i = 0;
    for(piter = pInRange.begin(); piter < pInRange.end(); piter++){

        (*piter)->draw();
        i++;
    }
 
}
//---------------------------------------------------------------------------
void PresetManager::startupLoadAll(){
  // get stuff from file
    // load file
  
    string fname = ofxiPhoneGetDocumentsDirectory() + "presets.dat";
    ifstream presetFile(fname.c_str());

    // simply prints file

    if(!presetFile){
        cout<<"no preset file";
        return;
    }
    while(presetFile >> *this);
    
    //TODO get presets from online database...
    
    presetFile.close();

    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";
    }
    presetFile.close();
    
    // TODO dleete all the new events??
    

}

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