import Phaser from "phaser";

export default class InfoScene extends Phaser.Scene {
  constructor() {
    super({ key: "InfoScene" });
  }

  create(): void {
    this.content = [
      window.gametitle,
	  "",
	  "Press W,A,S,D to move",
	  "Press W,A,S,D + Space to dash",
	  "Press left mouse button to attack",
	  "Press R to reset the game"
	  "",
	  "Status: Wounded (-1 hitpoint / 5sec)",
	  "Hitpoints: 100",
	  "Inventory: Nothing",
	  "Mortality: 0"
    ];
    this.text = this.add.text(25, 25, this.content, {
      fontFamily: "sans-serif",
      color: "#ffffff"
    });
    this.text.setAlpha(0.9);
	
	this.pauseText = this.add.text(window.innerWidth/2, window.innerHeight/2, ["Game paused", "Click to resume"], {
      fontFamily: "sans-serif",
      color: "#ffffff",
	  fontSize: 40
    });
	this.pauseText.setOrigin(0.5);
	this.pauseText.setShadow(3, 3, 'rgba(0,0,0,0.5)', 2);
	this.pauseText.setVisible(false);

	this.deathText = this.add.text(window.innerWidth/2, window.innerHeight/2, ["GAME OVER", ""], {
      fontFamily: "sans-serif",
      color: "#ffffff",
	  fontSize: 70
    });
	this.deathText.setOrigin(0.5);
	this.deathText.setShadow(3, 3, 'rgba(255,255,255,0.5)', 2);
	this.deathText.setVisible(false);
	
	this.deathTextB = this.add.text(window.innerWidth/2, window.innerHeight/2, ["", "Press F5 to restart the level"], {
      fontFamily: "sans-serif",
      color: "#ffffff",
	  fontSize: 36
    });
	this.deathTextB.setOrigin(0.5);
	this.deathTextB.setShadow(3, 3, 'rgba(255,255,255,0.5)', 2);
	this.deathTextB.setVisible(false);
	
	this.dialogText = this.add.text(window.innerWidth/2, window.innerHeight/2, [
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"  Professor Quirkelfox:",
		"  Hello my new warrior. Welcome in your new live.",
		"  I have work for you. You need to bring me blood so i can continue my research.",
		"  But be carefull, in the cellar are old experiments and other crazy stuff.",
		"  Oh, one thing you should know is that i made a little mistake when i created you.",
		"  You are constantly loosing blood. So you better be fast."
		"  Also, if you find organs i can exchange them with your inferior organs. Hehehe.",
		"  [Press space to continue]"
	], {
      fontFamily: "sans-serif",
      color: "#ffffff",
	  fontSize: 20
    });
	this.dialogText.setOrigin(0.5);
	
	var level = +location.search.substring(1);
	this.level = level;
	
	if (!localStorage.profState) {
		localStorage.profState = "0";
	}
	window.profState = localStorage.profState;
	
	if (!localStorage.julinaState) {
		localStorage.julinaState = "0";
	}
	window.julinaState = localStorage.julinaState;
  }
  
  update(time: number, delta: number) {
	if (this.level === 0) {
		if (Phaser.Math.Distance.Between(window.professor.sprite.x, window.professor.sprite.y, window.player.sprite.x, window.player.sprite.y) < 40 && window.player.hp > 100 && !window.dialogOpen) {
			window.player.hp = 100;
			window.dialogOpen = true;
			this.dialogText.text = [
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"  Professor Quirkelfox:",
				"  Thank you for your blood. Hehehe.",
				"  [Press space to continue]"
			];
		}
		
		if (window.profState === "0" && !window.dialogOpen) {
			window.dialogOpen = true;
			window.profState = "1";
		} else if (window.profState === "1" && window.inventory.length > 0 && Phaser.Math.Distance.Between(window.professor.sprite.x, window.professor.sprite.y, window.player.sprite.x, window.player.sprite.y) < 40) {
			switch(window.inventory.pop()) {
				case 'Lung':
					window.player.maxhp += 50;
					window.player.hp += 50;
					break;
				case 'Arteries':
				case 'Veins':
					window.player.maxhp += 100;
					window.player.hp += 100;
					break;
				case 'Heart':
					window.player.maxhp += 150;
					window.player.hp += 150;
					break;
			}
			window.playerLeak += 1;
			window.dialogOpen = true;
			this.dialogText.text = [
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"  Professor Quirkelfox:",
				"  I exchanged your inferior organ with the one you brought me.",
				"  Unfortunately i did a small mistake and your are now leaking",
				"  even more blood than before. Hehehe.",
				"  [Press space to continue]"
			];
		} else if (window.player.maxhp >= 550 && window.profState === "1") {
			window.profState = "2";
			this.scene.stop("InfoScene");
			this.scene.stop("DungeonScene");
			this.scene.start("CreditsScene");
		}
	}
	
	if (this.level === 1 && window.enemy1[24].alive === false && Phaser.Math.Distance.Between(window.julina.sprite.x, window.julina.sprite.y, window.player.sprite.x, window.player.sprite.y) < 110) {
		if (window.julinaState === "0" && !window.dialogOpen) { 
			window.dialogOpen = true;
			window.julinaState = "1";
			this.dialogText.text = [
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"  Julina:",
				"  Hey warrior, you look better than the rest of us down here. How are you?  ",
				"  Oh it does not matter. The only important thing is that you are currently alive  ",
				"  I can help you if you want. Bring me organs and i will upgrade yours.",
				"  You are leaking a little bit.",
				"  The professor is not a good person. If no one can stop him we will all die here.",
				"  Hihihihi. I mean all others including me are almost death. ",
				"  But there will follow many more if you do not stop him.",
				"  Good luck",
				"  [Press space to continue]"
			];
		} else if (window.julinaState === "1" && window.inventory.length > 0 && Phaser.Math.Distance.Between(window.julina.sprite.x, window.julina.sprite.y, window.player.sprite.x, window.player.sprite.y) < 40 && !window.dialogOpen) {
			switch(window.inventory.pop()) {
				case 'Lung':
					window.player.maxhp += 50;
					window.player.hp += 50;
					break;
				case 'Arteries':
				case 'Veins':
					window.player.maxhp += 100;
					window.player.hp += 100;
					break;
				case 'Heart':
					window.player.maxhp += 150;
					window.player.hp += 150;
					break;
			}
			window.dialogOpen = true;
			this.dialogText.text = [
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"  Julina:",
				"  I upgraded your organ. If you bring me another one you might  ",
				"  soon be able to kill the professor and take revenge.",
				"  [Press space to continue]"
			];
		} else if (window.player.maxhp >= 550 && window.julinaState === "1") {
			window.julinaState = "2"
			window.dialogOpen = true;
			this.dialogText.text = [
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"",
				"  Julina:",
				"  You are now strong enough to fight the professor.  ",
				"  I wish you luck. I would really like to help you but  ",
				"  as you can see i'm heavily wounded. You have to do this ",
				"  on your own.",
				"  [Press space to continue]"
			];
		}
	}
  
    var hp = Math.max(0, window.player.hp);
	var status = "Wounded (-" + window.playerLeak + " hitpoints/5sec)";
	var alive = window.player.alive;
	var inventory = window.inventory.length === 0 ? "Nothing" : window.inventory.join(", ");
	this.content[7] = "Status: " + status;
	this.content[8] = "Hitpoints: " + hp + "/" + window.player.maxhp;
	this.content[9] = "Inventory: " + inventory;
	if(localStorage.deaths !== undefined) this.content[10] = "Mortality: " + localStorage.deaths;
	this.text.text = this.content;
	this.pauseText.setVisible(false);
	this.deathText.setVisible(false);
	this.deathTextB.setVisible(false);
	this.dialogText.setVisible(false);
	this.text.setVisible(true);
	if (window.paused) {
		this.pauseText.x = window.innerWidth/2;
		this.pauseText.y = window.innerHeight/2;
		this.pauseText.setVisible(true);
		this.deathText.setVisible(false);
		this.deathTextB.setVisible(false);
		this.text.setVisible(false);
		this.dialogText.setVisible(false);
	} else if (window.dialogOpen) {
		this.dialogText.setVisible(true);
		this.pauseText.setVisible(false);
		this.deathText.setVisible(false);
		this.deathTextB.setVisible(false);
		this.text.setVisible(false);
	} else if (alive == false) {
		this.deathText.x = window.innerWidth/2;
		this.deathText.y = window.innerHeight/2;
		this.deathTextB.x = window.innerWidth/2;
		this.deathTextB.y = window.innerHeight/2;
		this.deathText.setVisible(true);
		this.deathTextB.setVisible(true);
		this.pauseText.setVisible(false);
		this.text.setVisible(false);
		this.dialogText.setVisible(false);
	}
  }
}
