import java.util.ArrayList;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.converter.IntegerStringConverter;
import javafx.scene.paint.ImagePattern;
import javafx.scene.image.Image;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;

public class main extends Application {
    Group root;
    Stage primaryStage;
    Level level;
    Scene scene;
    Pterodactyl terodactilo;
    spike spikes;
    Score scorelabel;
    boolean gameOver;
    Image over;
    ImageView overv;
    Timeline animation;
    
    public void game(){
        if(terodactilo.gameOver){
            overv.setImage(over);
        }
        else{
            overv.setImage(null);
        }
    }

    @Override
    public void start(Stage window) {
        primaryStage = window;
        primaryStage.setTitle("Pterodactyl's Revenge");
        primaryStage.setHeight(700);
        primaryStage.setWidth(800);
        primaryStage.setResizable(false);
        Image image = new Image("/img/icon.png");
        primaryStage.getIcons().add(image);

        //inicializacion de objetos
        root = new Group();
        scene = new Scene(root);
        level = new Level(scene);
        
        
        scorelabel = new Score(level);
        terodactilo = new Pterodactyl(scorelabel);
        spikes = new spike(terodactilo);
        
        
        
        root.getChildren().addAll(scorelabel.getView());
        root.getChildren().addAll(spikes);
        root.getChildren().add(terodactilo);
        
        over = new Image("img/gameover.png");
        overv = new ImageView();
        level.levelTransition(terodactilo.getLevel());

        overv.setX(258);
        overv.setY(50);

        root.getChildren().addAll(overv);

        animation = new Timeline(new KeyFrame(Duration.millis(30), e->game()));
        animation.setCycleCount(Timeline.INDEFINITE);
        
        animation.play();
                
        scene.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.SPACE && terodactilo.isFlying()) {
                terodactilo.jump();
            }
            else if(e.getCode() == KeyCode.SPACE && !terodactilo.isFlying()){
                terodactilo.play();
            }
        });
        scene.setOnKeyReleased(e -> {
            if (e.getCode() == KeyCode.SPACE && terodactilo.isFlying()) {
                terodactilo.stopJumping();
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();

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