#include "wnd_add_tex.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
#include <QCheckBox>
#include <QStyle>
#include <QComboBox>

namespace {
[[maybe_unused]] QString TexType2String_Full(Z::TextureType type) {
    switch (type) {
    case Z::TextureType::NONE: return "None";
    case Z::TextureType::DIFFUSE: return "Diffuse";
    case Z::TextureType::SPECULAR: return "Specular";
    case Z::TextureType::AMBIENT: return "Ambient";
    case Z::TextureType::EMISSIVE: return "Emissive";
    case Z::TextureType::HEIGHT: return "Height";
    case Z::TextureType::NORMALS: return "Normals";
    case Z::TextureType::SHININESS: return "Shiness";
    case Z::TextureType::OPACITY: return "Opacity";
    case Z::TextureType::DISPLACEMENT: return "Displacement";
    case Z::TextureType::LIGHTMAP: return "Lightmap";
    case Z::TextureType::REFLECTION: return "Reflection";
    case Z::TextureType::BASE_COLOR: return "BaseColor";
    case Z::TextureType::NORMAL_CAMERA: return "NormalCamera";
    case Z::TextureType::EMISSION_COLOR: return "EmissionColor";
    case Z::TextureType::METALNESS: return "Metalness";
    case Z::TextureType::DIFFUSE_ROUGHNESS: return "DiffuseRoughness";
    case Z::TextureType::AMBIENT_OCCLUSION: return "AmbientOcclusion";
    case Z::TextureType::UNKNOWN: return "Unknow";
    case Z::TextureType::SHEEN: return "Sheen";
    case Z::TextureType::CLEARCOAT: return "Clearcoat";
    case Z::TextureType::TRANSMISSION: return "Transmission";
    case Z::TextureType::MAYA_BASE: return "MayaBase";
    case Z::TextureType::MAYA_SPECULAR: return "MayaSpecular";
    case Z::TextureType::SPECULAR_COLOR: return "SpecularColor";
    case Z::TextureType::MAYA_SPECULAR_ROUGHNESS: return "MayaSpecularRoughness";
    case Z::TextureType::ANISOTROPY: return "Anisotropy";
    case Z::TextureType::GLTF_METALLIC_ROUGHNESS: return "GltfMetallicRoughness";
    default: return "None";
    }
}
}
class WndAddTex::WgtConfig : public QWidget {
public :
    explicit WgtConfig(ReturnData &outData) : m_outData(outData) {
        this->setLayout(new QVBoxLayout);
        layout()->setAlignment(Qt::AlignTop);
    }

    void DisplaySRGB() {
        PurgeWgts();
        m_outData.SRGB = true;
        QCheckBox *cb = new QCheckBox{"16 bytes per pixel"};
        cb->setCheckState(m_outData.bpp_16 ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
        connect(cb, &QCheckBox::checkStateChanged, this, [this](bool checked) {
            this->m_outData.bpp_16 = checked;
        });
        layout()->addWidget(cb);
        b_clear = false;
    }
    void DisplayLinear() {
        PurgeWgts();
        m_outData.SRGB = false;
        QCheckBox *cb = new QCheckBox{"16 bytes per pixel"};
        cb->setCheckState(m_outData.bpp_16 ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
        connect(cb, &QCheckBox::checkStateChanged, this, [this](bool checked) {
            this->m_outData.bpp_16 = checked;
        });
        layout()->addWidget(cb);
        b_clear = false;
    }
    void DisplayNormal() {
        PurgeWgts();
        m_outData.SRGB = false;
        QCheckBox *cb = new QCheckBox{"Reverse G Channel"};
        cb->setCheckState(Qt::CheckState::Unchecked);
        connect(cb, &QCheckBox::checkStateChanged, this, [this](bool checked) {
            this->m_outData.reverse_Y = checked;
        });
        layout()->addWidget(cb);
        b_clear = false;
    }

private:
    void PurgeWgts() {
        if (b_clear)return;
        while (QLayoutItem *item = layout()->takeAt(0)) {
            if (QWidget *widget = item->widget()) {
                // 延迟删除控件
                widget->deleteLater();
            }
            // 删除 QLayoutItem、Spacer 或已经清空的子布局
            delete item;
        }
        b_clear = true;
    }

private:
    ReturnData &m_outData;
    bool b_clear{true};
};
WndAddTex::WndAddTex(ReturnData &outData) : m_outData(outData) {
    outData = {};

    resize(250, 300);
    this->setWindowTitle("Text Config");
    this->setWindowIcon(QIcon{});

    QVBoxLayout *lay = new QVBoxLayout(this);
    this->setLayout(lay);
    lay->setAlignment(Qt::AlignJustify);

    this->setStyleSheet(".QCheckBox{font-size:18px;}.QCheckBox::indicator{width: 20px;height: 20px;}");

    // 下拉菜单选项
    QComboBox *box = new QComboBox{};
    box->addItem("SRGB");   //0
    box->addItem("Linear"); //1
    box->addItem("Normal"); //2
    lay->addWidget(box);

    // 具体内部选项
    m_config = new WgtConfig{outData};
    lay->addWidget(m_config);
    connect(box, &QComboBox::currentIndexChanged, this, &WndAddTex::OnSelectChanged);
    OnSelectChanged(0);

    // 最下方按钮
    lay->addStretch();
    QWidget *btnarea = new QWidget;
    btnarea->setLayout(new QHBoxLayout);
    QPushButton *btn_accept = new QPushButton{"Confirm", this};
    connect(btn_accept, &QPushButton::clicked, this, [this,&outData]() {
        outData.valid = true;
        this->accept();
    });
    QPushButton *btn_cancel = new QPushButton{"Cancel", this};
    connect(btn_cancel, &QPushButton::clicked, this, [this,&outData]() {
        outData.valid = false;
        this->reject();
    });
    btnarea->layout()->addWidget(btn_accept);
    btnarea->layout()->addWidget(btn_cancel);
    lay->addWidget(btnarea);
}
WndAddTex::~WndAddTex() = default;
void WndAddTex::OnSelectChanged(int index) {
    switch (index) {
    case 0: {
        this->m_outData.textureType = Z::TextureType::DIFFUSE;
        this->m_config->DisplaySRGB();
        break;
    }
    case 1: {
        this->m_outData.textureType = Z::TextureType::NONE;
        this->m_config->DisplayLinear();
        break;
    }
    case 2: {
        this->m_outData.textureType = Z::TextureType::NORMALS;
        this->m_config->DisplayNormal();
        break;
    }
    default: break;
    }
}



















//
