#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~

Copyright 2018 - 2023 OneLoneCoder.com

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions or derivations of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions or derivative works in binary form must reproduce the above
copyright notice. This list of conditions and the following	disclaimer must be
reproduced in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS	"AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT	HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#pragma endregion
#pragma once
#include "olcPixelGameEngine.h"
#include "Animation.h"
#include "olcUTIL_Animate2D.h"
#include "Monster.h"
#include "olcPGEX_TransformedView.h"
#include "Player.h"
#include "olcUTIL_Camera2D.h"
#include "Bullet.h"
#include "Effect.h"
#include "Map.h"
#include "TMXParser.h"
#include "olcUTIL_DataFile.h"
#include "Key.h"
#include "GameState.h"

class Crawler : public olc::PixelGameEngine
{
	friend class GameState;
	friend class State_GameRun;
	friend class sig::Animation;
	std::unique_ptr<Player>player;
public:
	Pathfinding pathfinder;
	static InputGroup KEY_CONFIRM;
	static InputGroup KEY_ATTACK;
	static InputGroup KEY_LEFT;
	static InputGroup KEY_RIGHT;
	static InputGroup KEY_UP;
	static InputGroup KEY_DOWN;
	static float SIZE_CHANGE_SPEED;
	float levelTime;
	Camera2D camera;
	std::map<MapName,Map>MAP_DATA;
private:
	std::vector<std::unique_ptr<Effect>>foregroundEffects,backgroundEffects,foregroundEffectsToBeInserted,backgroundEffectsToBeInserted;
	std::vector<TileRenderData*>tilePreparationList,tileForegroundList;
	std::vector<int>dropsBeforeLower,dropsAfterLower,dropsBeforeUpper,dropsAfterUpper;
	std::vector<ZoneData>endZones,upperEndZones;
	std::vector<vf2d>circleCooldownPoints;
	std::vector<vf2d>squareCircleCooldownPoints;
	std::map<std::string,TilesetData>MAP_TILESETS;
	vf2d worldShake={};
	float worldShakeTime=0;
	float lastWorldShakeAdjust=0;
	vf2d worldShakeVel={};
	const float WORLD_SHAKE_ADJUST_MAX_TIME=0.4f;
	MapName currentLevel=MapName::CAMPAIGN_1_1;
	std::vector<TileGroup>foregroundTileGroups;
	std::vector<TileGroup>upperForegroundTileGroups;
	int bridgeLayerIndex=-1;
	float bridgeFadeFactor=0.f;
	void InitializeClasses();
	int DEBUG_PATHFINDING=0;
	std::vector<Monster*>monstersBeforeLower,monstersAfterLower,monstersBeforeUpper,monstersAfterUpper;
	std::vector<Bullet*>bulletsLower,bulletsUpper;
	std::vector<Effect*>backgroundEffectsLower,backgroundEffectsUpper,foregroundEffectsLower,foregroundEffectsUpper;
	float reflectionUpdateTimer=0;
	float reflectionStepTime=0;
	std::set<vi2d>visibleTiles;
	float bossDisplayTimer=0;
	std::string bossName;
	int totalDamageDealt=0;
	float encounterDuration=0;
	bool encounterStarted=false;
	int totalBossEncounterMobs=0;
	int chapter=1; //We start at chapter 1.
	std::array<Item,3>loadout;
	float fadeOutDuration=0;
	float fadeOutTotalTime=0;
	float fadeInDuration=0;
	States::State transitionState=States::State::GAME_RUN;

	std::vector<Monster>monstersToBeSpawned;

	void ValidateGameStatus();
public:
	Crawler();
	bool OnUserCreate() override;
	bool OnUserUpdate(float fElapsedTime) override;
	bool OnUserDestroy() override;
public:
	geom2d::rect<int>NO_COLLISION={};
	TileTransformedView view;
	void InitializeLevel(std::string mapFile,MapName map);
	void LoadLevel(MapName map);
	void HandleUserInput(float fElapsedTime);
	void UpdateCamera(float fElapsedTime);
	void UpdateEffects(float fElapsedTime);
	void UpdateBullets(float fElapsedTime);
	void RenderWorld(float fElapsedTime);
	void RenderHud();
	void RenderMenu();
	void AddEffect(std::unique_ptr<Effect>foreground,std::unique_ptr<Effect>background);
	//If back is true, places the effect in the background
	void AddEffect(std::unique_ptr<Effect>foreground,bool back=false);
	void HurtEnemies(vf2d pos,float radius,int damage,bool upperLevel,float z);
	vf2d GetWorldMousePos();
	bool LeftHeld();
	bool RightHeld();
	bool UpHeld();
	bool DownHeld();
	bool LeftReleased();
	bool RightReleased();
	bool UpReleased();
	bool DownReleased();
	Player*GetPlayer();
	void SetupWorldShake(float duration);
	//tileID is the tile number from the tilesets.
	bool IsForegroundTile(TilesheetData sheet,int tileID);
	//tileID is the tile number from the tilesets.
	bool IsUpperForegroundTile(int tileID);
	//tileID is the tile number from the tilesets.
	TilesheetData GetTileSheet(MapName map,int tileID);
	//Gets the rectangle of the tile collision at this tile. If upperLevel is set to true, the collision tile must be in a Bridge class layer for the tile to hit. Also, zones containing LowerBridgeCollision will apply when upperLevel is set to false.
	geom2d::rect<int>GetTileCollision(MapName map,vf2d pos,bool upperLevel=false);
	//Checks if the point resides inside of a collision tile.
	bool HasTileCollision(MapName map,vf2d pos,bool upperLevel=false);
	MapName GetCurrentLevel();
	bool IsBridgeLayer(LayerTag&layer);
	std::map<std::string,std::vector<ZoneData>>&GetZoneData(MapName map);
	void PopulateRenderLists();
	void ChangePlayerClass(Class cl);
	std::string GetString(std::string key);
	datafilestringdata GetStringList(std::string key);
	int GetInt(std::string key);
	datafileintdata GetIntList(std::string key);
	float GetFloat(std::string key);
	datafilefloatdata GetFloatList(std::string key);
	double GetDouble(std::string key);
	datafiledoubledata GetDoubleList(std::string key);
	static void OutputDebugInfo(const char*key,std::size_t len);
	void InitializeLevels();
	void RenderTile(vi2d pos,TilesheetData tileSheet,int tileSheetIndex,vi2d tileSheetPos);
	void RenderTile(TileRenderData&tileSheet,Pixel col);
	bool IsReflectiveTile(TilesheetData tileSheet,int tileID);
	void SpawnMonster(vf2d pos,MonsterData*data,bool upperLevel=false,bool isBossSpawn=false); //Queues a monster for spawning on the next frame.
	void DrawPie(vf2d center,float radius,float degreesCut,Pixel col);
	void DrawSquarePie(vf2d center,float radius,float degreesCut,Pixel col);
	void RenderCooldowns();
	void InitializeDefaultKeybinds();
	void SetBossNameDisplay(std::string name,float time=5);
	bool InBossEncounter();
	void StartBossEncounter();
	void DisplayBossEncounterInfo();
	void BossDamageDealt(int damage);
	void ReduceBossEncounterMobCount();
	void InitializeGraphics();
	void RenderVersionInfo();
	MapTag GetCurrentMap();
	int GetCurrentChapter();
	void SetChapter(int chapter);
	Item&GetLoadoutItem(int slot);
	void SetLoadoutItem(int slot,std::string itemName);
	//Returns true if the item can be used (we have >0 of it)
	bool UseLoadoutItem(int slot);
	//Blanks out this loadout item.
	void ClearLoadoutItem(int slot);
	void RenderFadeout();
	bool GamePaused();

	struct TileGroupData{
		vi2d tilePos;
		int layer;
		bool operator<(const TileGroupData&rhs)const{
			return layer<rhs.layer||(layer==rhs.layer&&tilePos<rhs.tilePos);
		}
	};
};