#include "AlphaField.h"

#include "Image.h"
#include "Point.h"
#include "Scroll.h"
#include "Text.h"
#include "ToolTip.h"

using namespace Framework;

// Content of the AlphaField class from AlphaField.h
// Constructor
AlphaField::AlphaField()
    : Drawable(),
      strength(5),
      color(0x9B000000)
{}

AlphaField::~AlphaField() {}

// non-constant
void AlphaField::setStrength(int st) // sets the strength
{
    strength = st;
    rend = 1;
}

void AlphaField::setColor(int f) // sets the color
{
    color = f;
    rend = 1;
}

void AlphaField::render(Image& zRObj) // draws to zRObj
{
    Drawable::render(zRObj);
    int br = gr.x - 1;
    int hi = gr.y - 1;
    int xp = pos.x, yp = pos.y;
    int a = (color >> 24) & 0xFF;
    int index = (br / 2) * (br <= hi) + (hi / 2) * (br > hi);
    int fc = color & 0x00FFFFFF;
    int fc2 = color;
    if (strength > 0) index = index > (a / strength) ? a / strength : index;
    if (strength < 0)
        index
            = index > ((255 - a) / -strength) ? ((255 - a) / -strength) : index;
    for (int i = 0; i < index; ++i)
    {
        a -= strength;
        fc2 = (a << 24) | fc;
        int i2 = i << 1;
        zRObj.drawLineHAlpha(
            xp + i + 1, yp + i, br - i2, fc2); // top left --- top right
        zRObj.drawLineVAlpha(xp + br - i,
            yp + i + 1,
            hi - i2,
            fc2); // top right -- bottom right
        zRObj.drawLineHAlpha(
            xp + i, yp + hi - i, br - i2, fc2); // bottom right - bottom left
        zRObj.drawLineVAlpha(
            xp + i, yp + i, hi - i2, fc2); // bottom left -- top left
    }
    if (index == br / 2)
    {
        for (int i = index; i <= index + (br - index) - index; ++i)
            zRObj.drawLineVAlpha(xp + i,
                yp + index,
                hi - (index << 1) + 1,
                fc2); // remaining area vertical
    }
    else
    {
        for (int i = index; i <= index + (hi - index) - index; ++i)
            zRObj.drawLineHAlpha(xp + index,
                yp + i,
                br - (index << 1) + 1,
                fc2); // remaining area horizontal
    }
}

// constant
int AlphaField::getStrength() const // returns the strength
{
    return strength;
}

int AlphaField::getColor() const // returns the color
{
    return color;
}

Drawable* AlphaField::duplicate() const // copies the drawing object
{
    AlphaField* obj = new AlphaField();
    obj->setPosition(pos);
    obj->setSize(gr);
    obj->setMouseEventParameter(makParam);
    obj->setKeyboardEventParameter(takParam);
    obj->setMouseEvent(mak);
    obj->setKeyboardEvent(tak);
    if (toolTip) obj->setToolTipZ((ToolTip*)toolTip->duplicate());
    obj->setStrength(strength);
    obj->setColor(color);
    return obj;
}