#include "materiallayerinfo.h"
#include "ui_materiallayerinfo.h"
#include <QFileDialog>
#include "modelcreatordata.h"
#include <QTabBar>
#include "modelcreatordata.h"
#include "m3derror.h"
#include <QMessageBox>
#include <iostream>

MaterialLayerInfo::MaterialLayerInfo(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::MaterialLayerInfo)
{
    m_ui->setupUi(this);
}

void MaterialLayerInfo::setObjectName(const QString &name)
{
    QWidget::setObjectName(name);
    QString oname = objectName();
    if(oname == "difuseTab") {type = M3D::M3DML_DIFFUSE;subtype = 0;}
    else if(oname == "ambientTab") {type = M3D::M3DML_AMBIENT;subtype = 1;}
    else if(oname == "shininessTab") {type = M3D::M3DML_SHININESS; subtype = 2;}
    else if(oname == "specularTab") {type = M3D::M3DML_SPECULAR; subtype = 3;}
    else if(oname == "emissionTab") {type = M3D::M3DML_EMISSION; subtype =4;}
    else if(oname == "bumpMapTab") {type = M3D::M3DML_BUMP_MAP; subtype = 5;}
    else if(oname == "normalMapTab") {type = M3D::M3DML_NORMAL_MAP; subtype = 6;}
    else if(oname == "roughnessTab") {type = M3D::M3DML_ROUGHNESS; subtype = 7;}
    switch (type)
    {
        case M3D::M3DML_DIFFUSE:
        {
            m_ui->isColorBlock->setDisabled(true);
            m_ui->isFloatBlock->setDisabled(true);
            m_ui->isTextureBlock->setChecked(true);
            break;
        }
        case M3D::M3DML_AMBIENT:
        {
            m_ui->isColorBlock->setChecked(true);
            m_ui->isFloatBlock->setDisabled(true);
            break;
        }
        case M3D::M3DML_SHININESS:
        {
            m_ui->isColorBlock->setDisabled(true);
            m_ui->isFloatBlock->setChecked(true);
            break;
        }
        case M3D::M3DML_SPECULAR:
        {
            m_ui->isColorBlock->setChecked(true);
            m_ui->isFloatBlock->setDisabled(true);
            break;
        }
        case M3D::M3DML_EMISSION:
        {
            m_ui->isColorBlock->setChecked(true);
            m_ui->isFloatBlock->setDisabled(true);
            break;
        }
        case M3D::M3DML_BUMP_MAP:
        {
            m_ui->isColorBlock->setDisabled(true);
            m_ui->isFloatBlock->setDisabled(true);
            m_ui->isTextureBlock->setChecked(true);
            break;
        }
        case M3D::M3DML_NORMAL_MAP:
        {
            m_ui->isColorBlock->setDisabled(true);
            m_ui->isFloatBlock->setDisabled(true);
            m_ui->isTextureBlock->setChecked(true);
            break;
        }
        case M3D::M3DML_ROUGHNESS:
        {
            m_ui->isColorBlock->setDisabled(true);
            m_ui->isFloatBlock->setChecked(true);
            break;
        }
    }
}

MaterialLayerInfo::~MaterialLayerInfo()
{
    delete m_ui;
}

void MaterialLayerInfo::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void MaterialLayerInfo::textureOpen()
{            
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open texture"), "", tr("Magic3D texture files (*.m3dt)"));
    emit setTexturePath(fileName);
    int idx = fileName.lastIndexOf("/")+1;
    QString texture_name = fileName.mid(idx);
    m_ui->textureName->setText(texture_name);
    MCD->texture[subtype].load(fileName.toUtf8().data());
    emit textureLoaded(subtype);
}
void MaterialLayerInfo::saveLayer()
{
    if(m_ui->isColorBlock->isChecked())//Цветовой блок
    {
        ALLOC(MCD->material_data[subtype].color_block, M3D::M3DML_Color_Block);
        ALLOC(MCD->material_data[subtype].data_block, M3D::M3DML_Data_Block);
        m_ui->colorInformationWidget->getColorInformation(MCD->material_data[subtype].color_block);
        MCD->material_data[subtype].data_block->block_data_type = M3D::M3DML_COLOR;
        MCD->material_data[subtype].data_block->block_type = type;
        MCD->material.set_layer_data(type, MCD->material_data[subtype]);

    }
    else if(m_ui->isFloatBlock->isChecked())
    {
        ALLOC(MCD->material_data[subtype].float_block, M3D::M3DML_Float_Block);
        ALLOC(MCD->material_data[subtype].data_block, M3D::M3DML_Data_Block);
        m_ui->floatInformationWidget->getFloatData(MCD->material_data[subtype].float_block);
        MCD->material_data[subtype].data_block->block_data_type = M3D::M3DML_FLOAT;
        MCD->material_data[subtype].data_block->block_type = type;
        MCD->material.set_layer_data(type, MCD->material_data[subtype]);
    }
    else if(m_ui->isTextureBlock->isChecked())
    {
        ALLOC(MCD->material_data[subtype].texture_block, M3D::M3DML_Texture_Block);
        ALLOC(MCD->material_data[subtype].data_block, M3D::M3DML_Data_Block);
        QString texture = m_ui->textureName->text();
        ALLOC_N(MCD->material_data[subtype].texture_path, char, texture.length()+1);
        MCD->material_data[subtype].texture_block->texture_path_length = texture.length()+1;
        strncpy(MCD->material_data[subtype].texture_path, texture.toUtf8().data(), texture.length()+1);
        MCD->material_data[subtype].data_block->block_data_type = M3D::M3DML_TEXTURE;
        MCD->material_data[subtype].data_block->block_type = type;
        MCD->material.set_layer_data(type, MCD->material_data[subtype]);
    }
}

bool MaterialLayerInfo::loadLayer(QString &base)
{
    const M3D::M3DML_Layer *layer = MCD->material.get_layer_data(type);
    if(!layer)return false;
    QString tpath;
    switch(layer->data_block->block_data_type)
    {
        case M3D::M3DML_TEXTURE://???????
            m_ui->isTextureBlock->setChecked(true);
            m_ui->textureName->setText(layer->texture_path);
            tpath = base + m_ui->textureName->text();
            try
            {
                MCD->texture[subtype].load(tpath.toUtf8().data());
            }catch(M3DError error)
            {
                QMessageBox::critical(this, tr("Open file error:") + tpath, QString::fromLocal8Bit(error.get_reason().c_str()));
                return true;
            }
            emit setTexturePath(tpath);
            emit textureLoaded(subtype);
            break;
        case M3D::M3DML_COLOR:
            m_ui->isColorBlock->setChecked(true);
            m_ui->colorInformationWidget->loadData(layer);
            break;
        case M3D::M3DML_FLOAT:
            m_ui->isFloatBlock->setChecked(true);
            m_ui->floatInformationWidget->loadData(layer);
            break;
    }
    return true;
}
