//
//  sliderPanel.cpp
//  tweakathlon
//
//  Created by Robert Tubb on 11/02/2014.
//
//

#include "sliderPanel.h"

//-----------------------------------------------------------------------------
SliderPanel::SliderPanel(float ax,
                         float ay,
                         float awidth,
                         float aheight,
                         const UIProps& aprops,
                         vector<controllerType> elemList) :
UIElementContainer(ax,ay,awidth,aheight,aprops)
{
    cout << "SliderPanel auto layout contructor\n";

    // generateControls(elemList);// called from messageorganiser
    
    
    ofVec3f ori;
    ori.x = 1.;
    ori.y = 0.5;
    
    resultLight.setOrientation(ori);
    resultLight.setDirectional();
    resultLight.setDiffuseColor(ofColor::white);
}
//--------------------------------------------------------------------------------

void SliderPanel::showHint(bool value){
    if (thisPanelType == ALL_SLIDERS){
        vector<UIElement *>::iterator UIitr;
        for(UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            
            
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->showHint(value);
        }
    }else if(thisPanelType == LEAP6DOF){
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->showHint(value);
        }
    }
}
//------------------------
void SliderPanel::setHintColor(ofColor c){
    vector<UIElement *>::iterator UIitr;
    for(UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
        (*UIitr)->setHintColor(c);
    }
}
//------------------------
void SliderPanel::setSliderColors(ofColor c){
    for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
        ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
        thisSlider->setColor(c);
    }
}
//------------------------
void SliderPanel::flash(){
    // turn hilight on and off
    if (!flashing){
        vector<UIElement *>::iterator UIitr;
        for(UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            (*UIitr)->setHighlight(true);
        }
        // set up timer
        TimerCallbackFunction tcb;
        tcb = boost::bind(&SliderPanel::flash, this);
        timeController.scheduleEvent(tcb, 250);
        flashing = true;
    }else{
        vector<UIElement *>::iterator UIitr;
        for(UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            (*UIitr)->setHighlight(false);
        }
        flashing = false;
    }
    
}
//------------------------
void SliderPanel::setBgColor(ofColor c){
    vector<UIElement *>::iterator UIitr;
    for(UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
        (*UIitr)->setColor(c);
    }
}
//------------------------
//------------------------
UIElement* SliderPanel::getSlider(int index){
    if (index < subElements.size()){
        return subElements[index];
    }else{
        cout << "ERROR: index too big for sldiers" << endl;
        return NULL;
    }
};
//------------------------
void SliderPanel::showValueIndicators(bool show){
    
    
    if (thisPanelType == LEAP6DOF){ // yeuch
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->showValueIndicator(show);
        }
        
    }else if(thisPanelType == ALL_SLIDERS){
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->showValueIndicator(show);
        }
    }
}
//------------------------
void SliderPanel::setHintValues(vector<int> vals){
    if (thisPanelType == LEAP6DOF){ // yeuch
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->setHintValues(vals);
        }
    }else if(thisPanelType == ALL_SLIDERS){
        int i = 0;
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->setHintValue(vals[i]);
            i++;
        }
    }
}
//------------------------
void SliderPanel::setValues(vector<int> vals){
    if (thisPanelType == LEAP6DOF){ // yeuch
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            cout << "set values" << endl;
            box->setValues(vals);
        }
    }else if(thisPanelType == ALL_SLIDERS){
        int i = 0;
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->setValueAndScale(vals[i]);
            i++;
        }
    }
}

//-----------------------------------------------------------------------------
vector<UIElement*> SliderPanel::generateControls(vector<controllerType> elemList, controlPanelType panelType){
    removeAllSubelements();
    vector<controllerType>::iterator i;
    
    // 10 cm is 520 pixels
    
    // calc positions
    int top  = y + myProps.spacerSize;
    
    float pixPerElem = width/(float)elemList.size();
    if (pixPerElem < myProps.sliderWidth ){
        cout << "error not enough room for sliders" << endl;
    }
    
    int n=0;
    for(i=elemList.begin(); i<elemList.end();i++){
        if(*i == SLIDER){
            // add a slider
            float c = (n + 0.5) * pixPerElem;
            float l = c - myProps.sliderWidth/2;
            if(n==1){
                cout << "centre " << c << endl;
                cout << l << endl;
            }
            
            ButtronSlider * revslider = new ButtronSlider(l , top , myProps.sliderWidth, myProps.sliderHeight,TEXTURE_FILL, myProps);
            revslider->setLabel("unassigned");
            subElements.push_back(revslider);
            revslider->showHint(false);
            // grey out all but first
            if(panelType == SEQUENTIAL && i != elemList.begin()){
                revslider->setActive(false);
            }
            
            n++;
            
        }else if(*i == XYPAD){
            // add a xy
            float c = (n + 0.5) * pixPerElem;
            float left = c - myProps.XYsize/2;
            ButtronXY * xyp = new ButtronXY(left , top , myProps);
            xyp->setLabel("unassigned","unassigned");
            xyp->showHint(false);
            subElements.push_back(xyp);
            n++;
        }else if(*i == LEAP3D){
            // add a threed box
            float c = x+width*0.5;
            float left = c - myProps.XYsize;
            
            //Leap3DBox * l3d = new Leap3DBox(left , top+50 , myProps.XYsize*0.75,myProps.XYsize*0.75,150,50, myProps);

            Leap3DBoxGL * l3d = new Leap3DBoxGL(left , top+50 , myProps.XYsize*0.75,myProps.XYsize*0.75,150,50, myProps);
            

            
            subElements.push_back(l3d);
            n++;
        }else if(*i == LEAP6D){
            // add a threed box
            float c = x+width*0.5;
            float left = c - myProps.XYsize;

            Leap6DBox * l6d = new Leap6DBox(left , top+50 , myProps.XYsize*0.75,myProps.XYsize*0.75,150,50, myProps);

            
            subElements.push_back(l6d);
            n++;
        
        }else{
            cout << "ERROR: slider panel only handles xy pads and sliders" << endl;
        }
        

    }
    thisPanelType = panelType;
    autoArrangeRow(); // will set positions
    


    return subElements;
}
//-----------------------------------------------------------------------
void SliderPanel::setAndShowHint(vector<int> values, ofImage* texture){
    animating = false;
    setHintValues(values);
    setHintTexture(texture);
    showHint(true);

}
void SliderPanel::setHintNoShow(vector<int> values, ofImage* texture){
    animating = false;
    setHintValues(values);
    setHintTexture(texture);
    showHint(false);
    
}
//------------------------------------------
void SliderPanel::setHintTexture(ofImage* texture){
    if (thisPanelType == LEAP6DOF){ // yeuch
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->setHintTexture(texture);
        }
        
    }else if(thisPanelType == ALL_SLIDERS){

        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->setHandTexture(texture);
        }
    }
}
//------------------------------------------
void SliderPanel::setIndicatorTexture(ofImage* texture){
    if (thisPanelType == LEAP6DOF){ // yeuch
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->setHandTexture(texture);
        }
        
    }else if(thisPanelType == ALL_SLIDERS){
        
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->setHandTexture(texture);
        }
    }
}
//---------------------------------------------
void SliderPanel::animateToNewHintValues(vector<int> newTargetValues, float timeToTake, ofImage* newTexture){
    animating = true;
    
    if(thisPanelType == LEAP6DOF){
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->animateHintToNewValues(newTargetValues, timeToTake, newTexture);
        }
    }else if(thisPanelType == ALL_SLIDERS){
        int i = 0;
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
     
            thisSlider->animateHintToNewValue(newTargetValues[i], timeToTake);
            i++;
        }
    }
    
}
//---------------------------------------------
void SliderPanel::animateToNewValues(vector<int> newTargetValues, float timeToTake){
    animating = true;
    
    if(thisPanelType == LEAP6DOF){
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->animateToNewValues(newTargetValues, timeToTake);
        }
    }else if(thisPanelType == ALL_SLIDERS){
        int i = 0;
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            
            thisSlider->animateToNewValue(newTargetValues[i], timeToTake);
            i++;
        }
    }
    
}
//---------------------------------------------

void SliderPanel::update(){
    if(!animating) return;
    if(thisPanelType == LEAP6DOF){
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->update();
        }
    }else if(thisPanelType == ALL_SLIDERS){
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->update();

        }
    }
    
}


void SliderPanel::flashResultLight(ofColor c, int howLong){
    
    resultLight.setDiffuseColor(c);
    resultLight.enable();
    // set up timer
    TimerCallbackFunction tcb;
    tcb = boost::bind(&SliderPanel::turnOffResultLight, this);
    timeController.scheduleEvent(tcb, howLong);
    if(thisPanelType == LEAP6DOF){
        if (subElements[0]->getType() == LEAP6D){
            Leap6DBox * box = (Leap6DBox*)subElements[0];
            
            box->flashScore(5, c);
        }
    }else if(thisPanelType == ALL_SLIDERS){
        for(auto UIitr = subElements.begin(); UIitr < subElements.end(); UIitr++){
            ButtronSlider* thisSlider = (ButtronSlider *)(*UIitr);
            thisSlider->flashScore(5, c);
            
        }
    }
    
}
void SliderPanel::turnOffResultLight(){
    
    resultLight.setDiffuseColor(ofColor::white);
    resultLight.disable();
}

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

