#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
 * OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
 *
 * OpenRCT2 is the work of many authors, a full list can be found in contributors.md
 * For more information, visit https://github.com/OpenRCT2/OpenRCT2
 *
 * OpenRCT2 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.
 *
 * A full copy of the GNU General Public License can be found in licence.txt
 *****************************************************************************/
#pragma endregion

#include "../addresses.h"
#include "../common.h"
#include "../localisation/localisation.h"
#include "../interface/window.h"
#include "../platform/platform.h"
#include "../object.h"
#include "../world/water.h"
#include "drawing.h"

// HACK These were originally passed back through registers
int gLastDrawStringX;
int gLastDrawStringY;

uint8* _screenDirtyBlocks = NULL;
int _screenDirtyBlocksSize = 0;
uint16 _screenDirtyBlockWidth;
uint16 _screenDirtyBlockHeight;
uint16 _screenDirtyBlockColumns;
uint16 _screenDirtyBlockRows;
uint8 _screenDirtyBlockShiftX;
uint8 _screenDirtyBlockShiftY;

rct_drawpixelinfo gScreenDPI;
rct_drawpixelinfo gWindowDPI;

#define MAX_RAIN_PIXELS 0xFFFE
static uint32 _rainPixels[MAX_RAIN_PIXELS];
static uint32 _numRainPixels;

uint8 gGamePalette[256 * 4];
uint32 gPaletteEffectFrame;

uint32 gPickupPeepImage;
sint32 gPickupPeepX;
sint32 gPickupPeepY;


//Originally 0x9ABE0C, 12 elements from 0xF3 are the peep top colour, 12 elements from 0xCA are peep trouser colour
const uint8 peep_palette[0x100] = {
	0x00, 0xF3, 0xF4, 0xF5, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x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DC, 0xDD, 0xDE, 0xDF,
	0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
	0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
};

//Originally 0x9ABE04
uint8 text_palette[0x8] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// Previously 0x97FCBC use it to get the correct palette from g1_elements
const uint16 palette_to_g1_offset[PALETTE_TO_G1_OFFSET_COUNT] = {
	0x1333, 0x1334, 0x1335, 0x1336,
	0x1337, 0x1338, 0x1339, 0x133A,
	0x133B, 0x133C, 0x133D, 0x133E,
	0x133F, 0x1340, 0x1341, 0x1342,
	0x1343, 0x1344, 0x1345, 0x1346,
	0x1347, 0x1348, 0x1349, 0x134A,
	0x134B, 0x134C, 0x134D, 0x134E,
	0x134F, 0x1350, 0x1351, 0x1352,
	0x1353, 0x0C1C, 0x0C1D, 0x0C1E,
	0x0C1F, 0x0C20, 0x0C22, 0x0C23,
	0x0C24, 0x0C25, 0x0C26, 0x0C21,
	0x1354, 0x1355, 0x1356, 0x1357,
	0x1358, 0x1359, 0x135A, 0x135B,
	0x135C, 0x135D, 0x135E, 0x135F,
	0x1360, 0x1361, 0x1362, 0x1363,
	0x1364, 0x1365, 0x1366, 0x1367,
	0x1368, 0x1369, 0x136A, 0x136B,
	0x136C, 0x136D, 0x136E, 0x136F,
	0x1370, 0x1371, 0x1372, 0x1373,
	0x1374, 0x1375, 0x1376, 0x1377,
	0x1378, 0x1379, 0x137A, 0x137B,
	0x137C, 0x137D, 0x137E, 0x137F,
	0x1380, 0x1381, 0x1382, 0x1383,
	0x1384, 0x1385, 0x1386, 0x1387,
	0x1388, 0x1389, 0x138A, 0x138B,
	0x138C, 0x138D, 0x138E, 0x138F,
	0x1390, 0x1391, 0x1392, 0x1393,
	0x1394, 0x1395, 0x1396, 0x1397,
	0x1398, 0x1399, 0x139A, 0x139B,
	0x139C, 0x139D, 0x139E, 0x139F,
	0x13A0, 0x13A1, 0x13A2, 0x13A3,
	0x13A4, 0x13A5, 0x13A6, 0x13A7,
	0x13A8, 0x13A9, 0x13AA, 0x13AB,
	0x13AC, 0x13AD, 0x13AE, 0x13AF,
	0x13B0, 0x13B1, 0x13B2, 0x13B3,
	0x13B4, 0x13B5, 0x13B6, 0x13B7,
};

static void gfx_draw_dirty_blocks(int x, int y, int columns, int rows);

/**
 * Clears the screen with the specified colour.
 *  rct2: 0x00678A9F
 */
void gfx_clear(rct_drawpixelinfo *dpi, int colour)
{
	int w = dpi->width >> dpi->zoom_level;
	int h = dpi->height >> dpi->zoom_level;
	uint8* ptr = dpi->bits;

	for (int y = 0; y < h; y++) {
		memset(ptr, colour, w);
		ptr += w + dpi->pitch;
	}
}

void gfx_draw_pixel(rct_drawpixelinfo *dpi, int x, int y, int colour)
{
	gfx_fill_rect(dpi, x, y, x, y, colour);
}

/**
 *
 *  rct2: 0x00683854
 * a1 (ebx)
 * product (cl)
 */
void gfx_transpose_palette(int pal, unsigned char product)
{
	rct_g1_element g1 = g1Elements[pal];
	int width = g1.width;
	int x = g1.x_offset;
	uint8* dest_pointer = &gGamePalette[x * 4];
	uint8* source_pointer = g1.offset;

	for (; width > 0; width--) {
		dest_pointer[0] = (source_pointer[0] * product) >> 8;
		dest_pointer[1] = (source_pointer[1] * product) >> 8;
		dest_pointer[2] = (source_pointer[2] * product) >> 8;
		source_pointer += 3;
		dest_pointer += 4;
	}
	platform_update_palette(gGamePalette, 10, 236);
}

/**
 *
 *  rct2: 0x006837E3 
 */
void load_palette(){
	rct_water_type* water_type = (rct_water_type*)object_entry_groups[OBJECT_TYPE_WATER].chunks[0];

	uint32 palette = 0x5FC;

	if ((sint32)water_type != -1){
		palette = water_type->image_id;
	}

	rct_g1_element g1 = g1Elements[palette];
	int width = g1.width;
	int x = g1.x_offset;
	uint8* dest_pointer = &gGamePalette[x * 4];
	uint8* source_pointer = g1.offset;

	for (; width > 0; width--) {
		dest_pointer[0] = source_pointer[0];
		dest_pointer[1] = source_pointer[1];
		dest_pointer[2] = source_pointer[2];
		source_pointer += 3;
		dest_pointer += 4;
	}
	platform_update_palette(gGamePalette, 10, 236);
}

/**
 *
 *  rct2: 0x006ED7E5
 */
void gfx_invalidate_screen()
{
	gfx_set_dirty_blocks(0, 0, gScreenWidth, gScreenHeight);
}

uint8* gfx_get_dirty_blocks()
{
	int size = _screenDirtyBlockColumns * _screenDirtyBlockRows;
	if (_screenDirtyBlocksSize != size) {
		if (_screenDirtyBlocks) {
			_screenDirtyBlocks = realloc(_screenDirtyBlocks, size);
		} else {
			_screenDirtyBlocks = malloc(size);
		}
		_screenDirtyBlocksSize = size;
	}
	return _screenDirtyBlocks;
}

/**
 *
 *  rct2: 0x006E732D
 * left (ax)
 * top (bx)
 * right (dx)
 * bottom (bp)
 */
void gfx_set_dirty_blocks(sint16 left, sint16 top, sint16 right, sint16 bottom)
{
	int x, y;
	uint8 *screenDirtyBlocks = gfx_get_dirty_blocks();

	left = max(left, 0);
	top = max(top, 0);
	right = min(right, gScreenWidth);
	bottom = min(bottom, gScreenHeight);

	if (left >= right)
		return;
	if (top >= bottom)
		return;

	right--;
	bottom--;

	left >>= _screenDirtyBlockShiftX;
	right >>= _screenDirtyBlockShiftX;
	top >>= _screenDirtyBlockShiftY;
	bottom >>= _screenDirtyBlockShiftY;

	uint32 dirtyBlockColumns = _screenDirtyBlockColumns;
	for (y = top; y <= bottom; y++) {
		uint32 yOffset = y * dirtyBlockColumns;
		for (x = left; x <= right; x++) {
			screenDirtyBlocks[yOffset + x] = 0xFF;
		}
	}
}

/**
 *
 *  rct2: 0x006E73BE
 */
void gfx_draw_all_dirty_blocks()
{
	uint32 x, y, xx, yy, columns, rows;
	uint32 dirtyBlockColumns = _screenDirtyBlockColumns;
	uint32 dirtyBlockRows = _screenDirtyBlockRows;
	uint8 *screenDirtyBlocks = gfx_get_dirty_blocks();

	for (x = 0; x < dirtyBlockColumns; x++) {
		for (y = 0; y < dirtyBlockRows; y++) {
			uint32 yOffset = y * dirtyBlockColumns;
			if (screenDirtyBlocks[yOffset + x] == 0) {
				continue;
			}

			// Determine columns
			for (xx = x; xx < dirtyBlockColumns; xx++) {
				if (screenDirtyBlocks[yOffset + xx] == 0) {
					break;
				}
			}
			columns = xx - x;

			// Check rows
			for (yy = y; yy < dirtyBlockRows; yy++) {
				uint32 yyOffset = yy * dirtyBlockColumns;
				for (xx = x; xx < x + columns; xx++) {
					if (screenDirtyBlocks[yyOffset + xx] == 0) {
						goto endRowCheck;
					}
				}
			}

		endRowCheck:
			rows = yy - y;
			gfx_draw_dirty_blocks(x, y, columns, rows);
		}
	}
}

static void gfx_draw_dirty_blocks(int x, int y, int columns, int rows)
{
	uint32 left, top, right, bottom;
	uint32 dirtyBlockColumns = _screenDirtyBlockColumns;
	uint8 *screenDirtyBlocks = gfx_get_dirty_blocks();

	// Unset dirty blocks
	for (top = y; top < y + (uint32)rows; top++) {
		uint32 topOffset = top * dirtyBlockColumns;
		for (left = x; left < x + (uint32)columns; left++) {
			screenDirtyBlocks[topOffset + left] = 0;
		}
	}

	// Determine region in pixels
	left = max(0, x * _screenDirtyBlockWidth);
	top = max(0, y * _screenDirtyBlockHeight);
	right = min((uint32)gScreenWidth, left + (columns * _screenDirtyBlockWidth));
	bottom = min((uint32)gScreenHeight, top + (rows * _screenDirtyBlockHeight));
	if (right <= left || bottom <= top) {
		return;
	}

	// Draw region
	gfx_redraw_screen_rect(left, top, right, bottom);
}

/**
 *
 *  rct2: 0x006E7499
 * left (ax)
 * top (bx)
 * right (dx)
 * bottom (bp)
 */
void gfx_redraw_screen_rect(short left, short top, short right, short bottom)
{
	rct_drawpixelinfo *screenDPI = &gScreenDPI;
	rct_drawpixelinfo *windowDPI = &gWindowDPI;

	windowDPI->bits = screenDPI->bits + left + ((screenDPI->width + screenDPI->pitch) * top);
	windowDPI->x = left;
	windowDPI->y = top;
	windowDPI->width = right - left;
	windowDPI->height = bottom - top;
	windowDPI->pitch = screenDPI->width + screenDPI->pitch + left - right;

	for (rct_window *w = g_window_list; w < gWindowNextSlot; w++) {
		if (w->flags & WF_TRANSPARENT)
			continue;
		if (right <= w->x || bottom <= w->y)
			continue;
		if (left >= w->x + w->width || top >= w->y + w->height)
			continue;
		window_draw(w, left, top, right, bottom);
	}
}

/*
*
* rct2: 0x006EE53B
* left (ax)
* width (bx)
* top (cx)
* height (dx)
* drawpixelinfo (edi)
*/
bool clip_drawpixelinfo(rct_drawpixelinfo *dst, rct_drawpixelinfo *src, int x, int y, int width, int height)
{
	int right = x + width;
	int bottom = y + height;

	dst->bits = src->bits;
	dst->x = src->x;
	dst->y = src->y;
	dst->width = src->width;
	dst->height = src->height;
	dst->pitch = src->pitch;
	dst->zoom_level = 0;

	if (x > dst->x) {
		uint16 clippedFromLeft = x - dst->x;
		dst->width -= clippedFromLeft;
		dst->x = x;
		dst->pitch += clippedFromLeft;
		dst->bits += clippedFromLeft;
	}

	int stickOutWidth = dst->x + dst->width - right;
	if (stickOutWidth > 0) {
		dst->width -= stickOutWidth;
		dst->pitch += stickOutWidth;
	}

	if (y > dst->y) {
		uint16 clippedFromTop = y - dst->y;
		dst->height -= clippedFromTop;
		dst->y = y;
		uint32 bitsPlus = (dst->pitch + dst->width) * clippedFromTop;
		dst->bits += bitsPlus;
	}

	int bp = dst->y + dst->height - bottom;
	if (bp > 0) {
		dst->height -= bp;
	}

	if (dst->width > 0 && dst->height > 0) {
		dst->x -= x;
		dst->y -= y;
		return true;
	}

	return false;
}

/**
 *
 *  rct2: 0x00684027
 * ebp used to be a parameter but it is always zero
 * left   : eax
 * top    : ebx
 * width  : ecx
 * height : edx
 * x_start: edi
 * y_start: esi
 */
void gfx_draw_rain(int left, int top, int width, int height, sint32 x_start, sint32 y_start)
{
	static const uint8 RainPattern[] = {
		32, 32, 0, 12, 0, 14, 0, 16, -1, 0, -1, 0, -1, 0, -1, 0, -1,
		0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0,
		-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
		0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, 0
	};

	const uint8 *pattern = RainPattern;
	uint8 pattern_x_space = *pattern++;
	uint8 pattern_y_space = *pattern++;

	uint8 pattern_start_x_offset = x_start % pattern_x_space;
	uint8 pattern_start_y_offset = y_start % pattern_y_space;

	rct_drawpixelinfo *dpi = &gScreenDPI;
	uint32 pixel_offset = (dpi->pitch + dpi->width) * top + left;
	uint8 pattern_y_pos = pattern_start_y_offset % pattern_y_space;

	//Stores the colours of changed pixels
	uint32 *pixel_store = _rainPixels;
	pixel_store += _numRainPixels;

	for (; height != 0; height--) {
		uint8 pattern_x = pattern[pattern_y_pos * 2];
		if (pattern_x != 0xFF) {
			if (_numRainPixels < (MAX_RAIN_PIXELS - (uint32)width)) {
				int final_pixel_offset = width + pixel_offset;

				int x_pixel_offset = pixel_offset;
				x_pixel_offset += ((uint8)(pattern_x - pattern_start_x_offset)) % pattern_x_space;

				uint8 pattern_pixel = pattern[pattern_y_pos * 2 + 1];
				for (; x_pixel_offset < final_pixel_offset; x_pixel_offset += pattern_x_space){
					uint8 current_pixel = dpi->bits[x_pixel_offset];
					dpi->bits[x_pixel_offset] = pattern_pixel;
					_numRainPixels++;

					// Store colour and position
					*pixel_store++ = (x_pixel_offset << 8) | current_pixel;
				}
			}
		}

		pixel_offset += dpi->pitch + dpi->width;
		pattern_y_pos++;
		pattern_y_pos %= pattern_y_space;
	}
}

/**
 *
 *  rct2: 0x006843DC
 */
void redraw_rain()
{
	if (_numRainPixels > 0) {
		rct_window *window = window_get_main();
		uint32 numPixels = window->width * window->height;

		uint8 *screenPixels = gScreenDPI.bits;
		for (uint32 i = 0; i < _numRainPixels; i++) {
			uint32 pixel = _rainPixels[i];
			uint32 pixelIndex = pixel >> 8;

			// HACK
			if (pixelIndex > numPixels) {
				log_verbose("Pixel error, skipping rain draw in this frame");
				break;
			}
			screenPixels[pixelIndex] = pixel & 0xFF;
		}
		_numRainPixels = 0;
	}
}

void gfx_invalidate_pickedup_peep()
{
	uint32 sprite = gPickupPeepImage;
	if (sprite != UINT32_MAX) {
		sprite = sprite & 0x7FFFF;

		rct_g1_element *g1_elements = &g1Elements[sprite];
		sint32 left = gPickupPeepX + g1_elements->x_offset;
		sint32 top = gPickupPeepY + g1_elements->y_offset;
		sint32 right = left + g1_elements->width;
		sint32 bottom = top + g1_elements->height;

		gfx_set_dirty_blocks(left, top, right, bottom);
	}
}

void gfx_draw_pickedup_peep()
{
	if (gPickupPeepImage != UINT32_MAX) {
		gfx_draw_sprite(&gScreenDPI, gPickupPeepImage, gPickupPeepX, gPickupPeepY, 0);
	}
}

/**
 * Draws the given colour image masked out by the given mask image. This can currently only cope with bitmap formatted mask and
 * colour images. Presumebly the original game never used RLE images for masking. Colour 0 represents transparent.
 *
 *  rct2: 0x00681DE2
 */
void FASTCALL gfx_draw_sprite_raw_masked(rct_drawpixelinfo *dpi, int x, int y, int maskImage, int colourImage)
{
	int left, top, right, bottom, width, height;
	rct_g1_element *imgMask = &g1Elements[maskImage & 0x7FFFF];
	rct_g1_element *imgColour = &g1Elements[colourImage & 0x7FFFF];

	assert(imgMask->flags & 1);
	assert(imgColour->flags & 1);

	if (dpi->zoom_level != 0) {
		// TODO implement other zoom levels (probably not used though)
		assert(false);
		return;
	}

	width = min(imgMask->width, imgColour->width);
	height = min(imgMask->height, imgColour->height);

	x += imgMask->x_offset;
	y += imgMask->y_offset;

	left = max(dpi->x, x);
	top = max(dpi->y, y);
	right = min(dpi->x + dpi->width, x + width);
	bottom = min(dpi->y + dpi->height, y + height);

	width = right - left;
	height = bottom - top;
	if (width < 0 || height < 0)
		return;

	int skipX = left - x;
	int skipY = top - y;

	uint8 *maskSrc = imgMask->offset + (skipY * imgMask->width) + skipX;
	uint8 *colourSrc = imgColour->offset + (skipY * imgColour->width) + skipX;
	uint8 *dst = dpi->bits + (left - dpi->x) + ((top - dpi->y) * (dpi->width + dpi->pitch));

	int maskWrap = imgMask->width - width;
	int colourWrap = imgColour->width - width;
	int dstWrap = ((dpi->width + dpi->pitch) - width);
	for (int y = top; y < bottom; y++) {
		for (int x = left; x < right; x++) {
			uint8 colour = (*colourSrc) & (*maskSrc);
			if (colour != 0) {
				*dst = colour;
			}

			maskSrc++;
			colourSrc++;
			dst++;
		}
		maskSrc += maskWrap;
		colourSrc += colourWrap;
		dst += dstWrap;
	}
}

void gfx_configure_dirty_grid()
{
	_screenDirtyBlockShiftX = 7;
	_screenDirtyBlockShiftY = 6;
	_screenDirtyBlockWidth = 1 << _screenDirtyBlockShiftX;
	_screenDirtyBlockHeight = 1 << _screenDirtyBlockShiftY;
	_screenDirtyBlockColumns = (gScreenWidth >> _screenDirtyBlockShiftX) + 1;
	_screenDirtyBlockRows = (gScreenHeight >> _screenDirtyBlockShiftY) + 1;
}
