#pragma once
#include "windows/windows_render_context.h"
#include <Eigen/Eigen>

class mirage_window;

struct mirage_window_state {
	virtual ~mirage_window_state() {
		clear();
	}
	sg_buffer buffer;
	sg_swapchain swapchain;
	sg_bindings bindings;
	sg_pipeline pipeline;
	bool vsync = true;

	virtual void clear() {}
	virtual void present() {}
	virtual void resize(const Eigen::Vector2i& size) {}
};

class mirage_window {
public:
	bool create_window(int width, int height, const wchar_t* title);

	void show();
	void hide();
	void close();
	void maximize();
	void minimize();

	void resize(int width, int height);
	void resize(const Eigen::Vector2i& size) { resize(size.x(), size.y()); }
	void move(int x, int y);
	void move(const Eigen::Vector2i& pos) { move(pos.x(), pos.y()); }

	[[nodiscard]] Eigen::Vector2i get_window_size() const;
	[[nodiscard]] Eigen::Vector2i get_window_position() const;
	[[nodiscard]] Eigen::Vector2i get_window_frame_size() const;
	// 创建从屏幕坐标到DCI坐标的正交投影矩阵
	[[nodiscard]] static Eigen::Matrix4f create_screen_to_dci_matrix(float in_screen_width, float in_screen_height);
	[[nodiscard]] Eigen::Matrix4f create_screen_to_dci_matrix() const;

	[[nodiscard]] void* get_window_handle() const;

	// style functions
	mirage_window& set_title(const wchar_t* title);
	mirage_window& set_has_minimize_button(bool has_minimize_button);
	mirage_window& set_has_maximize_button(bool has_maximize_button);
	mirage_window& set_has_close_button(bool has_close_button);
	mirage_window& set_has_border(bool has_border);
	mirage_window& set_has_caption(bool has_caption);
	mirage_window& set_has_resizable_border(bool has_resizable_border);
	mirage_window& set_topmost(bool is_topmost);
	// end style functions

	[[nodiscard]] bool close_requested() const { return close_request; }
	static bool poll_events();
	static const std::vector<mirage_window*>& get_windows();

	void on_resize(int width, int height);

	std::unique_ptr<mirage_window_state> state;
private:
	void* window_handle{};
	bool close_request = false;
};
