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

#include "UIElement.h"

//----------------------------------------------------------------------
UIElement::UIElement(){
    //
    //cout << " UIElement default constructur BAD !!!\n";
    init();
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
UIElement::UIElement(float ax,
                     float ay,
                     float awidth,
                     float aheight,
                     const UIProps& props) :
x(ax),
y(ay),
width(awidth),
height(aheight),
background(props.generalBackground)
{
    //cout << " UIElement constructur with defs \n";
    init();
    verdana16 = props.verdana16;
    bigFont = props.bigFont;
    
}
//----------------------------------------------------------------------
UIElement::UIElement(float ax,
                     float ay,
                     float awidth,
                     float aheight,
                     ofColor bg) :
x(ax),
y(ay),
width(awidth),
height(aheight),
background(bg)
{
    init();

}
//----------------------------------------------------------------------
void UIElement::init(){

    
    hidden = false;
    inactive = false;
    zLayer = 0;
    onlyOneTouchAllowed = false;
}
//----------------------------------------------------------------------
void UIElement::draw(){
    if(hidden) return;
    //cout<<"element draw\n";
    ofSetColor(background);
    ofRect(x,y,width,height);
    
    
};
//----------------------------------------------------------------------
bool UIElement::touch(int tx, int ty, touchType ttype, int touchID){
    if(isMyTouch(tx,ty,ttype,touchID)){
        handleMyTouch(tx, ty, ttype,touchID);
        return true;
    }else{
        return false;
    }
}
bool UIElement::atLeastOneTouchAlready(){
    if (myTouchIDs.size() >= 1){
        return true;
    }else{
        return false;
    }
}
//----------------------------------------------------------------------
// called first from all subclasses
bool UIElement::isMyTouch(int tx, int ty, touchType ttype, int touchID){
    if(hidden || inactive) return false;

    if(ttype == TOUCH_DOWN){
        if (touchIsInMyArea(tx, ty)){
            if (!isExistingTouchID(touchID)){
                //cout << "Touchdown in area, grabbing focus " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
                if (onlyOneTouchAllowed && atLeastOneTouchAlready()){
                    cout << "ignoring xtra touch" << endl;
                }else{
                    addTouchID(touchID);
                }
                return true;

            }else{
                //shouldn't happen?
                return true;
            }

        }else{
            //cout << "Touchdown outside area, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;

        }
    }
    if(ttype == TOUCH_UP){
        if (isExistingTouchID(touchID)){
            //cout << "Touch Up for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            //myTouchID = -1;
            removeTouchID(touchID);
            return true;
        }else{
            //cout << "Touch Up NOT my  ID, ignoring " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;
        }
    }
    
    if(ttype == TOUCH_MOVED){
        if(isExistingTouchID(touchID)){
            //cout << "Touch moved for my ID, handling " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return true;
        }else{
            //cout << "Touch moved NOT my ID, ignore " << labelName << " mytouchID: " << myTouchID << " finger ID: " << touchID << endl;
            return false;
        }
    }
    
    cout << "UNHANDLED SITUATION!" << labelName << endl;
    return false;

}

//----------------------------------------------------------------------
bool UIElement::touchIsInMyArea(int tx, int ty){
    
    // work out relative coords
    double relx = tx - x;
    double rely = ty - y;
    return !(relx < 0 || relx > width || rely < 0 || rely > height);
}
//----------------------------------------------------------------------
bool UIElement::isCoordInMyRegion(double ax, double ay){
    if(hidden) return false;
    
    if( (ax > x && ax < x+width) && (ay > y && ay < y+height)){
        return true;
    }else{
        return false;
    }
  
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------