/*
	Copyright (C) 2021-2025 Mark Tyler

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program in the file COPYING.
*/

#ifndef MTLANTER_QT_H_
#define MTLANTER_QT_H_

#undef BIN_NAME
#define BIN_NAME "mtlanter" BIN_SUFFIX

#include "lanter_be.h"



#ifdef U_TK_QT5
	#include <mtqex5.h>
#endif

#ifdef U_TK_QT6
	#include <mtqex6.h>
#endif



#include <QOpenGLWidget>



// Functions return: 0 = success, NULL = fail; unless otherwise stated.



#ifdef __cplusplus
extern "C" {
#endif

// C API



#ifdef __cplusplus
}

// C++ API



class MainWindow;
class TerraView;



class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	explicit MainWindow ( CommandLine & cline );

	void press_quit ();
	void press_change_mode ();
	void output_image_file ();

	CommandLine const & cline() { return m_cline; }
	TerraMap	& map() { return m_map; }
	TerraScene	& scene() { return m_scene; }

private slots:

protected:
	void closeEvent ( QCloseEvent * ev );

private:

	enum
	{
		MODE_NULL		= -1,
		MODE_INTERACTIVE	= 0,
		MODE_ANIMATION		= 2
	};

	void store_window_geometry ();
	int export_image ( std::string const & filename );

	int init_map ();

	void set_mode ( int mode );

/// ----------------------------------------------------------------------------

	int	m_mode		= MODE_NULL;

/// Low level private references

	CommandLine		& m_cline;
	mtKit::UserPrefs	& m_uprefs;
	MemPrefs	const	& m_mprefs;

/// Low level private members

	TerraMap	m_map;
	TerraScene	m_scene;
	TerraAnimate	m_animate;

	TerraView	* m_terraview_a;
	TerraView	* m_terraview_b;

/// ----------------------------------------------------------------------------

	MTKIT_RULE_OF_FIVE( MainWindow )
};



class TerraView : public QOpenGLWidget, protected QOpenGLFunctions
{
	Q_OBJECT

public:
	TerraView ( MainWindow & mainwindow );

	void move_camera_home ( int top );

	TerraCamera	& camera() { return m_camera; }

public slots:

signals:

protected:
	void initializeGL ()					override;
	void paintGL ()						override;
	void resizeGL ( int width, int height )			override;
	void mousePressEvent ( QMouseEvent * event )		override;
	void mouseReleaseEvent ( QMouseEvent * event )		override;
	void mouseMoveEvent ( QMouseEvent * event )		override;
	void keyPressEvent ( QKeyEvent * event )		override;

private:
	MainWindow	& m_mainwindow;

	TerraCamera	m_camera;

	QPoint		m_last_pos;
	int		m_mbutton_down	= 0;
};



#endif		// C++ API



#endif		// MTLANTER_QT_H_

