/*
    QGLView
    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 "ui_material_editor.h"


MaterialEditor::MaterialEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::MaterialEditor();
	ui->setupUi(this);
	ui->frameReflection->hide();
	active = true;
}


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


void MaterialEditor::setMaterial(const Material & m) {
	active = false;
	ui->colorDiffuse->setColor(m.color_diffuse);
	ui->colorSpecular->setColor(m.color_specular);
	ui->colorSelfIllum->setColor(m.color_self_illumination);
	ui->checkGlass->setChecked(m.glass);
	ui->spinTransparent->setValue(m.transparency);
	ui->spinReflect->setValue(m.reflectivity);
	ui->spinIOF->setValue(m.iof);
	ui->spinDispersion->setValue(m.dispersion);
	ui->mapDiffuse->setMap(m.map_diffuse);
	ui->mapSpecular->setMap(m.map_specular);
	ui->mapSelfIllum->setMap(m.map_self_illumination);
	ui->mapSpecularity->setMap(m.map_specularity);
	ui->mapBump->setMap(m.map_normal);
	ui->mapRelief->setMap(m.map_relief);
	ui->lineReflFront->setProperty("GLpath", m.map_reflection.path(0));
	ui->lineReflFront->setText(QFileInfo(m.map_reflection.path(0)).fileName());
	ui->lineReflBack->setProperty("GLpath", m.map_reflection.path(1));
	ui->lineReflBack->setText(QFileInfo(m.map_reflection.path(1)).fileName());
	ui->lineReflLeft->setProperty("GLpath", m.map_reflection.path(2));
	ui->lineReflLeft->setText(QFileInfo(m.map_reflection.path(2)).fileName());
	ui->lineReflRight->setProperty("GLpath", m.map_reflection.path(3));
	ui->lineReflRight->setText(QFileInfo(m.map_reflection.path(3)).fileName());
	ui->lineReflTop->setProperty("GLpath", m.map_reflection.path(4));
	ui->lineReflTop->setText(QFileInfo(m.map_reflection.path(4)).fileName());
	ui->lineReflBottom->setProperty("GLpath", m.map_reflection.path(5));
	ui->lineReflBottom->setText(QFileInfo(m.map_reflection.path(5)).fileName());
	active = true;
}


Material MaterialEditor::material() {
	Material m;
	m.color_diffuse           = ui->colorDiffuse->color();
	m.color_specular          = ui->colorSpecular->color();
	m.color_self_illumination = ui->colorSelfIllum->color();
	m.glass                   = ui->checkGlass->isChecked();
	m.transparency            = ui->spinTransparent->value();
	m.reflectivity            = ui->spinReflect->value();
	m.iof                     = ui->spinIOF->value();
	m.dispersion              = ui->spinDispersion->value();
	m.map_diffuse             = ui->mapDiffuse->map();
	m.map_specular            = ui->mapSpecular->map();
	m.map_self_illumination   = ui->mapSelfIllum->map();
	m.map_specularity         = ui->mapSpecularity->map();
	m.map_normal              = ui->mapBump->map();
	m.map_relief              = ui->mapRelief->map();
	m.map_reflection.setPath(0, ui->lineReflFront->property("GLpath").toString());
	m.map_reflection.setPath(1, ui->lineReflBack->property("GLpath").toString());
	m.map_reflection.setPath(2, ui->lineReflLeft->property("GLpath").toString());
	m.map_reflection.setPath(3, ui->lineReflRight->property("GLpath").toString());
	m.map_reflection.setPath(4, ui->lineReflTop->property("GLpath").toString());
	m.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;
	GLCubeTexture 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();
}
