/*
    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/>.
*/

#ifndef QGLVIEW_H
#define QGLVIEW_H

#include "glcamera.h"
#include "glframebuffer.h"
#include "glprimitives.h"
#include "glscene.h"
#include "mouse_controller.h"
#include "openglwindow.h"
#include "qglengine_core_export.h"
#include "renderer.h"

#include <QElapsedTimer>
#include <QMenu>

class QGLENGINE_CORE_EXPORT QGLView
	: public OpenGLWindow
	, public ParameteredObject {
	friend class Renderer;
	friend class RendererSelection;
	Q_OBJECT
	Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth)
	Q_PROPERTY(float FOV READ FOV WRITE setFOV)
	Q_PROPERTY(float depthStart READ depthStart WRITE setDepthStart)
	Q_PROPERTY(float gamma READ gamma WRITE setGamma)
	Q_PROPERTY(bool autoExposure READ autoExposure WRITE setAutoExposure)
	Q_PROPERTY(QColor fogColor READ fogColor WRITE setFogColor)
	Q_PROPERTY(bool fogEnabled READ isFogEnabled WRITE setFogEnabled)
	Q_PROPERTY(float fogDensity READ fogDensity WRITE setFogDensity)
	Q_PROPERTY(float fogDecay READ fogDecay WRITE setFogDecay)
	Q_PROPERTY(int renderMode READ renderMode WRITE setRenderMode)
	Q_PROPERTY(bool grabMouse READ isGrabMouseEnabled WRITE setGrabMouseEnabled)
	Q_PROPERTY(bool mouseRotate READ isMouseRotateEnabled WRITE setMouseRotateEnabled)
	Q_PROPERTY(bool mouseSelection READ isMouseSelectionEnabled WRITE setMouseSelectionEnabled)
	Q_PROPERTY(bool cameraOrbit READ isCameraOrbit WRITE setCameraOrbit)
	Q_PROPERTY(bool hoverHalo READ isHoverHaloEnabled WRITE setHoverHaloEnabled)
	Q_PROPERTY(QColor hoverHaloColor READ hoverHaloColor WRITE setHoverHaloColor)
	Q_PROPERTY(float hoverHaloFillAlpha READ hoverHaloFillAlpha WRITE setHoverHaloFillAlpha)
	Q_PROPERTY(bool selectionHalo READ isSelectionHaloEnabled WRITE setSelectionHaloEnabled)
	Q_PROPERTY(QColor selectionHaloColor READ selectionHaloColor WRITE setSelectionHaloColor)
	Q_PROPERTY(float selectionHaloFillAlpha READ selectionHaloFillAlpha WRITE setSelectionHaloFillAlpha)
	Q_PROPERTY(Qt::MouseButton selectionButton READ selectionButton WRITE setSelectionButton)
	Q_PROPERTY(Qt::KeyboardModifier selectionModifier READ selectionModifier WRITE setSelectionModifier)
	Q_PROPERTY(Scene::SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
	Q_PROPERTY(QSize textureMapSize READ textureMapSize WRITE setTextureMapSize)
	Q_PROPERTY(QSize shadowMapSize READ shadowMapSize WRITE setShadowMapSize)
	Q_PROPERTY(int softShadowsSamples READ softShadowsSamples WRITE setSoftShadowsSamples)
	Q_PROPERTY(float softShadowsQuality READ softShadowsQuality WRITE setSoftShadowsQuality)
	Q_PROPERTY(bool softShadows READ softShadows WRITE setSoftShadows)

public:
	QGLView();
	virtual ~QGLView();

	enum CameraLightMode {
		clmOff,
		clmAuto,
		clmOn,
	};
	enum RenderMode {
		rmPoint = GL_POINT,
		rmLine  = GL_LINE,
		rmFill  = GL_FILL
	};

	Q_ENUM(CameraLightMode)

	void stop();
	void start(float freq = 0.);

	float lineWidth() const { return lineWidth_; }
	float FOV() const { return camera()->FOV(); }
	float depthStart() const { return camera()->depthStart(); }
	float currentFPS() const { return fps_; }
	float gamma() const { return renderer_.gamma_; }
	bool autoExposure() const { return renderer_.tone_proc.enabled; }
	int maxAnisotropicLevel() const { return max_anisotropic; }
	QString environmentMapFile() const { return renderer_.tex_env.fileHDR(); }
	bool FXAA() const { return FXAA_; }
	void setFXAA(bool on) { FXAA_ = on; }

	QColor fogColor() const { return fogColor_; }
	float fogDensity() const { return fogDensity_; }
	float fogDecay() const { return fogDecay_; }
	bool isFogEnabled() const { return fogEnabled_; }
	bool isLightEnabled() const { return lightEnabled_; }
	bool isGrabMouseEnabled() const { return mouse.isGrabMouseEnabled(); }
	bool isMouseRotateEnabled() const { return mouse.isMouseRotateEnabled(); }
	bool isMouseSelectionEnabled() const { return mouse.isMouseSelectionEnabled(); }
	bool isCameraOrbit() const { return mouse.isCameraOrbit(); }
	bool isHoverHaloEnabled() const { return hoverHalo_; }
	QColor hoverHaloColor() const { return hoverHaloColor_; }
	float hoverHaloFillAlpha() const { return hoverHaloFill_; }
	bool isSelectionHaloEnabled() const { return selectionHalo_; }
	QColor selectionHaloColor() const { return selectionHaloColor_; }
	float selectionHaloFillAlpha() const { return selectionHaloFill_; }

	int renderMode() const { return (int)render_mode; }
	void setRenderMode(int mode) { render_mode = (RenderMode)mode; }

	bool isServiceMode() const { return renderer_.edit_mode; }
	void setServiceMode(bool yes) { renderer_.edit_mode = yes; }


	Scene::SelectionMode selectionMode() const { return scene_->selectionMode(); }
	Qt::MouseButton selectionButton() const { return mouse.selectionButton(); }
	Qt::KeyboardModifier selectionModifier() const { return mouse.selectionModifier(); }

	void setSelectionMode(Scene::SelectionMode m) { scene_->setSelectionMode(m); }
	void setSelectionButton(Qt::MouseButton v) { mouse.setSelectionButton(v); }
	void setSelectionModifier(Qt::KeyboardModifier v) { mouse.setSelectionModifier(v); }

	void selectObject(ObjectBase * o, bool add_to_selection = false) { scene_->selectObject(o, add_to_selection); }
	void clearSelection() { scene_->clearSelection(); }
	ObjectBaseList selectedObjects(bool top_only = false) const { return scene_->selectedObjects(top_only); }
	QList<Light *> selectedLights() const;
	QList<Camera *> selectedCameras() const;
	ObjectBase * selectedObject() const { return scene_->selectedObject(); }

	TextureManager * textureManager() { return renderer_.textures_manager; }
	void reloadTextures();

	Scene * scene() { return scene_; }
	void focusOn(const Box3D & bb);
	void setCameraLightMode(CameraLightMode m) { renderer_.setCameraLightMode(m); }
	CameraLightMode cameraLightMode() const { return (CameraLightMode)renderer_.cameraLightMode(); }

	Camera * camera() { return camera_; }
	const Camera * camera() const { return camera_; }
	void setCamera(Camera * camera) { camera_ = camera; }
	void setDefaultCamera() { camera_ = default_camera; }
	bool isDefaultCamera() const { return camera_ == default_camera; }
	QByteArray saveCamera();
	void restoreCamera(const QByteArray & ba);

	QImage materialThumbnail(Material * m) { return renderer_.materialThumbnail(m); }
	void setCurrentAction(RendererService::HandleAction ha) { renderer_.rend_service.setCurrentAction(ha); }
	void setContextActions(QList<QAction *> al) {
		context_menu.clear();
		context_menu.addActions(al);
	}
	void popupMenu(const QPoint & pos, QAction * at = nullptr) { context_menu.popup(pos, at); }
	void setGrabImage(bool on) { renderer_.setGrabImage(on); }
	bool isGrabImage() const { return renderer_.isGrabImage(); }
	QImage getImage() const { return renderer_.getImage(); }

	void setShadowMapSize(QSize sz);
	void setTextureMapSize(QSize sz);
	QSize shadowMapSize() const;
	QSize textureMapSize() const;

	int softShadowsSamples() const { return soft_shadows_samples; }
	void setSoftShadowsSamples(int s) { soft_shadows_samples = s; }
	float softShadowsQuality() const { return soft_shadows_quality; }
	void setSoftShadowsQuality(float s) { soft_shadows_quality = s; }
	bool softShadows() const { return soft_shadows; }
	void setSoftShadows(bool on) { soft_shadows = on; }

	GLfloat aspect, iaspect;
	Renderer renderer_;

protected:
	void render();
	void resizeEvent(QResizeEvent * e);

	void timerEvent(QTimerEvent *);
	void initialize();
	void resizeGL(int width, int height);
	void mousePressEvent(QMouseEvent * e) { mouse.mousePressEvent(e); }
	void mouseMoveEvent(QMouseEvent * e) { mouse.mouseMoveEvent(e); }
	void mouseReleaseEvent(QMouseEvent * e) { mouse.mouseReleaseEvent(e); }
	void wheelEvent(QWheelEvent * e) { mouse.wheelEvent(e); }
	void mouseDoubleClickEvent(QMouseEvent * e) { mouse.mouseDoubleClickEvent(e); }
	void leaveEvent(QEvent *);

	void keyPressEvent(QKeyEvent * e);
	void keyReleaseEvent(QKeyEvent * e);
	void focusOutEvent(QFocusEvent *);

	void checkCaps();

private:
	void processKeys();
	bool setupViewport();

	Scene * scene_;
	Camera *camera_, *default_camera;
	MouseController mouse;
	QMenu context_menu;
	QSet<int> keys_;
	QColor fogColor_, hoverHaloColor_, selectionHaloColor_;
	QElapsedTimer time;
	GLint max_anisotropic, max_texture_chanels;
	RenderMode render_mode;
	QSize prev_size, shadow_map_size;
	float lineWidth_, soft_shadows_quality;
	float fps_, fps_tm, fogDensity_, fogDecay_;
	float hoverHaloFill_, selectionHaloFill_, m_motionBlurFactor;
	int timer, fps_cnt, sh_id_loc, soft_shadows_samples;
	bool fogEnabled_, lightEnabled_, FXAA_;
	bool shaders_supported, shaders_bind;
	bool hoverHalo_, selectionHalo_;
	bool is_init, soft_shadows;

private slots:
	void __destroyed();
	void __objectDeleted(ObjectBase * o);

public slots:
	void setLineWidth(const float & arg) { lineWidth_ = arg; }
	void setFOV(const float & arg) { camera()->setFOV(arg); }
	void setDepthStart(const float & arg) { camera()->setDepthStart(arg); }
	void setGamma(const float & arg) { renderer_.gamma_ = arg; }
	void setAutoExposure(bool arg) { renderer_.tone_proc.enabled = arg; }
	void setEnvironmentMapFile(QString file) {
		renderer_.tex_env.setFileHDR(file);
		renderer_.recreateMaterialThumbnails(true);
	}
	void setFogColor(const QColor & arg) { fogColor_ = arg; }
	void setFogDensity(const float & arg) { fogDensity_ = arg; }
	void setFogDecay(const float & arg) { fogDecay_ = arg; }
	void setFogEnabled(const bool & arg) { fogEnabled_ = arg; }
	void setLightEnabled(const bool & arg) { lightEnabled_ = arg; }
	void setGrabMouseEnabled(const bool & arg) { mouse.setGrabMouseEnabled(arg); }
	void setMouseRotateEnabled(const bool & arg) { mouse.setMouseRotateEnabled(arg); }
	void setMouseSelectionEnabled(const bool & arg) { mouse.setMouseSelectionEnabled(arg); }
	void setCustomMouseMove(const bool & arg) { mouse.setCustomMouseMove(arg); }
	void setCameraOrbit(const bool & arg) { mouse.setCameraOrbit(arg); }
	void setHoverHaloEnabled(const bool & arg) { hoverHalo_ = arg; }
	void setHoverHaloColor(const QColor & arg) { hoverHaloColor_ = arg; }
	void setHoverHaloFillAlpha(const float & arg) { hoverHaloFill_ = arg; }
	void setSelectionHaloEnabled(const bool & arg) { selectionHalo_ = arg; }
	void setSelectionHaloColor(const QColor & arg) { selectionHaloColor_ = arg; }
	void setSelectionHaloFillAlpha(const float & arg) { selectionHaloFill_ = arg; }

	void reloadShaders() { renderer_.reloadShaders(); }

signals:
	void glBeginPaint();
	void glEndPaint();
	void glKeyPressEvent(QKeyEvent * e);
	void glKeyReleaseEvent(QKeyEvent * e);
	void glMousePressEvent(QMouseEvent * e);
	void glMouseMoveEvent(QMouseEvent * e);
	void glMouseReleaseEvent(QMouseEvent * e);
	void glWheelEvent(QWheelEvent * e);
	void glResize(int, int);
	void glInitializeDone();
	void cameraPosChanged(QVector3D pos);
	void keyEvent(Qt::Key key, Qt::KeyboardModifiers mod);
	void customMouseMoveEvent(QPoint curpos, QPoint lastpos, Qt::MouseButtons buttons);

	void hoverChanged(ObjectBase * cur, ObjectBase * prev);
	void selectionChanged();
	void objectsPositionChanged();
	void materialsChanged();
	void materialThumbnailCreated(Material *);
	void doubleClick();
};

#endif // QGLVIEW_H
