#include "glwidget.h"

#include "renderer_deferred_shading.h"
#include "renderer_simple.h"

#include <QVBoxLayout>


GLWidget::GLWidget(QWidget * parent): QWidget(parent) {
	view_ = new QGLView();
	view_->setFlags(windowFlags() | Qt::FramelessWindowHint);
	container = QWidget::createWindowContainer(view_, this);
	lay       = new QVBoxLayout(this);
	lay->addWidget(container);
	lay->setContentsMargins(0, 0, 0, 0);
	lay->setSpacing(0);
	setMouseTracking(true);
	setWindowIcon(QIcon("://icons/qglview.png"));
	connect(view_, &QGLView::doubleClick, this, &GLWidget::viewDoubleClicked);
}


QColor GLWidget::backColor() const {
	return view_->backColor();
}


qreal GLWidget::lineWidth() const {
	return view_->lineWidth();
}


qreal GLWidget::FOV() const {
	return view_->FOV();
}


qreal GLWidget::depthStart() const {
	return view_->depthStart();
}


qreal GLWidget::depthEnd() const {
	return view_->depthEnd();
}


QColor GLWidget::ambientColor() const {
	return view_->ambientColor();
}


bool GLWidget::isLightEnabled() const {
	return view_->isLightEnabled();
}


bool GLWidget::isGrabMouseEnabled() const {
	return view_->isGrabMouseEnabled();
}


bool GLWidget::isMouseRotateEnabled() const {
	return view_->isMouseRotateEnabled();
}


bool GLWidget::isMouseSelectionEnabled() const {
	return view_->isMouseSelectionEnabled();
}


bool GLWidget::isCameraOrbit() const {
	return view_->isCameraOrbit();
}


bool GLWidget::isHoverHaloEnabled() const {
	return view_->isHoverHaloEnabled();
}


QColor GLWidget::hoverHaloColor() const {
	return view_->hoverHaloColor();
}


qreal GLWidget::hoverHaloFillAlpha() const {
	return view_->hoverHaloFillAlpha();
}


bool GLWidget::isSelectionHaloEnabled() const {
	return view_->isSelectionHaloEnabled();
}


QColor GLWidget::selectionHaloColor() const {
	return view_->selectionHaloColor();
}


qreal GLWidget::selectionHaloFillAlpha() const {
	return view_->selectionHaloFillAlpha();
}


void GLWidget::addObject(GLObjectBase * o) {
	view_->addObject(o);
}


QByteArray GLWidget::saveCamera() {
	return view_->saveCamera();
}


void GLWidget::restoreCamera(const QByteArray & ba) {
	view_->restoreCamera(ba);
}


void GLWidget::stop() {
	view_->stop();
}


void GLWidget::start(float freq, GLRendererBase * r) {
	if (r == nullptr) r = new RendererSimple(view_);
	GLRendererBase * pr = nullptr;
	view_->setRenderer(r, &pr);
	if (pr != nullptr && pr != r) delete pr;
	view_->start(freq);
}


void GLWidget::setBackColor(const QColor & c) {
	view_->setBackColor(c);
}


void GLWidget::setLineWidth(const qreal & arg) {
	view_->setLineWidth(arg);
}


void GLWidget::setFOV(const qreal & arg) {
	view_->setFOV(arg);
}


void GLWidget::setDepthStart(const qreal & arg) {
	view_->setDepthStart(arg);
}


void GLWidget::setDepthEnd(const qreal & arg) {
	view_->setDepthEnd(arg);
}


void GLWidget::setAmbientColor(const QColor & arg) {
	view_->setAmbientColor(arg);
}


void GLWidget::setLightEnabled(const bool & arg) {
	view_->setLightEnabled(arg);
}


void GLWidget::setGrabMouseEnabled(const bool & arg) {
	view_->setGrabMouseEnabled(arg);
}


void GLWidget::setMouseRotateEnabled(const bool & arg) {
	view_->setMouseRotateEnabled(arg);
}


void GLWidget::setMouseSelectionEnabled(const bool & arg) {
	view_->setMouseSelectionEnabled(arg);
}


void GLWidget::setCameraOrbit(const bool & arg) {
	view_->setCameraOrbit(arg);
}


void GLWidget::setHoverHaloEnabled(const bool & arg) {
	view_->setHoverHaloEnabled(arg);
}


void GLWidget::setHoverHaloColor(const QColor & arg) {
	view_->setHoverHaloColor(arg);
}


void GLWidget::setHoverHaloFillAlpha(const qreal & arg) {
	view_->setHoverHaloFillAlpha(arg);
}


void GLWidget::setSelectionHaloEnabled(const bool & arg) {
	view_->setSelectionHaloEnabled(arg);
}


void GLWidget::setSelectionHaloColor(const QColor & arg) {
	view_->setSelectionHaloColor(arg);
}


void GLWidget::setSelectionHaloFillAlpha(const qreal & arg) {
	view_->setSelectionHaloFillAlpha(arg);
}


void GLWidget::viewDoubleClicked() {
	//	qDebug() << "click widget!!";
	if (view_->windowState() == Qt::WindowFullScreen) {
		//		view_->hide();
		container = QWidget::createWindowContainer(view_, this);
		lay->addWidget(container);
		container->show();
		//		show();
	} else {
		//		hide();
		view_->setParent(nullptr);
		view_->showFullScreen();
		lay->removeWidget(container);
	}
	//	qDebug() << "click widge done!";
}
