package com.ld52.agent;

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;

public class LoseScreen implements Screen {
	final Agent 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;

	// font title? yeah! (later)
	public LoseScreen(final Agent game) {
		this.game = game;
		camera = new OrthographicCamera();
		vport = new FitViewport(Agent.MW, Agent.MH, camera);
		sfx_titlescreen = Gdx.audio.newSound(Gdx.files.internal("sfx/doubleClick.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/titleMenu.ogg"));
		chillMusic.setLooping(true);
		thump = Gdx.audio.newSound(Gdx.files.internal("sfx/spin.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) {
					Agent.Health = 200;
					Agent.Score = 0;
					Agent.Speed = 100;
					// 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!!");
					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 LoseScreen(game));
			}
		}
	}

	public void dropTitleLetters(OrthographicCamera camera) {
		if (!isTitleLettersDown) {
			float _targetWidth = 700f;
			int _halign = 0;
			boolean _wrap = true;
			float _endPosX = .902f;
			float _endPosXS = .91f;
			float _endScale = 4f;
			float _endMenuScale = 1f;
			float _o = 0f;
			game.font.getData().setScale(titleTimerOne);
			game.font.setColor(Color.GRAY);
			game.font.draw(game.batch, "At least you know how to lose...", Gdx.graphics.getWidth() * .22f, Gdx.graphics.getHeight() * _posYS - _o, _targetWidth,
					_halign, _wrap);
			game.font.setColor(Color.RED);
			game.font.draw(game.batch, "At least you know how to lose...", Gdx.graphics.getWidth() * .222f, 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, "Score: " + Agent.Score, Gdx.graphics.getWidth() * .101f - _o, Gdx.graphics.getHeight() * .401f, _targetWidth,
						_halign, _wrap);
				game.font.draw(game.batch, "your score... what?! :(", Gdx.graphics.getWidth() * .341f - _o, Gdx.graphics.getHeight() * .401f, _targetWidth,
						_halign, _wrap);
				game.font.draw(game.batch, "next time try to find the BIG   ^   TRUCK", Gdx.graphics.getWidth() * .541f - _o, Gdx.graphics.getHeight() * .101f,
						_targetWidth, _halign, _wrap);
				game.font.setColor(Color.YELLOW);
				game.font.draw(game.batch, "YELLOW", Gdx.graphics.getWidth() * timer / 2 * 1.490f - _o, Gdx.graphics.getHeight() * timer2 * .281f, _targetWidth,
						_halign, _wrap);
				game.font.setColor(Color.BROWN);
				game.font.draw(game.batch, "Press Enter To Start", 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 Start", 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.DARK_GRAY);
				game.font.draw(game.batch, "PRESS ENTER TO GO TO STARRT___,.@#$_()=", Gdx.graphics.getWidth() * timer2 / 4 * .466f,
						Gdx.graphics.getHeight() * timer2 / 2 * .79f);
				if (menuItemTimer0 > _endMenuScale)
					game.font.getData().setScale(menuItemTimer0);
				else
					game.font.getData().setScale(1f);
				game.font.setColor(Color.GRAY);
				game.font.draw(game.batch, "PRESS ENTER TO GO TO STARRT___,.@#$_()=", Gdx.graphics.getWidth() * timer / 2 * .203f,
						Gdx.graphics.getHeight() * timer / 2 * .6f);
				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;
				}
			}
			if (cameraShakeTimer > 0) {
				u.UsefulMethods.MoveCameraAroundRandomly(camera, 1, 1);
				camera.update();
				cameraShakeTimer -= 1;
			} else {
				camera.position.x = cameraInitX;
				camera.position.y = cameraInitY;
			}
		}

	}

	@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();
	}

}
