#include "UIMLProgressAndStateView.h"

#include <FileSystem.h>
#include <Image.h>
#include <TextField.h>
#include <ToolTip.h>

#include "Globals.h"

#undef max

UIMLProgressAndStateView::UIMLProgressAndStateView(Framework::Text id,
    Framework::Text background,
    Framework::Text foreground,
    int* targetAddress,
    int addressLength,
    char registerMessage,
    char unregisterMessage,
    Framework::Text toolTipPrefix,
    Framework::Text toolTipSuffix)
    : Framework::Drawable(),
      NetworkAPIProcessor(),
      id(id),
      background(0),
      foreground(0),
      registerMessage(registerMessage),
      unregisterMessage(unregisterMessage),
      toolTipPrefix(toolTipPrefix),
      toolTipSuffix(toolTipSuffix),
      targetAddress(targetAddress),
      addressLength(addressLength),
      max(0),
      current(0)
{
    Framework::LTDBFile file;
    file.setFile(
        background.getTeilText(0, background.positionOf(".ltdb/") + 5));
    file.readData(0);
    this->background = file.load(
        0, background.getTeilText(background.positionOf(".ltdb/") + 6));
    file.setFile(
        foreground.getTeilText(0, foreground.positionOf(".ltdb/") + 5));
    file.readData(0);
    this->foreground = file.load(
        0, foreground.getTeilText(foreground.positionOf(".ltdb/") + 6));
    char* msg = new char[id.getLength() + 6];
    msg[0] = registerMessage; // register progress/state observer
    msg[1] = (char)id.getLength();
    memcpy(msg + 2, id.getText(), id.getLength());
    *(int*)(msg + 2 + id.getLength()) = NetworkAPIProcessor::getId();
    World::INSTANCE->zClient()->componentAPIRequest(
        targetAddress, addressLength, msg, id.getLength() + 6);
    delete[] msg;
}

UIMLProgressAndStateView::~UIMLProgressAndStateView()
{
    char* msg = new char[id.getLength() + 6];
    msg[0] = unregisterMessage; // unregister fuel state observer
    msg[1] = (char)id.getLength();
    memcpy(msg + 2, id.getText(), id.getLength());
    *(int*)(msg + 2 + id.getLength()) = NetworkAPIProcessor::getId();
    World::INSTANCE->zClient()->componentAPIRequest(
        targetAddress, addressLength, msg, id.getLength() + 6);
    delete[] msg;
    if (background)
    {
        background->release();
    }
    if (foreground)
    {
        foreground->release();
    }
    delete[] targetAddress;
}

void UIMLProgressAndStateView::api(char* message)
{
    max = *(int*)message;
    current = *(int*)(message + 4);
    Framework::Text toolTipText;
    toolTipText.append() << toolTipPrefix.getText() << current << " / " << max
                         << toolTipSuffix;
    if (!toolTip)
    {
        setToolTipText(toolTipText,
            uiFactory.initParam.bildschirm,
            uiFactory.initParam.font);
    }
    else
    {
        Framework::TextField* textField
            = dynamic_cast<Framework::TextField*>(toolTip->zMember(0));
        if (textField)
        {
            textField->setText(toolTipText);
            textField->setSize(
                textField->getNeededWidth(), textField->getNeededHeight());
        }
        else
        {
            setToolTipText(toolTipText,
                uiFactory.initParam.bildschirm,
                uiFactory.initParam.font);
        }
    }
    toolTip->setRender();
    rend = 1;
}

void UIMLProgressAndStateView::render(Framework::Image& rObj)
{
    if (hasStyleNot(Style::Visible)) return;
    readLock().lock();
    if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
    {
        readLock().unlock();
        return;
    }
    Drawable::render(rObj);
    rObj.alphaImage(0, 0, gr.x, gr.y, *background);
    if (max > 0 && current > 0)
    {
        double progrss = (double)current / max;
        if (hasStyle(Style::Vertical))
        {
            int height = (int)(gr.y * progrss);
            if (hasStyle(Style::Reversed))
            {
                if (rObj.setDrawOptions(0, gr.y - height, gr.x, height))
                {
                    rObj.alphaImage(0, height - gr.y, gr.x, gr.y, *foreground);
                    rObj.releaseDrawOptions();
                }
            }
            else
            {
                rObj.alphaImage(0, 0, gr.x, height, *foreground);
            }
        }
        else
        {
            int width = (int)(gr.x * progrss);
            if (hasStyle(Style::Reversed))
            {
                if (rObj.setDrawOptions(gr.x - width, 0, width, gr.y))
                {
                    rObj.alphaImage(width - gr.x, 0, gr.x, gr.y, *foreground);
                    rObj.releaseDrawOptions();
                }
            }
            else
            {
                rObj.alphaImage(0, 0, width, gr.y, *foreground);
            }
        }
    }
    rObj.releaseDrawOptions();
    readLock().unlock();
}
