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

#include "glcamera.h"
#include "ui_globject_editor.h"


GLObjectEditor::GLObjectEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::GLObjectEditor();
	ui->setupUi(this);
	active = true;
	object = 0;
	rmodes << GLObjectBase::View << GLObjectBase::Point << GLObjectBase::Line << GLObjectBase::Fill;
	ui->groupLight->setEnabled(false);
	ui->groupLight->setVisible(false);
	ui->groupCamera->setEnabled(false);
	ui->groupCamera->setVisible(false);
}


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


void GLObjectEditor::setObject(GLObjectBase * o) {
	object = o;
	if (object == 0) {
		ui->groupLight->setEnabled(false);
		ui->groupLight->setVisible(false);
		ui->groupCamera->setEnabled(false);
		ui->groupCamera->setVisible(false);
		return;
	}
	ui->buttonDiscardRawMatrix->setEnabled(o->isRawMatrix());
	active = false;
	ui->spinPosX->setValue(object->posX());
	ui->spinPosY->setValue(object->posY());
	ui->spinPosZ->setValue(object->posZ());
	ui->spinRotationX->setValue(object->rotationX());
	ui->spinRotationY->setValue(object->rotationY());
	ui->spinRotationZ->setValue(object->rotationZ());
	ui->spinScaleX->setValue(object->scaleX());
	ui->spinScaleY->setValue(object->scaleY());
	ui->spinScaleZ->setValue(object->scaleZ());
	ui->spinLineWidth->setValue(object->lineWidth());
	ui->checkVisible->setChecked(object->isVisible());
	ui->checkAcceptLight->setChecked(object->isAcceptLight());
	ui->checkAcceptFog->setChecked(object->isAcceptFog());
	ui->checkCastShadows->setChecked(object->isCastShadows());
	ui->checkReceiveShadows->setChecked(object->isReceiveShadows());
	ui->comboRenderMode->setCurrentIndex(rmodes.indexOf(object->renderMode()));
	ui->groupLight->setEnabled(object->type() == GLObjectBase::glLight);
	ui->groupLight->setVisible(object->type() == GLObjectBase::glLight);
	if (object->type() == GLObjectBase::glLight) {
		Light * l = globject_cast<Light *>(object);
		// bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone;
		ui->buttonLightColor->setColor(l->color());
		ui->comboLightType->setCurrentIndex(l->light_type);
		ui->spinLightIntensity->setValue(l->intensity);
		ui->spinLightDecayConst->setValue(l->decay_const);
		ui->spinLightDecayLinear->setValue(l->decay_linear);
		ui->spinLightDecayQuadratic->setValue(l->decay_quadratic);
		ui->spinLightAngleStart->setValue(l->angle_start);
		ui->spinLightAngleEnd->setValue(l->angle_end);
		ui->spinLightDirectionX->setValue(l->direction.x());
		ui->spinLightDirectionY->setValue(l->direction.y());
		ui->spinLightDirectionZ->setValue(l->direction.z());
	}
	ui->groupCamera->setEnabled(object->type() == GLObjectBase::glCamera);
	ui->groupCamera->setVisible(object->type() == GLObjectBase::glCamera);
	if (object->type() == GLObjectBase::glCamera) {
		Camera * c = globject_cast<Camera *>(object);
		ui->checkCameraMirrorX->setChecked(c->isMirrorX());
		ui->checkCameraMirrorY->setChecked(c->isMirrorY());
		ui->spinCameraFOV->setValue(c->FOV());
		ui->spinCameraDepthStart->setValue(c->depthStart());
		ui->spinCameraDepthEnd->setValue(c->depthEnd());
	}
	active = true;
}


void GLObjectEditor::objectChanged() {
	if (!active || object == 0) return;
	if (!object->isRawMatrix()) {
		object->setPosX(ui->spinPosX->value());
		object->setPosY(ui->spinPosY->value());
		object->setPosZ(ui->spinPosZ->value());
		object->setRotationX(ui->spinRotationX->value());
		object->setRotationY(ui->spinRotationY->value());
		object->setRotationZ(ui->spinRotationZ->value());
		object->setScaleX(ui->spinScaleX->value());
		object->setScaleY(ui->spinScaleY->value());
		object->setScaleZ(ui->spinScaleZ->value());
	}
	object->setLineWidth(ui->spinLineWidth->value());
	object->setVisible(ui->checkVisible->isChecked());
	object->setAcceptLight(ui->checkAcceptLight->isChecked());
	object->setAcceptFog(ui->checkAcceptFog->isChecked());
	object->setCastShadows(ui->checkCastShadows->isChecked());
	object->setReceiveShadows(ui->checkReceiveShadows->isChecked());
	object->setRenderMode((GLObjectBase::RenderMode)rmodes[ui->comboRenderMode->currentIndex()]);
	if (object->type() == GLObjectBase::glLight) {
		Light * l = globject_cast<Light *>(object);
		// bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone;
		l->setColor(ui->buttonLightColor->color());
		l->light_type      = (Light::Type)ui->comboLightType->currentIndex();
		l->intensity       = ui->spinLightIntensity->value();
		l->decay_const     = ui->spinLightDecayConst->value();
		l->decay_linear    = ui->spinLightDecayLinear->value();
		l->decay_quadratic = ui->spinLightDecayQuadratic->value();
		l->angle_start     = ui->spinLightAngleStart->value();
		l->angle_end       = ui->spinLightAngleEnd->value();
		l->direction =
			QVector3D(ui->spinLightDirectionX->value(), ui->spinLightDirectionY->value(), ui->spinLightDirectionZ->value()).normalized();
	}
	if (object->type() == GLObjectBase::glCamera) {
		Camera * c = globject_cast<Camera *>(object);
		c->setMirrorX(ui->checkCameraMirrorX->isChecked());
		c->setMirrorY(ui->checkCameraMirrorY->isChecked());
		c->setFOV(ui->spinCameraFOV->value());
		c->setDepthStart(ui->spinCameraDepthStart->value());
		c->setDepthEnd(ui->spinCameraDepthEnd->value());
	}
	emit changed();
}


void GLObjectEditor::on_spinLightAngleStart_valueChanged(double v) {
	if (ui->spinLightAngleEnd->value() < v) ui->spinLightAngleEnd->setValue(v);
}


void GLObjectEditor::on_spinLightAngleEnd_valueChanged(double v) {
	if (ui->spinLightAngleStart->value() > v) ui->spinLightAngleStart->setValue(v);
}


void GLObjectEditor::on_buttonDiscardRawMatrix_clicked() {
	if (!active || !object) return;
	object->setPosX(ui->spinPosX->value());
	object->setPosY(ui->spinPosY->value());
	object->setPosZ(ui->spinPosZ->value());
	object->setRotationX(ui->spinRotationX->value());
	object->setRotationY(ui->spinRotationY->value());
	object->setRotationZ(ui->spinRotationZ->value());
	object->setScaleX(ui->spinScaleX->value());
	object->setScaleY(ui->spinScaleY->value());
	object->setScaleZ(ui->spinScaleZ->value());
	ui->buttonDiscardRawMatrix->setEnabled(false);
}
