/*
    QGL MaterialEditor
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "material_editor.h"

#include "glmaterial.h"
#include "ui_material_editor.h"


MaterialEditor::MaterialEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::MaterialEditor();
	ui->setupUi(this);
	ui->mapDiffuse->configure(tr("Diffuse"), true);
	ui->mapNormal->configure(tr("Normal"));
	ui->mapMetalness->configure(tr("Metalness"));
	ui->mapRoughness->configure(tr("Roughness"));
	ui->mapEmission->configure(tr("Emission"), true);
	ui->mapRelief->configure(tr("Relief"));
	ui->checkGlass->hide();
	ui->frameReflection->hide();
	ui->label_13->hide();
	mat         = 0;
	active      = true;
	ignore_next = 0;
}


void MaterialEditor::changeEvent(QEvent * e) {
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange: ui->retranslateUi(this); break;
	default: break;
	}
}


void MaterialEditor::materialChanged() {
	if (!active || !mat) return;
	ignore_next         = 2;
	mat->_changed       = true;
	mat->color_diffuse  = ui->mapDiffuse->color();
	mat->color_emission = ui->mapEmission->color();
	mat->transparency   = ui->spinTransparent->value();
	mat->reflectivity   = ui->spinReflect->value();
	mat->iof            = ui->spinIOF->value();
	mat->dispersion     = ui->spinDispersion->value();
	mat->glass          = ui->checkGlass->isChecked();
	// emit changed();
}


void MaterialEditor::setMaterial(Material * m) {
	if (ignore_next > 0) {
		// ignore_next = false;
		return;
	}
	active = false;
	mat    = m;
	setEnabled(m);
	if (!mat) return;
	ui->mapDiffuse->setColor(mat->color_diffuse);
	ui->mapEmission->setColor(mat->color_emission);
	ui->spinTransparent->setValue(mat->transparency);
	ui->spinReflect->setValue(mat->reflectivity);
	ui->spinIOF->setValue(mat->iof);
	ui->spinDispersion->setValue(mat->dispersion);
	ui->checkGlass->setChecked(mat->glass);
	ui->mapDiffuse->setMap(&(mat->map_diffuse));
	ui->mapNormal->setMap(&(mat->map_normal));
	ui->mapMetalness->setMap(&(mat->map_metalness));
	ui->mapRoughness->setMap(&(mat->map_roughness));
	ui->mapEmission->setMap(&(mat->map_emission));
	ui->mapRelief->setMap(&(mat->map_relief));
	/*ui->lineReflFront->setProperty("GLpath", mat->map_reflection.path(0));
	ui->lineReflFront->setText(QFileInfo(mat->map_reflection.path(0)).fileName()); ui->lineReflBack->setProperty("GLpath",
	mat->map_reflection.path(1)); ui->lineReflBack->setText(QFileInfo(mat->map_reflection.path(1)).fileName());
	ui->lineReflLeft->setProperty("GLpath", mat->map_reflection.path(2));
	ui->lineReflLeft->setText(QFileInfo(mat->map_reflection.path(2)).fileName()); ui->lineReflRight->setProperty("GLpath",
	mat->map_reflection.path(3)); ui->lineReflRight->setText(QFileInfo(mat->map_reflection.path(3)).fileName());
	ui->lineReflTop->setProperty("GLpath", mat->map_reflection.path(4));
	ui->lineReflTop->setText(QFileInfo(mat->map_reflection.path(4)).fileName()); ui->lineReflBottom->setProperty("GLpath",
	mat->map_reflection.path(5)); ui->lineReflBottom->setText(QFileInfo(mat->map_reflection.path(5)).fileName());
	*/
	active = true;
}

/*
Material MaterialEditor::material() {
    Material m;
    mat->color_diffuse  = ui->colorDiffuse ->color();
    mat->color_specular = ui->colorSpecular->color();
    mat->color_emission = ui->colorEmission->color();
    mat->transparency = ui->spinTransparent->value();
    mat->reflectivity = ui->spinReflect->value();
    mat->iof = ui->spinIOF->value();
    mat->dispersion = ui->spinDispersion->value();
    mat->glass = ui->checkGlass->isChecked();
    mat->map_diffuse   = ui->mapDiffuse->map();
    mat->map_normal    = ui->mapNormal->map();
    mat->map_specular  = ui->mapSpecular->map();
    mat->map_roughness = ui->mapRoughness->map();
    mat->map_emission  = ui->mapEmission->map();
    mat->map_relief    = ui->mapRelief->map();
    mat->map_reflection.setPath(0, ui->lineReflFront->property("GLpath").toString());
    mat->map_reflection.setPath(1, ui->lineReflBack->property("GLpath").toString());
    mat->map_reflection.setPath(2, ui->lineReflLeft->property("GLpath").toString());
    mat->map_reflection.setPath(3, ui->lineReflRight->property("GLpath").toString());
    mat->map_reflection.setPath(4, ui->lineReflTop->property("GLpath").toString());
    mat->map_reflection.setPath(5, ui->lineReflBottom->property("GLpath").toString());
    return m;
}
*/

void MaterialEditor::on_buttonReflFrontSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflFront->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflFront->setProperty("GLpath", str);
	ui->lineReflFront->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflBackSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflBack->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflBack->setProperty("GLpath", str);
	ui->lineReflBack->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflLeftSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflLeft->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflLeft->setProperty("GLpath", str);
	ui->lineReflLeft->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflRightSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflRight->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflRight->setProperty("GLpath", str);
	ui->lineReflRight->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflTopSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflTop->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflTop->setProperty("GLpath", str);
	ui->lineReflTop->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflBottomSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->lineReflBottom->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->lineReflBottom->setProperty("GLpath", str);
	ui->lineReflBottom->setText(QFileInfo(str).fileName());
	materialChanged();
}


void MaterialEditor::on_buttonReflFrontClear_clicked() {
	ui->lineReflFront->setText("");
	ui->lineReflFront->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonReflBackClear_clicked() {
	ui->lineReflBack->setText("");
	ui->lineReflBack->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonReflLeftClear_clicked() {
	ui->lineReflLeft->setText("");
	ui->lineReflLeft->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonReflRightClear_clicked() {
	ui->lineReflRight->setText("");
	ui->lineReflRight->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonReflTopClear_clicked() {
	ui->lineReflTop->setText("");
	ui->lineReflTop->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonReflBottomClear_clicked() {
	ui->lineReflBottom->setText("");
	ui->lineReflBottom->setProperty("GLpath", "");
	materialChanged();
}


void MaterialEditor::on_buttonLoadCubeDir_clicked() {
	QString dir = QFileDialog::getExistingDirectory(this, "Select directory", ui->lineReflFront->property("GLpath").toString());
	if (dir.isEmpty()) return;
	/*CubeTexture cb(0);
	cb.loadPathesFromDirectory(dir);
	active = false;
	ui->lineReflFront->setProperty("GLpath", cb.path(0)); ui->lineReflFront->setText(QFileInfo(cb.path(0)).fileName());
	ui->lineReflBack->setProperty("GLpath", cb.path(1)); ui->lineReflBack->setText(QFileInfo(cb.path(1)).fileName());
	ui->lineReflLeft->setProperty("GLpath", cb.path(2)); ui->lineReflLeft->setText(QFileInfo(cb.path(2)).fileName());
	ui->lineReflRight->setProperty("GLpath", cb.path(3)); ui->lineReflRight->setText(QFileInfo(cb.path(3)).fileName());
	ui->lineReflTop->setProperty("GLpath", cb.path(4)); ui->lineReflTop->setText(QFileInfo(cb.path(4)).fileName());
	ui->lineReflBottom->setProperty("GLpath", cb.path(5)); ui->lineReflBottom->setText(QFileInfo(cb.path(5)).fileName());*/
	active = true;
	materialChanged();
}
