#include "eweb.h"
#include "gutil/qgui.h"
#include <QSpinBox>
#include <QLineEdit>
#include <QPainter>

EWeb::EWeb(EBase *multiWin) : EBase(multiWin) {
    mType = EBase::Web;
}
EWeb::EWeb(const JObj &json, EBase *multiWin) : EBase(multiWin) {
    mType = EBase::Web;
    setBaseAttr(json);
    url = json["url"].toString();
    zoom = json["zoom"].toInt(100);
    _x = json["offX"].toInt(0);
    _y = json["offY"].toInt(0);
}

void EWeb::paint(QPainter *painter, const QStyleOptionGraphicsItem *a, QWidget *b) {
    auto inner = innerRect();
    painter->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    double maskW = holder().width(), maskH = holder().height();
    if(maskW>inner.width() || maskH>inner.height()) {
        double rate = qMin(inner.width() / maskW, inner.height() / maskH);
        maskW *= rate;
        maskH *= rate;
    }
    painter->drawImage(QRectF((inner.width() - maskW)/2, (inner.height() - maskH)/2, maskW, maskH), holder());
    EBase::paint(painter, a, b);
}

QWidget* EWeb::attrWgt() {
    auto wgtAttr = new QWidget;
    auto vBox = new VBox(wgtAttr);
    vBox->setContentsMargins(6, 0, 6, 0);
    if(mMultiWin) vBox->setSpacing(3);

    addBaseAttrWgt(vBox);

    auto hBox = new HBox(vBox);
    hBox->addLabel(tr("Basic Properties"));

    auto line = new QFrame;
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    hBox->addWidget(line, 1);

    hBox = new HBox(vBox);
    hBox->addSpacing(6);
    hBox->addLabel("URL:");

    auto url_fd = new QLineEdit(url);
    hBox->addWidget(url_fd);
    connect(url_fd, &QLineEdit::textChanged, this, [this](const QString &text) {
        url = text;
    });

    hBox = new HBox(vBox);
    auto lb = hBox->addLabel(tr("Zoom")+":");
    lb->setMinimumWidth(70);
    lb->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    auto fdZoom = new QSpinBox;
    fdZoom->setRange(1, 99999);
    fdZoom->setValue(zoom);
    connect(fdZoom, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [=](int value) {
        zoom = value;
    });
    hBox->addWidget(fdZoom);
    hBox->addSpacing(-3);
    hBox->addLabel("%");
    hBox->addStretch();


    hBox = new HBox(vBox);
    lb = hBox->addLabel(tr("Offset")+" X:");
    lb->setMinimumWidth(70);
    lb->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    auto fdX = new QSpinBox;
    fdX->setRange(0, 99999);
    fdX->setValue(_x);
    connect(fdX, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [=](int value) {
        _x = value;
    });
    hBox->addWidget(fdX);
    hBox->addSpacing(9);

    hBox->addLabel("Y:");
    auto fdY = new QSpinBox;
    fdY->setRange(0, 99999);
    fdY->setValue(_y);
    connect(fdY, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [=](int value) {
        _y = value;
    });
    hBox->addWidget(fdY);
    hBox->addStretch();

    vBox->addStretch();
    return wgtAttr;
}

JObj EWeb::attrJson() const {
    JObj obj;
    addBaseAttr(obj);
    obj["elementType"] = "Web";
    obj["url"] = url;
    obj["zoom"] = zoom;
    obj["offX"] = _x;
    obj["offY"] = _y;
    return obj;
}