#pragma once

#include "Config.h"
#include "Common.h"
#include "Entity.h"
#include "UI.h"
#include "TinyXML/tinyxml.h"

class Game : private sf::NonCopyable
{
	private:
		bool running;
		sf::RenderWindow window;
		sf::View gameView;

		sf::Texture logoTex;
		sf::Texture fontTex;
		sf::Texture cursorTex;
		sf::Texture tilesTex;
		sf::Texture playerTex;

		sf::Texture windowsTex, fillTex, messageTex;
		sf::Texture btnCloseTex, btnPlayTex, btnStopTex, btnClearTex, btnNextTex;
		sf::Texture btnTplUpTex, btnTplRightTex, btnTplDownTex, btnTplLeftTex;
		sf::Texture btnTpl0Tex, btnTpl1Tex, btnTpl2Tex, btnTpl3Tex, btnTpl4Tex, btnTpl5Tex, btnTpl6Tex, btnTpl7Tex, btnTpl8Tex, btnTpl9Tex;
		sf::Texture btnUpTex, btnDownTex, btnScrollTex, scrollTex;
		sf::Texture actionStartTex, actionUpTex, actionRightTex, actionDownTex, actionLeftTex;
		sf::Texture arrowTex, markerListTex, markerNumberTex;

		sf::Texture mainTex, howplayTex, btnChooseLevelTex,btnHowPlayTex, btnExitTex, btnBackTex, btnLevelTex;
		sf::Sprite mainSpr, howplaySpr;
		Button chooseLevelBtn, howPlayBtn, exitBtn, backBtn;
		Button level1Btn, level2Btn, level3Btn, level4Btn, level5Btn, level6Btn, level7Btn, level8Btn, level9Btn, level10Btn;

		sf::Sprite windowsSpr, fillSpr, messageSpr;
		sf::Sprite arrowSpr;
		sf::Sprite markerListSpr;
		sf::Sprite markerNumberSpr;
		sf::Sprite scrollSpr;

		sf::Sprite scrollTopSpr;
		sf::Sprite scrollCenterSpr;
		sf::Sprite scrollBottomSpr;

		sf::Texture closeTex;
		sf::Sprite closeSpr;

		Button closeBtn, playBtn, stopBtn, clearBtn, nextBtn;
		Button tplUpBtn, tplRightBtn, tplDownBtn, tplLeftBtn;
		Button tpl0Btn, tpl1Btn, tpl2Btn, tpl3Btn, tpl4Btn, tpl5Btn, tpl6Btn, tpl7Btn, tpl8Btn, tpl9Btn;
		Button upBtn, downBtn, scrollBtn;


		sf::Sprite logoSpr;
		sf::Sprite fontSpr;
		sf::Sprite cursorSpr;
		sf::Sprite blockSpr;
		sf::Sprite markerSpr;

		sf::Texture tileTex;
		sf::Sprite tileSpr, playBtnDisabledSpr;

		sf::Texture splashTex;
		sf::Sprite splashSpr;

		int iSleepSceneLogos;

		Player player;
		TCell stLevel[MAP_SIZE_X][MAP_SIZE_Y]; //карта
		Vector2i levelPosition;
		Vector2i respawnLevel;
		int iCountMarkers;
		bool bWin;
		int iWinY;

		bool openLevel[COUNT_LEVELS];

		std::vector<Box> listBoxes;
		std::vector<ActionBlock> listActions;

		int iCurrentLevel;
		int iCurrentIndexAction;
		int iCurrentCountAction;
		int iCurrentSleepAction;
		int iCountSteps;
		bool bRunProgramm;

		int programmX;
		int programmY;
		int indexArrow;
		int indexMarker;
		int iHeightProgramm;

		sf::Sprite cursorBlockSpr; // спрайт перетаскиваемого блока (действие и количество)
		bool getBlockAction; // взяли (под курсором) блок действия
		bool getBlockNumber; // взяли (под курсором) блок числа
		E_ACTION getAction;
		int getNumber;

		bool overActionsWindow; // навели в экран действий

		TMouse mouse;
		TKeyboard keyboard;
		E_SCENES eCurrentScene;

		int iSplashFrame;
		E_SPLASH_STATE eSplashState;
		
		void updateMouse();
		void updateKeyboard();

		void update();
		void render();

		void drawText(int iX, int iY, std::string sText);
		void drawNumber(int iX, int iY, int iNumber);

		void clearLevel();
		bool loadLevel(std::string sFilename);
		void nextLevel();
		void resetLevel();
		bool isBlocked(Vector2i cellPosition, bool testBoxes = false);
		bool hasBox(Vector2i cellPosition, int &indexBox);
		void move(E_DIRECTION eDirection);
		void runProgramm();
		void clearActions();
		void addAction(E_ACTION eAction, int iCount, int iIndex = 0);

		void clearCursorBlock();
		void setCursorBlockAction(E_ACTION eAction);
		void setCursorBlockNumber(int iNumber);

		void startSplash(bool bOut = false);
		void startScene(E_SCENES eScene);

		void loadAccessLevels();
		void saveAccessLevels();

		void updateSceneLogos();
		void updateSceneMenu();
		void updateSceneHowPlay();
		void updateSceneLevels();
		void updateSceneGame();

		void drawSceneLogos();
		void drawSceneMenu();
		void drawSceneHowPlay();
		void drawSceneLevels();
		void drawSceneGame();
	public:
		
		Game();
		~Game();

		void run();
};