package com.ld50.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;

public class WinScreen implements Screen {
	final HF 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(60f, 80f);
	private boolean isTitleLettersDown;
	private boolean _isTwo;
	private boolean _isThree;
	private int cameraShakeTimer;
	private Sound thump;
	private Sound sfx_titlescreen;
	private float menuItemTimer0;
	private boolean isMenuDone;
	private float timer;
	private boolean up;
	private float _posX = .802f;
	private float _posXS = .001f;

	// font title? yeah! (later)
	public WinScreen(final HF game) {
		this.game = game;
		camera = new OrthographicCamera();
		vport = new FitViewport(HF.MW, HF.MH, camera);
		sfx_titlescreen = Gdx.audio.newSound(Gdx.files.internal("sfx/menuaaa.ogg"));
		// menustuff:
		pp = new PausePerson(Gdx.graphics.getWidth() - 1010f, Gdx.graphics.getHeight() - 240f);
		enter = Gdx.audio.newSound(Gdx.files.internal("sfx/enter.ogg"));
		enter2 = Gdx.audio.newSound(Gdx.files.internal("sfx/enter2.ogg"));
		// music
		chillMusic = Gdx.audio.newMusic(Gdx.files.internal("menu.ogg"));
		chillMusic.setLooping(true);
		thump = Gdx.audio.newSound(Gdx.files.internal("sfx/thump.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();
					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(2f);
		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 += .0005f;
			else
				timer -= .0006f;
			if (timer >= .67f) {
				up = 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 = 800f;
			int _halign = 0;
			boolean _wrap = true;
			float _endPosX = .402f;
			float _endPosXS = .4f;
			float _endScale = 6f;
			float _endMenuScale = 1f;
			float _o = 350f;
			game.font.getData().setScale(titleTimerOne);
			game.font.setColor(Color.SKY);
			game.font.draw(game.batch, "YOU WIN", Gdx.graphics.getWidth() * _posXS - _o, Gdx.graphics.getHeight() * .903f, _targetWidth, _halign, _wrap);
			game.font.setColor(Color.WHITE);
			game.font.draw(game.batch, "YOU WIN", Gdx.graphics.getWidth() * _posX - _o, Gdx.graphics.getHeight() * .9f, _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 (_posX <= _endPosX)
				_posX = _endPosX;
			if (_posX > _endPosX)
				_posX -= .001f;
			if (_posXS >= _endPosXS)
				_posXS = _endPosXS;
			if (_posXS < _endPosXS)
				_posXS += .001f;
			if (_isTwo) {
				if (titleTimerTwo > _endMenuScale)
					game.font.getData().setScale(titleTimerTwo);
				else
					game.font.getData().setScale(1.01f);
				game.font.setColor(Color.BLUE);
				game.font.draw(game.batch, "Score: " + HF.Score, Gdx.graphics.getWidth() * .401f - _o, Gdx.graphics.getHeight() * .201f, _targetWidth, _halign,
						_wrap);
				game.font.draw(game.batch, "You made it! All the way to the goal! VICTORY!", Gdx.graphics.getWidth() * .301f - _o,
						Gdx.graphics.getHeight() * .301f, _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, "Score: " + HF.Score, Gdx.graphics.getWidth() * .4f - _o, Gdx.graphics.getHeight() * .2f, _targetWidth, _halign,
						_wrap);
				game.font.draw(game.batch, "You made it! All the way to the goal! VICTORY!", Gdx.graphics.getWidth() * .301f - _o,
						Gdx.graphics.getHeight() * .301f, _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) {
				game.font.getData().setScale(1.01f);
				game.font.setColor(Color.CHARTREUSE);
				game.font.draw(game.batch, "Press ENTER To go to Start", Gdx.graphics.getWidth() * timer / 2 * .2f, Gdx.graphics.getHeight() * timer / 2 * .6f);
				game.font.getData().setScale(1f);
				game.font.setColor(Color.CORAL);
				game.font.draw(game.batch, "Press ENTER To go to Start", 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--;
			} 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();
	}

}
