#include "scenes/game.h"
#include "scenes/mainmenu.h"
#include "cycletext.h"
tiny::ErrorLevel tiny::level{9};
PLATFORM platform=LINUX;
std::string androidpackage="kn.kinfuyuki.animatronical-q1a1";
Font gamefont;
Font loadingfont;
char changeto=0;
Color*LOADINGCOLOR[3] {
	new Color{255,255,0,255},
	new Color{255,0,255,255},
	new Color{0,255,255,255}
};
class anim: public enginend::program {
public:
	int loadscreensecs=0;
	int loadingscreen=0;
	RenderTexture2D buffer;
	enginend::nodes::twod::animated* caramel;
	logi* loadingtext;
	const char *CONF() override{return "SAVEFILE.TDF";}

	bool changing=false;
	Image aniicon;
	void boot() override {
		aniicon=LoadImage("res/icon.png");
		this->currentscene=new mainmenu;
		
		SetConfigFlags(FLAG_VSYNC_HINT|FLAG_WINDOW_RESIZABLE);
		InitWindow(380,240,"ani1");
		
		SetWindowIcon(aniicon);
		InitAudioDevice();
		gamefont=LoadFont(AT("res/font/mga.ttf"));
		caramel=new enginend::nodes::twod::animated(
			AT("res/loading/out2.gif"),{0,0},{320,240},6
		);
		loadingtext=new logi(
			nullptr,enginend::DEFAULT,0,220,200,16,16,"LOADING..."
		);
		buffer=LoadRenderTexture(320,240);
		enginend::DEFAULT->font=gamefont;
		currentscene->boot();
	}
	void tick() override {
		if (IsKeyDown(KEY_LEFT_ALT)&&IsKeyDown(KEY_F4))shouldclose=true;
		if(changing) {
			loadingscreen++;
			caramel->tick();
			loadingtext->tick();
			if (loadingscreen>loadscreensecs) {
				changing=false;
				loadingscreen=0;
				currentscene->exit();
				delete currentscene;
				currentscene= new game;
				currentscene->boot();
				currentscene->tick();
			}
		}
		else currentscene->tick();
	}
	void draw() override {
		BeginTextureMode(buffer);
		ClearBackground(rl::BLACK);
		int currscene=changeto;
		if (!changing)currentscene->draw();
		else {
			caramel->draw();
			loadingtext->draw();
		}
		EndTextureMode();
		
		if (currscene!=changeto) {
			tiny::success("yay");
			switch (changeto) {
				case 0:
					currentscene->exit();
					delete currentscene;
					currentscene= new mainmenu;
					currentscene->boot();
					break;
				case 1:
					changing=true;
					loadscreensecs=GetRandomValue(2*60,10*60);
					break;
				default:
					tiny::fatal("wtf");
			}
		}
		
		BeginDrawing();
		ClearBackground(rl::BLACK);
		DrawTexturePro(buffer.texture,{0,0,320,-240},{0,0,(float)GetScreenWidth(),(float)GetScreenHeight()},{0,0},0,rl::WHITE);
		EndDrawing();
	}
	void exit() override {
		currentscene->exit();
	}
};

int main() {
	anim dagame;
	dagame.boot();
	tiny::startup("animatronical 1","0.1c");
	tiny::fatal("/home/komi/src/cpp/allgames/animatronical/q1/a1/res/map/on/s/0.jpg");
	while (!dagame.shouldclose) {
		dagame.tick();
		dagame.draw();
		
	}
	dagame.exit();
}