package com.ld58.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import Global.Global;
import entities.PausePerson;
import entities.Player;

public class WinScreen implements Screen {
	final rcr game;
	private OrthographicCamera camera;
	private Viewport vport;
	// add pause person. This is added to every screen in the game. ty.
	private PausePerson pp;
	private Sound enter;
	private Sound enter2;
	private Music chillMusic;
	// end pause variables
	private float cameraInitX;
	private float cameraInitY;
	private boolean isCameraPositionInitialized;
	private float titleTimerOne = 100;
	private float titleTimerTwo = MathUtils.random(50f, 70f);
	private boolean isTitleLettersDown;
	private boolean _isTwo;
	private boolean _isThree;
	private int cameraShakeTimer;
	private Sound thump;
	private Sound sfx_titlescreen;
	private float menuItemTimer0 = MathUtils.random(40f, 60f);
	private boolean isMenuDone;
	private float timer = .7f;
	private float timer2 = .8f;
	private boolean up;
	private boolean up2;
	private float _posY = 1.502f;
	private float _posYS = .001f;
	private float _rateOfDrop = .001f;
	private int _isDelayTimer = MathUtils.random(40, 60);;
	private boolean _isDelay;
	private float nameZoomX;
	private float nameZoomY;
	private boolean isNameZoomRight;
	private boolean isNameZoomUp;

	// font title? yeah! (later)
	public WinScreen(final rcr game) {
		this.game = game;
		camera = new OrthographicCamera();
		vport = new FitViewport(rcr.MW, rcr.MH, camera);
		sfx_titlescreen = Gdx.audio.newSound(Gdx.files.internal("sfx/select2.ogg"));
		// menustuff:
		pp = new PausePerson(Gdx.graphics.getWidth() / 2 - 210, Gdx.graphics.getHeight() - 235f);
		enter = Gdx.audio.newSound(Gdx.files.internal("sfx/click.ogg"));
		enter2 = Gdx.audio.newSound(Gdx.files.internal("sfx/click2.ogg"));
		// music
		chillMusic = Gdx.audio.newMusic(Gdx.files.internal("sfx/rcrMusic.ogg"));
		chillMusic.setLooping(true);
		thump = Gdx.audio.newSound(Gdx.files.internal("sfx/select.ogg"));
	}

	@Override
	public void show() {
		if (!Global.SetVolumeToZero)
			sfx_titlescreen.play();
		Gdx.input.setInputProcessor(new InputAdapter() {
			@Override
			public boolean keyDown(int keyCode) {
				// add this first part for the pause portion of the screen. The escape button.
				// Otherwise retain the space control.
				// Maybe we'll have a clickable button on the bottom side for options?
				if (keyCode == Input.Keys.ESCAPE && !Global.IsPaused) {
					if (!Global.SetVolumeToZero)
						enter.play(2f);
					Global.IsPaused = true;
				} else if (keyCode == Input.Keys.ESCAPE && Global.IsPaused && Global.IsKeyBindings && Global.IsSetKey) {
					if (!Global.SetVolumeToZero)
						enter2.play(2f);
					Global.IsSetKey = false;
				} else if (keyCode == Input.Keys.ESCAPE && Global.IsPaused && Global.IsKeyBindings) {
					if (!Global.SetVolumeToZero)
						enter2.play(2f);
					Global.IsKeyBindings = false;
					pp.IsSwitchingToKeyBindings = true;
				} else if (keyCode == Input.Keys.ESCAPE && Global.IsPaused) {
					if (!Global.SetVolumeToZero)
						enter2.play(2f);
					Global.IsPaused = false;
				} else if ((keyCode == Input.Keys.ENTER || Gdx.input.isTouched()) && !Global.IsPaused) {
					dispose();
					ScreenUtils.clear(0, 0, 0, 1);
					Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
					Global.resetAllStats();
					game.setScreen(new TitleScreen(game));
				}
				if (Global.IsSetKey && Global.IsPaused)
					Global.KeycodeSet = keyCode;
				return true;
			}

			@Override
			public boolean touchDown(int x, int y, int pointer, int button) {// this is for capturing mouse clicks
				if (Global.IsSetKey)
					Global.KeycodeSet = button;
				return true;
			}

		});
		// start the play back of the background music
		// when the screen is shown
		chillMusic.setVolume(.7f);
		if (!Global.IsMusicPaused)
			chillMusic.play();
	}

	@Override
	public void render(float delta) {
		ScreenUtils.clear(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		game.batch.setProjectionMatrix(camera.combined);
		if (!isCameraPositionInitialized) {
			cameraInitX = camera.position.x;
			cameraInitY = camera.position.y;
			isCameraPositionInitialized = true;
		}
		game.batch.begin();
		// add pause person. This is added to every screen in the game you want a pause
		// menu.
		if (Global.IsPaused) {
			pp.RenderEntirePauseMenu(delta, game, game.font, chillMusic, camera);
//			DebugRenderer.DrawDebugRectangle(pp.saveGameRect, Color.RED, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.loadGameRect, Color.GREEN, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.muteSoundRect, Color.PURPLE, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.muteMusicRect, Color.BLUE, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.keyBindingsRect, Color.ORANGE, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.backRect, Color.BROWN, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.exitGameRect, Color.PINK, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.crouchRect, Color.WHITE, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.sprintRect, Color.GRAY, camera.combined);
//			DebugRenderer.DrawDebugRectangle(pp.keyBindingsBackRect, Color.YELLOW, camera.combined);
		} else {
			dropTitleLetters(camera);

			game.batch.end();
			// if timer is less than .65 you need to go up.
			if (timer <= .648f) {
				up = true;
			}
			if (up)// if timer is greater than .68 you need to go down.
				timer += MathUtils.random(.001f, .0005f);
			else
				timer -= MathUtils.random(.001f, .0005f);
			;
			if (timer >= .67f) {
				up = false;
			}
			// if timer is less than .65 you need to go up.
			if (timer2 <= .5f) {
				up2 = true;
			}
			if (up2)// if timer is greater than .68 you need to go down.
				timer2 += MathUtils.random(.0001f, .0005f);
			else
				timer2 -= MathUtils.random(.0001f, .0005f);
			;
			if (timer2 >= .501f) {
				up2 = false;
			}

			if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) {
				// System.out.println("inputing Q");
				System.out.println("\r\r\r\r\r\r\r");// i dont know how to clear the consel this way without clicking on the
														// button.:)
				dispose();// hee hee i figured out how to reset the level. xD (dispose of the level and
							// reload it. ;-) )
				game.setScreen(new WinScreen(game));
			}
		}
	}

	public void dropTitleLetters(OrthographicCamera camera) {
		if (!isTitleLettersDown) {
			float _targetWidth = 1950f;
			int _halign = 0;
			boolean _wrap = true;
			float _endPosX = 1f;
			float _endPosXS = .8f;
			float _endScale = 3f;
			float _endMenuScale = 1f;
			float _o = 1000f;
			game.font.getData().setScale(titleTimerOne);
			game.font.setColor(Color.ORANGE);
			game.font.draw(game.batch, "COUNZGCUATUALTIONS!", Gdx.graphics.getWidth() * .12f - 400, Gdx.graphics.getHeight() * _posY + 2 - 150, _targetWidth,
					_halign, _wrap);
			game.font.setColor(Color.YELLOW);
			game.font.draw(game.batch, "COUNZGCUATUALTIONS!", Gdx.graphics.getWidth() * .122f - 400, Gdx.graphics.getHeight() * _posY + 4 - 150, _targetWidth,
					_halign, _wrap);
			game.font.getData().setScale(3f);
			game.font.setColor(Color.GOLD);
			game.font.draw(game.batch, "VICTROY WOAS HARD!", Gdx.graphics.getWidth() * .122f - 600, Gdx.graphics.getHeight() * _posY + 4 - 381, _targetWidth,
					_halign, _wrap);
			game.font.setColor(Color.WHITE);
			game.font.draw(game.batch, "VICTROY WOAS HARD!", Gdx.graphics.getWidth() * .122f - 600, Gdx.graphics.getHeight() * _posY + 4 - 380, _targetWidth,
					_halign, _wrap);
			game.font.getData().setScale(1.0f);// this resets the rest of the font to normal (it's only used for the above
												// setting)
			titleTimerOne--;
			if (titleTimerOne <= _endScale) {
				titleTimerOne = _endScale;
				if (!_isTwo) {
					cameraShakeTimer = 15;
					if (!Global.SetVolumeToZero)
						thump.play();
				}
				_isTwo = true;
			}
			if (_posY <= _endPosX)
				_posY = _endPosX;
			if (_posY > _endPosX)
				_posY -= _rateOfDrop;
			if (_posYS >= _endPosXS)
				_posYS = _endPosXS;
			if (_posYS < _endPosXS)
				_posYS += _rateOfDrop;
			if (_rateOfDrop >= .01f)
				_rateOfDrop = .01f;
			else
				_rateOfDrop += .0001f;

			if (_isTwo) {
				if (titleTimerTwo > _endMenuScale)
					game.font.getData().setScale(titleTimerTwo);
				else
					game.font.getData().setScale(1.02f);
				game.font.setColor(Color.BLUE);
				game.font.draw(game.batch, "Score: " + Player.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .401f, _targetWidth, _halign,
						_wrap);
//				game.font.draw(game.batch, "Score: " + Player2.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .301f, _targetWidth, _halign,
//						_wrap);
//				game.font.draw(game.batch, "Score: " + Player3.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .201f, _targetWidth, _halign,
//						_wrap);
				game.font.setColor(Color.WHITE);
				game.font.draw(game.batch, "Score: " + Player.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .402f, _targetWidth, _halign,
						_wrap);
//				game.font.draw(game.batch, "Score: " + Player2.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .302f, _targetWidth, _halign,
//						_wrap);
//				game.font.draw(game.batch, "Score: " + Player3.score, Gdx.graphics.getWidth() * .201f - _o, Gdx.graphics.getHeight() * .202f, _targetWidth, _halign,
//						_wrap);
				game.font.draw(game.batch, "pixel postmortems are the best! :D", Gdx.graphics.getWidth() * .491f - _o, Gdx.graphics.getHeight() * .101f,
						_targetWidth, _halign, _wrap);
				game.font.setColor(Color.BLUE);
				game.font.draw(game.batch, "ppsstt, you should watch pixel prophecy", Gdx.graphics.getWidth() * .091f - _o, Gdx.graphics.getHeight() * .101f,
						_targetWidth, _halign, _wrap);
				game.font.setColor(Color.GRAY);
				game.font.draw(game.batch, "Press Enter To Advance", Gdx.graphics.getWidth() * .151f - _o, Gdx.graphics.getHeight() * timer2 / 2 * 1.201f,
						_targetWidth, _halign, _wrap);
				if (titleTimerTwo > _endMenuScale)
					game.font.getData().setScale(titleTimerTwo);
				else
					game.font.getData().setScale(1f);
				game.font.setColor(Color.WHITE);
				game.font.draw(game.batch, "Press Enter To Advance", Gdx.graphics.getWidth() * .15f - _o, Gdx.graphics.getHeight() * timer2 / 2 * 1.2f, _targetWidth,
						_halign, _wrap);
				game.font.getData().setScale(1.0f);// this resets the rest of the font to normal (it's only used for the above
													// setting)
				titleTimerTwo--;
				if (titleTimerTwo <= _endMenuScale) {
					titleTimerTwo = _endMenuScale;
					if (!_isThree) {
						cameraShakeTimer = 15;
						if (!Global.SetVolumeToZero)
							thump.play();
					}
					_isThree = true;
				}
			}
			if (_isThree && _isDelayTimer >= 0)
				_isDelayTimer--;
			if (_isThree && _isDelayTimer < 0)
				_isDelay = true;
			if (_isThree && _isDelay) {
				if (menuItemTimer0 > _endMenuScale)
					game.font.getData().setScale(menuItemTimer0);
				else
					game.font.getData().setScale(1.02f);
				game.font.setColor(Color.WHITE);
				game.font.draw(game.batch, "PRESS ENTER, not sure where it will go", Gdx.graphics.getWidth() * timer2 / 2 * .266f,
						Gdx.graphics.getHeight() * timer2 / 2 * .79f);
				if (menuItemTimer0 > _endMenuScale)
					game.font.getData().setScale(menuItemTimer0);
				else
					game.font.getData().setScale(1f);
				game.font.setColor(Color.ORANGE);
				game.font.draw(game.batch, "PRESS ENTER, not sure where it will go", Gdx.graphics.getWidth() * timer / 2 * .217f,
						Gdx.graphics.getHeight() * timer / 2 * .60f);
				game.font.getData().setScale(1.0f);// this resets the rest of the font to normal (it's only used for the above
													// setting)
				menuItemTimer0--;
				if (menuItemTimer0 <= _endMenuScale) {
					menuItemTimer0 = _endMenuScale;
					if (!isMenuDone) {
						cameraShakeTimer = 15;
						if (!Global.SetVolumeToZero)
							thump.play();
					}
					isMenuDone = true;
				}
				game.font.getData().setScale(1f);
				for (int i = 0; i < 2; i++) {
					game.font.setColor(1f, 1f, setColorNumber(i), 1);
					if (isNameZoomRight && isNameZoomUp)
						game.font.draw(game.batch, "You survived the rush. Good job!", (float) (nameZoomX - 10 * i), (float) (nameZoomY - 10 * i));
					else if (isNameZoomRight && !isNameZoomUp)
						game.font.draw(game.batch, "You survived the rush. Good job!", (float) (nameZoomX - 10 * i), (float) (nameZoomY + 10 * i));
					else if (!isNameZoomRight && isNameZoomUp)
						game.font.draw(game.batch, "You survived the rush. Good job!", (float) (nameZoomX + 10 * i), (float) (nameZoomY - 10 * i));
					else
						game.font.draw(game.batch, "You survived the rush. Good job!", (float) (nameZoomX + 10 * i), (float) (nameZoomY + 10 * i));
				}
				game.font.setColor(Color.CYAN);
				game.font.draw(game.batch, "You survived the rush. Good job!", nameZoomX, nameZoomY);
				game.font.getData().setScale(1.0f);// this resets the rest of the font to normal (it's only used for the above

				// move the name left and right. then up and down. ruld//
				if (nameZoomX <= 16)
					isNameZoomRight = true;
				if (nameZoomX >= rcr.MW - 246)
					isNameZoomRight = false;
				if (isNameZoomRight)
					nameZoomX += MathUtils.random(2f, 4f);
				else
					nameZoomX -= MathUtils.random(2f, 4f);

				if (nameZoomY <= -16) {
					nameZoomY = rcr.MH;
				}
				nameZoomY = MathUtils.random(62f, 64f);
			}
			if (cameraShakeTimer > 0) {
				u.UsefulMethods.MoveCameraAroundRandomly(camera, 1, 1);
				camera.update();
				cameraShakeTimer -= 1;
			} else {
				camera.position.x = cameraInitX;
				camera.position.y = cameraInitY;
			}
		}

	}

	private float setColorNumber(int i) {
		if (i == 0)
			return 1f;
		if (i == 1)
			return .9f;
		if (i == 2)
			return .8f;
		if (i == 3)
			return .7f;
		if (i == 4)
			return .6f;
		if (i == 5)
			return .5f;
		if (i == 6)
			return .4f;
		if (i == 7)
			return .3f;
		if (i == 8)
			return .2f;
		if (i == 9)
			return .1f;
		if (i == 10)
			return .1f;
		else
			return 1f;
	}

	@Override
	public void hide() {
		Gdx.input.setInputProcessor(null);
	}

	@Override
	public void resize(int width, int height) {
		vport.update(width, height, true);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		sfx_titlescreen.dispose();
		enter.dispose();
		enter2.dispose();
		thump.dispose();
		chillMusic.dispose();
	}

}
