#pragma once
#include "shared.h"
#include <enginend/engine.h>
#include "cheats.h"
enum CURSORMODES {
	MAINMENU=0,
	COMPUTER=1,
	LEFT=2,RIGHT=3,UP=4,DOWN=5,
	FLASHLIGHT=6,INTERACT=7
};
#define LOADMAP LoadTexture(newpath.c_str());
struct entity {
	char pos;
	char dir;

};



extern std::vector<std::vector<char>> map;
extern std::vector<char> opposites;
char moveentity(char pos,char dir,bool backwards);
char rotateentity(char pos,char dir, bool clockwise);
bool ifforward();bool ifbackward();bool ifleft();bool ifright();
bool ifinteract();bool ifflashlight();
bool cansee(char dest,char pos, char dir);
std::vector<entity> pathfind(entity seeker, entity target);
extern std::vector<char> sides;
extern std::vector<std::string> lightlevel;




struct game:public enginend::scene {
	// dir 1
	
	int lightmode:2=1; // off, on, urcooked
	std::string mappath;
	// this is for quick loading but without weighting ram..
	// yea the assets are small but i like optimization
	float stamina=0.0;
	entity uycatroz{6,3};
	entity skaile{10,3};
	entity player{0,1};
	Texture2D current;
	Texture2D skailemenu[3];
	bool iskailepressed=false;
	bool iskailemenu=false;
	
char skmode,uymode;

	bool canseeuycatroz=false;
	bool canseeskaile=false;
	float crystallyprogress=1.;
	void exit() override {
		UnloadTexture(current);
		for (Texture2D i: skailemenu) {
			
			UnloadTexture(i);
		}
		scene::exit();
	}
	void boot() final {
		mappath=AT("res/map");
		std::string newpath=AT((mappath+"/"+lightlevel[lightmode]+"/"+sides[player.dir]+"/"+std::to_string(player.pos)+".jpg"));
		skailemenu[0]=LoadTexture(AT("res/gui/buttonf.png"));
		skailemenu[1]=LoadTexture(AT("res/gui/buttonn.png"));
		skailemenu[2]=LoadTexture(AT("res/gui/skailevisor.png"));
		current=LOADMAP
	}
	void tick() final {
		if (iskailemenu) {
			if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
				iskailepressed=true;
			}else {
				iskailepressed=false;
			}
		}
		
	}
	void draw() final {
		bool changed=false;
		bool updateview=false;
		if (stamina>=1.){
			if (ifforward()){player.pos=moveentity(player.pos,player.dir,false);changed=true;}
			if (ifbackward()){player.pos=moveentity(player.pos,player.dir,true);changed=true;}
			if (ifleft()){player.dir=rotateentity(player.pos,player.dir,false);changed=true;}
			if (ifright()){player.dir=rotateentity(player.pos,player.dir,true);changed=true;}
			if (ifflashlight()){lightmode=!lightmode;changed=true;}
			if (ifinteract()) {
				changed=true;
				if (player.dir==0&&player.pos==0)changeto=0;
				else if (moveentity(player.pos,player.dir,false)==skaile.pos) {
					iskailemenu=!iskailemenu;
				}
			}
			if (changed) {
				UnloadTexture(current);
				std::string newpath=AT((mappath+"/"+lightlevel[lightmode]+"/"+sides[player.dir]+"/"+std::to_string(player.pos)+".jpg"));
				current=LOADMAP
				//tiny::success((char*)newpath.c_str());
				updateview=true;
			}
			if (updateview) {
				canseeuycatroz=cansee(uycatroz.pos,player.pos,player.dir);
				canseeskaile=cansee(skaile.pos,player.pos,player.dir);
			}
			if (ifinteract()) {
				if (player.pos==0&&player.dir==0)changeto=0;
				
			}
		}else stamina+=.01;
		float screenw=320;float screenh=240;
		
		if(!iskailemenu) {
			DrawTexturePro(current,{0,0,480,280},{0,0,screenw,screenh},{0,0},0,rl::WHITE);
			if (canseeuycatroz)DrawRectangle(
				screenw/10.,screenh/10.,screenw/12,screenh/1.5,rl::BLUE);
			if (canseeskaile)DrawRectangle(
				screenw/2.,screenh/10.,screenw/12,screenh/1.5,rl::RED);
		}else {
			if (iskailemenu) {
				DrawTexture(skailemenu[2],0,0,rl::WHITE);
				tiny::success("%i",iskailepressed);
				DrawTexture(skailemenu[iskailepressed],261,120,rl::WHITE);
				
			}
		}
		DrawText(("pos: "+std::to_string(player.pos)+" dir:"+std::to_string(player.dir)).c_str(),0,0,16,rl::WHITE);
		DrawText(("uypos: "+std::to_string(uycatroz.pos)+" uydir:"+std::to_string(uycatroz.dir)).c_str(),0,32,16,rl::SKYBLUE);
		DrawText(("skpos: "+std::to_string(skaile.pos)+" skdir:"+std::to_string(skaile.dir)).c_str(),0,48,16,rl::RED);
		DrawText(("crystally: "+std::to_string(crystallyprogress)).c_str(),0,64,16,rl::LIME);
		
		
		
	}
};
