#include "wgt_prop_input.h"

#include <QHBoxLayout>
#include <QLineEdit>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QLabel>

namespace {



QWidget *CreateRowFloat(int column, float *ar_data, WgtPropInput *p,
                        void (WgtPropInput::*fpCB)()) {
    QWidget *w = new QWidget{};
    w->setLayout(new QHBoxLayout);
    w->layout()->setSpacing(10);
    w->layout()->setContentsMargins(0, 0, 0, 0);
    for (int i = 0; i < column; ++i) {
        QDoubleSpinBox *box = new QDoubleSpinBox{};
        float *pdata = &ar_data[i];
        box->setValue(*pdata);
        box->setMinimumWidth(40);
        box->setButtonSymbols(QAbstractSpinBox::NoButtons);
        QWidget::connect(box, &QDoubleSpinBox::valueChanged, [pdata,p,fpCB](double v) {
            *pdata = static_cast<float>(v);
            (p->*fpCB)();
        });
        w->layout()->addWidget(box);
    }
    return w;
}

}



WgtPropInput::WgtPropInput(SHADER::PropertyInfo &prop, void (*fpCallback)(void *, int), void *parameter, int para2) :
fpCallback(fpCallback), callBackOBJ(parameter), para2(para2), m_Prop(&prop) {
    int row{1};
    int column{1};
    bool bFloat{true};
    switch (prop.type) {
    case Z::ShaderInfo::PropType::Matrix3x3: {
        row = 3;
        column = 3;
        break;
    }
    case Z::ShaderInfo::PropType::Matrix4x4: {
        row = 4;
        column = 4;
        break;
    }
    case Z::ShaderInfo::PropType::Float: {
        break;
    }
    case Z::ShaderInfo::PropType::Float2: {
        column = 2;
        break;
    }
    case Z::ShaderInfo::PropType::Float3: {
        column = 3;
        break;
    }
    case Z::ShaderInfo::PropType::Float4: {
        column = 4;
        break;
    }
    case Z::ShaderInfo::PropType::Int: {
        bFloat = false;
        break;
    }
    case Z::ShaderInfo::PropType::Int2: {
        column = 2;
        bFloat = false;
        break;
    }
    case Z::ShaderInfo::PropType::Int3: {
        column = 3;
        bFloat = false;
        break;
    }
    case Z::ShaderInfo::PropType::Int4: {
        column = 4;
        bFloat = false;
        break;
    }
    default: {
        new QLabel("WgtPropInput: 错误的item类型", this);
        return;
    };
    }

    this->setLayout(new QVBoxLayout);
    this->layout()->setSpacing(0);
    this->layout()->setContentsMargins(0, 0, 0, 0);

    std::byte *pstart = reinterpret_cast<std::byte *>(&prop.value);

    for (int i = 0; i < row; ++i) {
        void *p = pstart + (4 * i * column);
        this->layout()->addWidget(CreateRowFloat(column, static_cast<float *>(p),
                                                 this, &WgtPropInput::OnValueChanged));
    }

}
// 在子框框内数值变动时回调
// 此时对应的数值已经在子框内填好,不需要再填
// 直接通知外部update
void WgtPropInput::OnValueChanged()  {
    if (fpCallback) {
        (*fpCallback)(callBackOBJ, para2);
    }
}














//
