/*
    QGL ViewEditor
    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 "view_editor.h"

#include "ui_view_editor.h"

#include <QFileDialog>
#include <colorbutton.h>
#include <spinslider.h>


ViewEditor::ViewEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::ViewEditor();
	ui->setupUi(this);
	ui->scrollArea->viewport()->setAutoFillBackground(false);
	ui->scrollAreaWidgetContents->setAutoFillBackground(false);
	ui->checkCameraLight->setCheckState(Qt::PartiallyChecked);
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
	ui->spinDepthStart->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType);
#endif
	for (int i = 6; i <= 13; ++i) {
		QSize sz(pow2(i), pow2(i));
		ui->comboMapSizeTexture->addItem(QString::number(sz.width()), sz);
		ui->comboMapSizeShadow->addItem(QString::number(sz.width()), sz);
	}
}


void ViewEditor::assignQGLView(QGLView * v) {
	view = v;
	if (!view) return;
	active = false;
	ui->spinFOV->setValue(view->FOV());
	ui->spinDepthStart->setValue(view->depthStart());
	ui->checkHoverHalo->setChecked(view->isHoverHaloEnabled());
	ui->checkSelectionHalo->setChecked(view->isSelectionHaloEnabled());
	ui->spinHoverHaloFill->setValue(view->hoverHaloFillAlpha());
	ui->spinSelectionHaloFill->setValue(view->selectionHaloFillAlpha());
	ui->colorHoverHalo->setColor(view->hoverHaloColor());
	ui->colorSelectionHalo->setColor(view->selectionHaloColor());
	ui->checkFXAA->setChecked(view->FXAA());
	ui->checkCameraOrbit->setChecked(view->isCameraOrbit());
	ui->checkCameraLight->setCheckState((Qt::CheckState)view->cameraLightMode());
	ui->checkService->setChecked(view->isServiceMode());
	ui->lineHDR->setProperty("GLpath", view->environmentMapFile());
	ui->lineHDR->setText(QFileInfo(view->environmentMapFile()).fileName());
	ui->checkVSync->setChecked(view->getVSync());
	ui->colorFogBack->setColor(view->fogColor());
	ui->spinFogDecay->setValue(view->fogDecay());
	ui->spinFogDensity->setValue(view->fogDensity());
	ui->checkSoftShadows->setChecked(view->softShadows());
	ui->spinSoftShadowSamples->setValue(view->softShadowsSamples());
	ui->spinSoftShadowQuality->setValue(view->softShadowsQuality());
	auto setMapSize = [](QComboBox * combo, QSize sz) {
		for (int i = 0; i < combo->count(); ++i) {
			if (combo->itemData(i).toSize() == sz) {
				combo->setCurrentIndex(i);
				break;
			}
		}
	};
	setMapSize(ui->comboMapSizeTexture, view->textureMapSize());
	setMapSize(ui->comboMapSizeShadow, view->shadowMapSize());
	active = true;
}


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


void ViewEditor::on_spinFOV_valueChanged(double val) {
	if (!view || !active) return;
	view->setFOV(val);
}


void ViewEditor::on_spinDepthStart_valueChanged(double val) {
	if (!view || !active) return;
	view->setDepthStart(val);
}


void ViewEditor::on_spinViewGamma_valueChanged(double val) {
	if (!view || !active) return;
	view->setGamma(val);
}


void ViewEditor::on_comboViewRenderMode_currentIndexChanged(int val) {
	if (!view || !active) return;
	static int modes[] = {GL_POINT, GL_LINE, GL_FILL};
	view->setRenderMode((QGLView::RenderMode)modes[val]);
}


void ViewEditor::on_groupHalos_clicked(bool val) {
	if (!view || !active) return;
	view->setHoverHaloEnabled(val && ui->checkHoverHalo->isChecked());
	view->setSelectionHaloEnabled(val && ui->checkSelectionHalo->isChecked());
}


void ViewEditor::on_checkHoverHalo_clicked(bool val) {
	if (!view || !active) return;
	view->setHoverHaloEnabled(val);
}


void ViewEditor::on_checkSelectionHalo_clicked(bool val) {
	if (!view || !active) return;
	view->setSelectionHaloEnabled(val);
}


void ViewEditor::on_spinHoverHaloFill_valueChanged(double val) {
	if (!view || !active) return;
	view->setHoverHaloFillAlpha(val);
}


void ViewEditor::on_spinSelectionHaloFill_valueChanged(double val) {
	if (!view || !active) return;
	view->setSelectionHaloFillAlpha(val);
}


void ViewEditor::on_colorHoverHalo_colorChanged(QColor color) {
	if (!view || !active) return;
	view->setHoverHaloColor(color);
}


void ViewEditor::on_colorSelectionHalo_colorChanged(QColor color) {
	if (!view || !active) return;
	view->setSelectionHaloColor(color);
}


void ViewEditor::on_checkAutoExposure_toggled(bool val) {
	if (!view || !active) return;
	view->setAutoExposure(val);
}


void ViewEditor::on_checkFXAA_clicked(bool val) {
	if (!view || !active) return;
	view->setFXAA(val);
}


void ViewEditor::on_checkCameraOrbit_clicked(bool val) {
	if (!view || !active) return;
	view->setCameraOrbit(val);
}


void ViewEditor::on_checkService_clicked(bool val) {
	if (!view || !active) return;
	view->setServiceMode(val);
}


void ViewEditor::on_checkCameraLight_stateChanged(int s) {
	if (!view || !active) return;
	view->setCameraLightMode((QGLView::CameraLightMode)s);
}


void ViewEditor::on_buttonHDRClear_clicked() {
	if (!view || !active) return;
	ui->lineHDR->setText("");
	ui->lineHDR->setProperty("GLpath", "");
	view->setEnvironmentMapFile("");
}


void ViewEditor::on_buttonHDRSelect_clicked() {
	if (!view || !active) return;
	QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineHDR->property("GLpath").toString(), "Radiance HDR(*.hdr)");
	if (str.isEmpty()) return;
	ui->lineHDR->setText(QFileInfo(str).fileName());
	ui->lineHDR->setProperty("GLpath", str);
	view->setEnvironmentMapFile(str);
}


void ViewEditor::on_colorFogBack_colorChanged(const QColor & color) {
	if (!view || !active) return;
	view->setFogColor(color);
}


void ViewEditor::on_spinFogDensity_valueChanged(double arg1) {
	if (!view || !active) return;
	view->setFogDensity(arg1);
}


void ViewEditor::on_spinFogDecay_valueChanged(double arg1) {
	if (!view || !active) return;
	view->setFogDecay(arg1);
}


void ViewEditor::on_comboMapSizeTexture_currentIndexChanged(int index) {
	if (!view || !active) return;
	view->setTextureMapSize(ui->comboMapSizeTexture->itemData(index).toSize());
}


void ViewEditor::on_comboMapSizeShadow_currentIndexChanged(int index) {
	if (!view || !active) return;
	view->setShadowMapSize(ui->comboMapSizeShadow->itemData(index).toSize());
}


void ViewEditor::on_checkSoftShadows_clicked(bool arg1) {
	if (!view || !active) return;
	view->setSoftShadows(arg1);
}


void ViewEditor::on_spinSoftShadowSamples_valueChanged(double arg1) {
	if (!view || !active) return;
	view->setSoftShadowsSamples(arg1);
}


void ViewEditor::on_spinSoftShadowQuality_valueChanged(double arg1) {
	if (!view || !active) return;
	view->setSoftShadowsQuality(arg1);
}


void ViewEditor::on_checkVSync_clicked(bool val) {
	if (!view || !active) return;
	view->setVSync(val);
}
