/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
 */

/** @file cocoa_v.h The Cocoa video driver. */

#ifndef VIDEO_COCOA_H
#define VIDEO_COCOA_H

#include "../video_driver.hpp"
#include "../../core/geometry_type.hpp"


extern bool _cocoa_video_started;

@class OTTD_CocoaWindowDelegate;
@class OTTD_CocoaWindow;
@class OTTD_CocoaView;

class VideoDriver_Cocoa : public VideoDriver {
private:
	Dimension orig_res;       ///< Saved window size for non-fullscreen mode.
	bool refresh_sys_sprites; ///< System sprites need refreshing.

public:
	bool setup; ///< Window is currently being created.

	OTTD_CocoaWindow *window;    ///< Pointer to window object
	OTTD_CocoaView *cocoaview;   ///< Pointer to view object
	CGColorSpaceRef colour_space; ///< Window colour space

	OTTD_CocoaWindowDelegate *delegate; //!< Window delegate object

public:
	VideoDriver_Cocoa(bool uses_hardware_acceleration = false);

	void Stop() override;
	void MainLoop() override;

	void MakeDirty(int left, int top, int width, int height) override;
	bool AfterBlitterChange() override;

	bool ChangeResolution(int w, int h) override;
	bool ToggleFullscreen(bool fullscreen) override;

	void ClearSystemSprites() override;
	void PopulateSystemSprites() override;

	void EditBoxLostFocus() override;

	std::vector<int> GetListOfMonitorRefreshRates() override;

	/* --- The following methods should be private, but can't be due to Obj-C limitations. --- */

	void MainLoopReal();

	/**
	 * Resize the window.
	 * @param force If true window resizing will be forced.
	 */
	virtual void AllocateBackingStore(bool force = false) = 0;

protected:
	Rect dirty_rect;    ///< Region of the screen that needs redrawing.
	bool buffer_locked; ///< Video buffer was locked by the main thread.

	Dimension GetScreenSize() const override;
	void InputLoop() override;
	bool LockVideoBuffer() override;
	void UnlockVideoBuffer() override;
	bool PollEvent() override;

	void GameSizeChanged();

	std::optional<std::string_view> Initialize();

	void UpdateVideoModes();

	bool MakeWindow(int width, int height);

	/**
	 * Allocate the view to show the game on.
	 * @return The allocated view.
	 */
	virtual NSView *AllocateDrawView() = 0;

	/**
	 * Get a pointer to the video buffer.
	 * @return The pointer.
	 */
	virtual void *GetVideoPointer() = 0;
	/** Hand video buffer back to the drawing backend. */
	virtual void ReleaseVideoPointer() {}

private:
	bool IsFullscreen();
};

class VideoDriver_CocoaQuartz : public VideoDriver_Cocoa {
private:
	int buffer_depth;     ///< Colour depth of used frame buffer
	std::unique_ptr<uint8_t[]> pixel_buffer; ///< used for direct pixel access
	std::unique_ptr<uint32_t[]> window_buffer; ///< Colour translation from palette to screen

	int window_width;     ///< Current window width in pixel
	int window_height;    ///< Current window height in pixel
	int window_pitch;

	uint32_t palette[256];  ///< Colour Palette

	void BlitIndexedToView32(int left, int top, int right, int bottom);
	void UpdatePalette(uint first_colour, uint num_colours);

public:
	CGContextRef cgcontext;      ///< Context reference for Quartz subdriver

	VideoDriver_CocoaQuartz();

	std::optional<std::string_view> Start(const StringList &param) override;
	void Stop() override;

	std::string_view GetName() const override { return "cocoa"; }

	void AllocateBackingStore(bool force = false) override;

protected:
	void Paint() override;
	void CheckPaletteAnim() override;

	NSView *AllocateDrawView() override;

	void *GetVideoPointer() override { return this->buffer_depth == 8 ? static_cast<void *>(this->pixel_buffer.get()) : static_cast<void *>(this->window_buffer.get()); }
};

class FVideoDriver_CocoaQuartz : public DriverFactoryBase {
public:
	FVideoDriver_CocoaQuartz() : DriverFactoryBase(Driver::Type::Video, 8, "cocoa", "Cocoa Video Driver") {}
	std::unique_ptr<Driver> CreateInstance() const override { return std::make_unique<VideoDriver_CocoaQuartz>(); }
};

#endif /* VIDEO_COCOA_H */
