package game;

import game.menu.Menu;
import game.menu.StatusMenu;
import game.player.EnemyNames;
import game.player.Player;
import game.player.Type;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.util.HashMap;

public class Main extends Application {

    private static final int WIDTH = 1240;  //800
    private static final int HIGH = 826;    //width / 12 * 9;
    private static final int GAME_WIDTH = 1240;
    private static final int GAME_HIGH = 826;
    private static final String BACKGROUND = "universe.jpg";
    private static final String MAP = "map.png";

    private static Menu menu;
    private StatusMenu status;
    private Group root;
    private Scene rootScene;
    public Scene gameScene;
    private Canvas canvas;
    private GraphicsContext graphicsContext;
    private Player player;
    public HashMap<String, Player> enemies;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {
        super.init();
        this.root = new Group();                                        //create root node
        this.rootScene = new Scene(this.root, WIDTH, HIGH);
        //this.gameScene = new Scene(this.root, GAME_WIDTH, GAME_HIGH);
        this.canvas = new Canvas(WIDTH, HIGH);
        this.graphicsContext = this.canvas.getGraphicsContext2D();
        this.graphicsContext.drawImage(new Image(BACKGROUND), 0, 0);    //draw background
        this.graphicsContext.drawImage(new Image(MAP), 100, 100);       //draw map
        this.root.getChildren().add(canvas);
        this.status = new StatusMenu();
        this.menu = new Menu(this.root, this.status, this.player);
        this.enemies = new HashMap<>();
        for (int i = 0; i < 20; i++) {
            EnemyNames name = EnemyNames.values()[i];
            this.enemies.put(name + "", new Player( name + "", this.status, Type.ENEMY));
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Game Nozdormu");
        primaryStage.setScene(this.rootScene);
        primaryStage.show();

        Thread gameLoop = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Main.this.rootScene.setOnKeyPressed(new EventHandler<KeyEvent>() {
                        @Override
                        public void handle(KeyEvent event) {
                            //TODO move steps
                            System.out.println(event);
                            System.out.println();
                        }
                    });
                }
            }
        });

        gameLoop.start();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        this.status = null;
        this.root = null;
        this.rootScene = null;
        this.player = null;
        this.graphicsContext = null;
        this.enemies = null;
        this.canvas = null;
        //release res
    }
}

@@ -143,16 +143,56 @@
 r.Type;%0A
+import javafx.animation.AnimationTimer;%0A
 import j
@@ -709,18 +709,18 @@
 IDTH = 1
-24
+00
 0;%0A    p
@@ -756,18 +756,18 @@
 HIGH = 8
-26
+00
 ;%0A    pr
@@ -883,23 +883,16 @@
 private 
-static 
 Menu men
@@ -1936,29 +1936,16 @@
 is.root,
- this.status,
  this.pl
@@ -2150,21 +2150,8 @@
  %22%22,
- this.status,
  Typ
@@ -2300,16 +2300,17 @@
 ormu%22);%0A
+%0A
         
@@ -2352,38 +2352,8 @@
 e);%0A
-        primaryStage.show();%0A%0A
     
@@ -2664,16 +2664,85 @@
 ent 
-event) %7B
+key) %7B%0A                            String keyCode = key.getCode().toString();
 %0A   
@@ -2783,16 +2783,23 @@
 ve steps
+ player
 %0A       
@@ -2838,21 +2838,19 @@
 println(
-event
+key
 );%0A     
@@ -2891,16 +2891,23 @@
 println(
+keyCode
 );%0A     
@@ -3008,22 +3008,310 @@
     
-gameLoop.start
+//move heroes in this timer%0A        new AnimationTimer() %7B%0A            public void handle(long currentNanoTime) %7B%0A                Main.this.menu.updateGridMenu();%0A                System.out.println(currentNanoTime);%0A            %7D%0A        %7D.start();%0A%0A        gameLoop.start();%0A%0A        primaryStage.show
 ();%0A

add animation timer (loop for all move on enemy and update - re render on game)