#pragma once
#include <cmath>
#include <enginend/engine.h>
#include "shared.h"


class mainmenu:public enginend::scene {
public:
	Texture2D bg;
	Texture2D title;
	Music bgm;
	Shader bzzzz;
	int wins:3=0b111;
	mainmenu() {
		
	}
	enginend::nodes::twod::labeledbutton* play;
	enginend::nodes::twod::labeledbutton* version;
	void boot() override {
		std::string modifier=".png";
		bg=LoadTexture(AT("res/menu/"+
			std::to_string(wins>>2&1)+std::to_string(wins>>1&1)+std::to_string(wins&1)
			+modifier));
		bgm=LoadMusicStream(AT("res/snd/song.mp3"));
		title=LoadTexture(AT("res/menu/title.png"));
		bgm.looping=true;
		PlayMusicStream(bgm);
		bzzzz=LoadShaderFromMemory(
		"attribute vec2 vertexPosition;attribute vec2 vertexTexCoord;attribute vec4 vertexColor;"
		"varying vec2 fragTexCoord;varying vec4 fragColor;varying float randf;"
		"uniform mat4 mvp;uniform float t;"
		"float rand(vec2 co){return fract(sin(dot(co,vec2(12.9898,78.233)))*43758.5453);}"
		"void main(){fragTexCoord=vertexTexCoord;fragColor=vertexColor;"
		"gl_Position=mvp*vec4(vertexPosition,0.,1.);"
		"randf=rand(vec2(gl_Position.x+t,gl_Position.y+t));}",
		"varying vec2 fragTexCoord;varying vec4 fragColor;varying float randf;"
		"uniform sampler2D texture0;"
		"void main(){"
		"vec4 texel=texture2D(texture0,fragTexCoord)*fragColor;"
		"float idx=(texel.r+texel.g+texel.b);"
		"if(idx<0.7||idx>0.9){idx*=randf;}"
		"gl_FragColor=vec4(texel.rgb,texel.a*idx);}"
		);
		enginend::DEFAULT->tint={255,255,255,0};
		play=new enginend::nodes::twod::labeledbutton("PLAY",
		nullptr,enginend::DEFAULT,0.65,0.75,0.5,0.05,std::function<void()>([]() {
			changeto=1;
			}),1
			);
		version=new enginend::nodes::twod::labeledbutton("0.1c",
		nullptr,enginend::DEFAULT,0.65,0.75,0.5,0.05,std::function<void()>([]() {
			changeto=2;
			}),1
		);
	}
	void tick() override {
		play->tick();
		version->tick();
	}
	void draw() override {
		float screenw=320;float screenh=240;
		UpdateMusicStream(bgm);
		DrawTexturePro(bg,{0,0,320,240},{0,0,screenw,screenh},{0,0},0,rl::WHITE);
		float t=GetTime()/200000;
		SetShaderValue(bzzzz,GetShaderLocation(bzzzz,"t"),&t,SHADER_UNIFORM_FLOAT);
		BeginShaderMode(bzzzz);
		DrawTexturePro(title,{0,0,1065,194},{0,0,screenw,(screenh/4)},{0,0},0,rl::WHITE);
		EndShaderMode();
		play->fs=std::sqrt(screenh+screenw);
		play->pos={0.65f*screenw,0.75f*screenh};
		play->size={0.3f*screenw,0.2f*screenh};
		unsigned char hovering=play->hover?play->pressed?128:64:0;
		enginend::DEFAULT->tint={255,255,255,hovering};
		enginend::DEFAULT->buttontext[0]=enginend::DEFAULT->buttontext[1]=enginend::DEFAULT->buttontext[2]=
		{255,0,255,255};
		play->draw();
		hovering=version->hover?version->pressed?128:64:0;
		enginend::DEFAULT->tint={255,255,255,hovering};
		
		version->fs=std::sqrt(screenh+screenw);
		version->pos={0.05f*screenw,0.75f*screenh};
		version->size={0.3f*screenw,0.2f*screenh};
		
		enginend::DEFAULT->buttontext[0]=enginend::DEFAULT->buttontext[1]=enginend::DEFAULT->buttontext[2]=
			{0,255,255,255};
		
		version->draw();
		
	}
	void exit() override {
		play->exit();
		version->exit();
		StopMusicStream(bgm);
		UnloadTexture(bg);
		UnloadTexture(title);
		UnloadMusicStream(bgm);
		UnloadShader(bzzzz);
	}
};