/*
    QGL MaterialMapEditor
    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_map_editor.h"

#include "glmaterial.h"
#include "gltexture_manager.h"
#include "ui_material_map_editor.h"

#include <qad_types.h>


MaterialMapEditor::MaterialMapEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::MaterialMapEditor();
	ui->setupUi(this);
	ui->buttonChannels->addActions({ui->actionInvert_R, ui->actionInvert_G, ui->actionInvert_B});
	ui->widgetMap->hide();
	active = true;
}


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


void MaterialMapEditor::resizeEvent(QResizeEvent * e) {
	QSize sz = preferredIconSize(6, this);
	ui->iconedLabel->setFixedSize(sz);
	ui->iconedLabel->setIconSize(sz);
}


void MaterialMapEditor::updateIcon() {
	ui->iconedLabel->setIcon(QIcon(TextureManager::findFile(ui->linePath->property("GLpath").toString())));
}


void MaterialMapEditor::mapChanged() {
	if (!active || !map) return;
	active          = false;
	map->use_bitmap = ui->checkMap->isChecked();
	if (ui->checkMap->isChecked()) {
		map->color_amount = ui->sliderAmount->value();
		map->color_offset = ui->sliderOffset->value();
	} else {
		map->color_amount = ui->sliderValue->value();
		map->color_offset = 0.f;
	}
	map->bitmap_scale.setX(ui->spinScaleX->value());
	map->bitmap_scale.setY(ui->spinScaleY->value());
	map->bake_options.invert_R = ui->actionInvert_R->isChecked();
	map->bake_options.invert_G = ui->actionInvert_G->isChecked();
	map->bake_options.invert_B = ui->actionInvert_B->isChecked();
	active                     = true;
	emit changed();
}


void MaterialMapEditor::setMap(Map * m) {
	active = false;
	map    = m;
	setEnabled(m);
	if (!map) return;
	ui->stackedWidget->setCurrentIndex(map->use_bitmap ? 1 : 0);
	ui->checkMap->setChecked(map->use_bitmap);
	ui->sliderValue->setValue(map->color_amount);
	ui->sliderAmount->setValue(map->color_amount);
	ui->sliderOffset->setValue(map->color_offset);
	ui->spinScaleX->setValue(map->bitmap_scale.x());
	ui->spinScaleY->setValue(map->bitmap_scale.y());
	ui->linePath->setProperty("GLpath", map->bitmap_path);
	ui->linePath->setText(QFileInfo(map->bitmap_path).fileName());
	ui->actionInvert_R->setChecked(map->bake_options.invert_R);
	ui->actionInvert_G->setChecked(map->bake_options.invert_G);
	ui->actionInvert_B->setChecked(map->bake_options.invert_B);
	updateIcon();
	active = true;
}


void MaterialMapEditor::configure(QString title, bool has_color) {
	ui->labelTitle->setText(title);
	ui->colorButton->setVisible(has_color);
}


void MaterialMapEditor::setColor(QColor c) {
	active = false;
	ui->colorButton->setColor(c);
	active = true;
}


QColor MaterialMapEditor::color() const {
	return ui->colorButton->color();
}

/*
Map MaterialMapEditor::map() {
    Map m;
    map->color_amount = ui->sliderAmount->value();
    map->color_offset = ui->sliderOffset->value();
    map->bitmap_scale.setX(ui->spinScaleX->value());
    map->bitmap_scale.setY(ui->spinScaleY->value());
    map->bitmap_path = ui->linePath->property("GLpath").toString();
    return m;
}
*/

void MaterialMapEditor::on_buttonSelect_clicked() {
	if (!map) return;
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->linePath->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->linePath->setProperty("GLpath", str);
	ui->linePath->setText(QFileInfo(str).fileName());
	map->setBitmapPath(str);
	updateIcon();
	mapChanged();
}


void MaterialMapEditor::on_buttonClear_clicked() {
	if (!map) return;
	ui->linePath->setText("");
	ui->linePath->setProperty("GLpath", "");
	map->clearBitmap();
	updateIcon();
	mapChanged();
}


void MaterialMapEditor::on_checkMap_toggled(bool checked) {
	if (checked) {
		ui->sliderAmount->setValue(ui->sliderValue->value());
		resizeEvent(0);
	} else
		ui->sliderValue->setValue(ui->sliderAmount->value());
	ui->stackedWidget->setCurrentIndex(checked ? 1 : 0);
	mapChanged();
}


void MaterialMapEditor::on_actionInvert_R_toggled(bool checked) {
	mapChanged();
}


void MaterialMapEditor::on_actionInvert_G_toggled(bool checked) {
	mapChanged();
}


void MaterialMapEditor::on_actionInvert_B_toggled(bool checked) {
	mapChanged();
}
