#include "BoxGeometry.h"

BoxGeometry::BoxGeometry()
    : mLocation(DiscretePoint(0, 0)),
        mSize({ 100, 0, 0, 100, 0, 0 }),
        mPadding(),
        mMargin()
{

}

BoxGeometry::~BoxGeometry()
{

}


BoundedSize BoxGeometry::getSize() const
{
    return mSize;
}

const DiscretePoint& BoxGeometry::getLocation() const
{
    return mLocation;
}

void BoxGeometry::setSize(const BoundedSize& size)
{
    if (size != mSize)
    {
        mSize = size;
        mTransformDirty = true;
    }
}

void BoxGeometry::setMargin(unsigned margin)
{
    setMargin({ margin, margin, margin, margin });
}

void BoxGeometry::setMargin(const BoundaryOffset& margin)
{
    if (margin != mMargin)
    {
        mMargin = margin;
        mTransformDirty = true;
    }
}

void BoxGeometry::setPadding(unsigned padding)
{
    setPadding({ padding, padding, padding, padding });
}

void BoxGeometry::setPadding(const BoundaryOffset& padding)
{
    if (padding != mPadding)
    {
        mPadding = padding;
        mTransformDirty = true;
    }
}

void BoxGeometry::setBounds(unsigned width, unsigned height)
{
    if (mSize.mMaxWidth > 0 && width > mSize.mMaxWidth)
    {
        width = mSize.mMaxWidth;
    }

    if (mSize.mMaxHeight > 0 && height > mSize.mMaxHeight)
    {
        height = mSize.mMaxHeight;
    }

    if (width != mSize.mWidth || height != mSize.mHeight)
    {
        mTransformDirty = true;
        mSize.mWidth = width;
        mSize.mHeight = height;
    }
}

void BoxGeometry::setWidth(unsigned width)
{
    setBounds(width, mSize.mHeight);
}

void BoxGeometry::setHeight(unsigned height)
{
    setBounds(mSize.mWidth, height);
}

void BoxGeometry::setMaxHeight(unsigned maxHieght)
{
    if (mSize.mMaxHeight != maxHieght)
    {
        mTransformDirty = true;
        mSize.mMaxHeight = maxHieght;
    }
}

void BoxGeometry::setLocation(const DiscretePoint& loc)
{
    if (mLocation != loc)
    {
        mLocation = loc;
        mTransformDirty = true;
    }
}

void BoxGeometry::setMaxWidth(unsigned maxWidth)
{
    if (mSize.mMaxWidth != maxWidth)
    {
        mTransformDirty = true;
        mSize.mMaxWidth = maxWidth;
    }
}

bool BoxGeometry::contains(const DiscretePoint& loc) const
{
    if (loc.getX() < mLocation.getX())
    {
        return false;
    }
    if (loc.getX() > mLocation.getX() + mSize.mWidth)
    {
        return false;
    }
    if (loc.getY() > mLocation.getY() + mSize.mHeight)
    {
        return false;
    }
    if (loc.getY() < mLocation.getY())
    {
        return false;
    }
    return true;
}
