package com.ld57.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;
import entities.Player2;
import entities.Player3;
import entities.Player4;
import entities.Player5;
import entities.Player6;
import levelElements.StoreIcon;

public class LoseScreen implements Screen {
	final ddd 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 = .5f;
	private float timer2 = .5f;
	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 String message;
	private float nameZoomX;
	private float nameZoomY;
	private boolean isNameZoomRight;
	private boolean isNameZoomUp;
	private StoreIcon storeIcon;

	// font title? yeah! (later)
	public LoseScreen(final ddd game, String _message) {
		this.game = game;
		camera = new OrthographicCamera();
		vport = new FitViewport(ddd.MW, ddd.MH, camera);
		storeIcon = new StoreIcon(300, 300);
		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/dddMusic.ogg"));
		chillMusic.setLooping(true);
		thump = Gdx.audio.newSound(Gdx.files.internal("sfx/select3.ogg"));
		message = _message;
	}

	@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)) {
					System.out.println("trying to leave!!");
					Global.resetPlayersStats();
					// LevelTmx.isPlayerXYGotten = false;
					dispose();
					ScreenUtils.clear(0, 0, 0, 1);
					Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
					System.out.println("we are ready to go to the titlescreen!!");
					ddd.GameSettings.putBoolean("isFairyEvent", true);
					ddd.GameSettings.flush();
					game.setScreen(new LoserStore(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.
		storeIcon.render(game, camera, delta);
		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 LoseScreen(game, "ok, why did you restart this?"));
			}
		}
	}

	public void dropTitleLetters(OrthographicCamera camera) {
		if (!isTitleLettersDown) {
			float _targetWidth = 750f;
			int _halign = 0;
			boolean _wrap = true;
			float _endPosX = .902f;
			float _endPosXS = .91f;
			float _endScale = 3f;
			float _endMenuScale = 1f;
			float _o = 0f;
			game.font.getData().setScale(titleTimerOne);
			game.font.setColor(Color.FIREBRICK);
			game.font.draw(game.batch, "What? " + message, Gdx.graphics.getWidth() * .207f, Gdx.graphics.getHeight() * _posY - _o, _targetWidth, _halign, _wrap);
			game.font.setColor(Color.RED);
			game.font.draw(game.batch, "What? " + message, Gdx.graphics.getWidth() * .21f, Gdx.graphics.getHeight() * _posY - _o, _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.RED);
				game.font.draw(game.batch, "P1 Score: " + Player.score, Gdx.graphics.getWidth() * .101f - _o, Gdx.graphics.getHeight() * .401f, _targetWidth,
						_halign, _wrap);
				game.font.draw(game.batch, "P2 Score: " + Player2.score + " (you wish! not implemented)", Gdx.graphics.getWidth() * .285f - _o,
						Gdx.graphics.getHeight() * .351f, _targetWidth, _halign, _wrap);
				game.font.draw(game.batch, "P3 Score: " + Player3.score + " (you wish! not implemented)", Gdx.graphics.getWidth() * .285f - _o,
						Gdx.graphics.getHeight() * .301f, _targetWidth, _halign, _wrap);
				game.font.draw(game.batch, "P4 Score: " + Player4.score + " (you wish! not implemented)", Gdx.graphics.getWidth() * .285f - _o,
						Gdx.graphics.getHeight() * .251f, _targetWidth, _halign, _wrap);
				game.font.draw(game.batch, "P5 Score: " + Player5.score + " (you wish! not implemented)", Gdx.graphics.getWidth() * .285f - _o,
						Gdx.graphics.getHeight() * .201f, _targetWidth, _halign, _wrap);
				game.font.draw(game.batch, "P6 Score: " + Player6.score + " (you wish! not implemented)", Gdx.graphics.getWidth() * .285f - _o,
						Gdx.graphics.getHeight() * .151f, _targetWidth, _halign, _wrap);
				game.font.draw(game.batch, "your scores... why?! :(", Gdx.graphics.getWidth() * .341f - _o, Gdx.graphics.getHeight() * .401f, _targetWidth, _halign,
						_wrap);
				game.font.draw(game.batch, "Just, try again", Gdx.graphics.getWidth() * .441f - _o, Gdx.graphics.getHeight() * .101f, _targetWidth, _halign, _wrap);
				game.font.setColor(Color.WHITE);
				game.font.draw(game.batch, "Press Enter To Start", Gdx.graphics.getWidth() * .01f - 100f, Gdx.graphics.getHeight() * timer2 / 2 * 1.401f,
						_targetWidth, _halign, _wrap);
				if (titleTimerTwo > _endMenuScale)
					game.font.getData().setScale(titleTimerTwo);
				else
					game.font.getData().setScale(1f);
				game.font.setColor(Color.ORANGE);
				game.font.draw(game.batch, "Press Enter To Start", Gdx.graphics.getWidth() * .01f - 101f, Gdx.graphics.getHeight() * timer2 / 2 * 1.4f, _targetWidth,
						_halign, _wrap);
				game.font.getData().setScale(1.0f);
				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.DARK_GRAY);
				game.font.draw(game.batch, "PRESS ENTER?!,      ...      please.", Gdx.graphics.getWidth() * timer2 / 4 * .366f,
						Gdx.graphics.getHeight() * timer2 / 2 * .79f);
				if (menuItemTimer0 > _endMenuScale)
					game.font.getData().setScale(menuItemTimer0);
				else
					game.font.getData().setScale(1f);
				game.font.setColor(Color.RED);
				game.font.draw(game.batch, "PRESS ENTER?!,      ...      please.", Gdx.graphics.getWidth() * timer / 4 * .283f,
						Gdx.graphics.getHeight() * timer / 2 * .6f);
				game.font.getData().setScale(.6f);// this resets the rest of the font to normal (it's only used for the above
													// setting)
				game.font.draw(game.batch, "this text is so tiny and so cute!", Gdx.graphics.getWidth() * .1f - 520, Gdx.graphics.getHeight() * .101f - 50,
						_targetWidth, _halign, _wrap);
				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 < 10; i++) {
					game.font.setColor(setColorNumber(i), 0, 0, 1);
					if (isNameZoomRight && isNameZoomUp)
						game.font.draw(game.batch, "You, please, like, what, no really, " + message + " hahaha!", (float) (nameZoomX - 10 * i),
								(float) (nameZoomY - 10 * i));
					else if (isNameZoomRight && !isNameZoomUp)
						game.font.draw(game.batch, "You, please, like, what, no really, " + message + " hahaha!", (float) (nameZoomX - 10 * i),
								(float) (nameZoomY + 10 * i));
					else if (!isNameZoomRight && isNameZoomUp)
						game.font.draw(game.batch, "You, please, like, what, no really, " + message + " hahaha!", (float) (nameZoomX + 10 * i),
								(float) (nameZoomY - 10 * i));
					else
						game.font.draw(game.batch, "You, please, like, what, no really, " + message + " hahaha!", (float) (nameZoomX + 10 * i),
								(float) (nameZoomY + 10 * i));
				}
				game.font.setColor(Color.MAROON);
				game.font.draw(game.batch, "You, please, like, what, no really, " + message + " hahaha!", 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 >= ddd.MW - 800)
					isNameZoomRight = false;
				if (isNameZoomRight)
					nameZoomX += MathUtils.random(2f, 4f);
				else
					nameZoomX -= MathUtils.random(2f, 4f);
				if (nameZoomY <= -16) {
					nameZoomY = ddd.MH;
				}
				nameZoomY -= MathUtils.random(2f, 2f);
			}
			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();
	}

}
