//
//  IconPanel.mm
//  riftathon
//
//  Created by Robert Tubb on 20/10/2014.
//
//

#include "IconPanel.h"

//------------------------------------------------------------------
IconPanel::IconPanel(float ax,
                 float ay,
                 float awidth,
                 float aheight,
                 const UIProps& props):
UIElement(ax,ay,awidth,aheight, props)

{
    cout << "ICON PANEL  constructor\n";
    
    thickness = 2.;
    radius = props.cornerRadius;
    foregroundHi = props.buttonHi;
    backgroundHi = props.generalBackground;
    foregroundLo = props.buttonLo;
    backgroundLo = props.generalBackground;
    fgInactive = props.inactiveGreyedOut;
    labelName = "YO";
    on = false;
    hasBeenSet = false;
    textureImage = NULL;
    showTick = false;
    showCross = false;
}
//------------------------------------------------------------------
void IconPanel::draw(){
    if(hidden) return;
    //cout << "drawing button" << endl;
    //ofFill();
    //UIElement::draw(); // should do background
    //drawOutline();
    ofDisableDepthTest();
    if(hasBeenSet){
        drawTexture();
        
        drawTextLabel();
        
        if(showTick){
            drawTick();
        }
        if(showCross){
            drawCross();
        }
        
    }
    ofEnableDepthTest();
    
    
}
//------------------------------------------------------------------
void IconPanel::drawTick(){
    ofPoint pos(x + width - 25, y + height - 25);
    ofSetColor(0, 200, 20);
    ofSetLineWidth(3);
    ofLine(pos.x, pos.y, pos.x+8, pos.y+10);
    ofLine(pos.x+8, pos.y+10, pos.x+10, pos.y+8);
}
//------------------------------------------------------------------
void IconPanel::drawCross(){
    ofPoint pos(x + width - 25, y + height - 25);
    ofSetColor(230, 0, 20);
    ofSetLineWidth(3);
    ofLine(pos.x, pos.y, pos.x+10, pos.y+10);
    ofLine(pos.x+10, pos.y, pos.x, pos.y+10);
}
//------------------------------------------------------------------

void IconPanel::drawTexture(){
    float z = 10;
    ofSetColor(255,255,255);
    textureImage->draw(x+30,y+30,z,width-20,height-30);
    ofSetColor(foregroundHi);
}
//------------------------------------------------------------------
void IconPanel::drawTextLabel(){
    ofColor fg,bg;
    
    if(on){
        fg = foregroundHi;
        bg = backgroundHi;
    }else{
        fg = foregroundLo;
        bg = backgroundLo;
    }
    if(inactive){
        fg = fgInactive;
    }
    ofSetColor(fg);
    verdana16.drawString(labelName, x + 40, y + 8);
    
}
//------------------------------------------------------------------
void IconPanel::drawOutline(){
    
    // draw bars
    ofColor fg,bg;
    
    if(on){
        fg = foregroundHi;
        bg = backgroundHi;
    }else{
        fg = foregroundLo;
        bg = backgroundLo;
    }
    ofSetColor(fg);
    
    float cornerSize = thickness + radius;
    // left
    ofRect(x, y+cornerSize, thickness, height - 2*(cornerSize));
    //right
    ofRect(x + width - thickness, y+cornerSize, thickness, height - 2*(cornerSize));
    // top
    ofRect(x+cornerSize, y, width-2*(cornerSize), thickness);
    // bottom
    ofRect(x+cornerSize, y+height-thickness, width-2*(cornerSize), thickness);
    
    // draw corner  foreground circles
    
    //tl
    ofCircle(x+cornerSize, y+cornerSize, cornerSize);
    
    
    //tr
    
    ofCircle(x+width-cornerSize, y+cornerSize, cornerSize);
    
    //bl
    
    ofCircle(x+cornerSize, y+height-cornerSize, cornerSize);
    
    //br
    
    ofCircle(x+width-cornerSize, y+height-cornerSize, cornerSize);
    
    // draw corner inner bg circles
    ofSetColor(bg);
    ofCircle(x+cornerSize, y+cornerSize, radius);
    
    ofCircle(x+width-cornerSize, y+cornerSize, radius);
    
    ofCircle(x+cornerSize, y+height-cornerSize, radius);
    
    ofCircle(x+width-cornerSize, y+height-cornerSize, radius);
    
    // fill in background
    ofRect(x+cornerSize,y+thickness,width-2*cornerSize,height-2*thickness);
    ofRect(x+thickness, y+cornerSize, radius, height-2*cornerSize);
    ofRect(x+width-cornerSize, y+cornerSize, radius, height-2*cornerSize);
}
//------------------------------------------------------------------------------
bool IconPanel::handleMyTouch(int tx, int ty, touchType ttype, int touchID){
    
    //cout << "buttron handling touch\n";
    if(ttype == TOUCH_DOWN){
        on = true;
        if(callback) callback(myParamID,1);
        
    }else if(ttype == TOUCH_MOVED){
        
    }else if(ttype == TOUCH_UP){
        on = false;
    }
    return true; // necessary?
    
}

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