//
//  ButtronSlider.cpp
//  emptyExample
//
//  Created by Robert Tubb on 22/05/2013.
//
//

#include "ButtronSlider.h"


ButtronSlider::ButtronSlider(float ax,
                             float ay,
                             float awidth,
                             float aheight,
                             float athickness,
                             float aradius,
                             ofColor aforegroundHi,
                             ofColor abackgroundHi,
                             ofColor aforegroundLo,
                             ofColor abackgroundLo,
                             SliderType type) :
Buttron(ax,ay,awidth,aheight,athickness,aradius,aforegroundHi,abackgroundHi,aforegroundLo,abackgroundLo),
sliderType(type)

{

    init();
}
ButtronSlider::ButtronSlider(float ax,
                             float ay,
                             float awidth,
                             float aheight,
                             SliderType type,
                             const UIProps& props) :
Buttron(ax,ay,awidth,aheight,props),
sliderType(type)

{
    init();
}
void  ButtronSlider::init(){
    myType = SLIDER;
    minVal = 0.;
    maxVal = 127.;
    value = 0.6;
    hintShowing = false;
    
    string fname = ofFilePath::getAbsolutePath(ofToDataPath("buttron.png"));
    defaultImage.loadImage(fname);
    setHandTexture(&defaultImage);
    showScoreForFrames = 0;
    hintPosAnimIncr = 0;
    posAnimIncr = 0;
    onlyOneTouchAllowed = true;
}
//
//---------------------------------------------------------------------
void ButtronSlider::drawIndicator(double proportion){
    if(!indicatorShowing) return;
    if(on){
        ofSetColor(foregroundHi);
    }else{
        ofSetColor(foregroundLo);
        
    }
    if(inactive){
        ofSetColor(fgInactive);
    }
    if(showScoreForFrames > 0){
        ofSetColor(flashColor);
        showScoreForFrames--;
    }
    if(sliderType == FILL){

        double maxH = height - 2 * thickness; //
        double w = width - 2 * thickness;//
        double barheight = value*maxH;
        double top = y + height - thickness - barheight;
        ofRect(x+thickness, top, w, barheight);
        
    }else if(sliderType == TEXTURE_FILL){
        
        double maxH = height - 2 * thickness; //
        double w = width - 2 * thickness;//
        double barheight = value*maxH;
        double top = y + height - thickness - barheight;
        //ofRect(x+thickness, top, w, barheight);
        ofSetColor(255,255, 255);
        (*handTextureRef).draw(x+thickness, top, w, barheight);
        
    }else if(sliderType == LINE){
        
        double loc = y + thickness +  (1 -value)*(height-3*thickness);
        ofRect(x+thickness,loc, width-2*thickness,thickness);
    }
    

}

void ButtronSlider::drawHintIndicator(){

    //ofSetColor(hintColor);
    float hthick = 16;
    double loc = y + thickness +  (1 - hintValue)*(height-3*thickness);
    //ofRect(x-thickness,loc, width+2*thickness,hthick);
    ofSetColor(255, 255, 255);
    (*handTextureRef).draw(x-thickness,loc-hthick/2, width+2*thickness,hthick);
}

void ButtronSlider::animateHintToNewValue(int newVal, float timeToTake){
    hintTargVal = (newVal - minVal)/(maxVal - minVal);
    
    float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake);
    hintPosAnimIncr = (hintTargVal - hintValue )*amtPerFrame;
    animating = true;
}
void ButtronSlider::animateToNewValue(int newVal, float timeToTake){
    targVal = (newVal - minVal)/(maxVal - minVal);
    
    float amtPerFrame = 1000./(ofGetFrameRate() * timeToTake);
    posAnimIncr = (targVal - value )*amtPerFrame;
    animating = true;
}
void ButtronSlider::update(){
    if (!animating) return;
    value += posAnimIncr;
    //hintValue += hintPosAnimIncr;
    
    // TODO when we've hit the target stop.
//    if (abs(targVal - value) < 0.01){
//        animating = false;
//    }
//    if (abs(hintTargVal - hintValue) < 0.01){
//        animating = false;
//    }
}
//---------------------------------------------------------------------

bool ButtronSlider::handleMyTouch(int tx, int ty, touchType ttype, int touchID){

    double ly = ty - y - thickness - radius;
    
    double prop;
    
    prop = 1 - ly/(height - 2 * (thickness + radius));

    if(prop > 1.) prop = 1.;                                                           
    if(prop < 0.) prop = 0.;

    setValue(prop);
    int scaleVal = int((maxVal - minVal)*prop + minVal);

    vector<int> pass;
    if(callback) callback(myParamID,scaleVal);
    
    return true;
}
//--------------------------------------------------------------------
void ButtronSlider::flashScore(int score, ofColor col, int howLong){
    showScoreForFrames = howLong;
    flashColor = col;
}
