package com.example.happy_bird;

import javafx.animation.AnimationTimer;
import javafx.animation.Transition;
import javafx.animation.TranslateTransition;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.animation.TranslateTransition;
import javafx.util.Duration;

public class Pipes extends Rectangle {
    private int y;
    private Rectangle topPipe;
    private Rectangle bottomPipe;
    private int distanceBeweenPipes = 250;
    private boolean isPassedOn = false;
    /***
     * Konstruktor GameObject
     * Variablen werden zugewiesen
     * param x      x-Position
     * param y      y-Position
     * param width  Breite
     * param height Höhe
     */
    public Pipes(){

    }


    public Rectangle[] createPipes(Transition top, Transition bottom, Pane pane) {
        int y = (int) (Math.random() * (200))-300;
        Rectangle topPipe = new Rectangle(1000, y, 91, 500);
        Rectangle bottomPipe = new Rectangle(1000, y + 400 + 250, 91, 500);


        Image img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
        topPipe.setFill(new ImagePattern(img));

        img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
        bottomPipe.setFill(new ImagePattern(img));

        top = new TranslateTransition();
        bottom = new TranslateTransition();

        ((TranslateTransition) top).setNode(topPipe);
        ((TranslateTransition) top).setDuration(Duration.seconds(5));
        ((TranslateTransition) top).setToX(-1200);
        top.setOnFinished(e -> {
            pane.getChildren().remove(topPipe);
        });

        ((TranslateTransition) bottom).setNode(bottomPipe);
        ((TranslateTransition) bottom).setDuration(Duration.seconds(5));
        ((TranslateTransition) bottom).setToX(-1200);
        bottom.setOnFinished(e -> {
            pane.getChildren().remove(bottomPipe);
        });

        Rectangle[] pipes = {topPipe, bottomPipe};
        return pipes;
    }

    public void destroy(Pane pane) {

        pane.getChildren().remove(topPipe);
        pane.getChildren().remove(bottomPipe);
    }

    /***
     * Setzt für die beiden Rechtecke der Pipes das Hintergrundbild
     */
    public void setImage() {
        Image img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
        topPipe.setFill(new ImagePattern(img));

        img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
        bottomPipe.setFill(new ImagePattern(img));
    }

    /***
     * Setzt ein Object auf eine neue Position
     * param newX  neue x-Position
     */
    public void resetToNewPosition(int newX) {
        topPipe.setX(newX);
        bottomPipe.setX(newX);
        //xKoord = newX;
    }

    /*
    // Prüfung ob Rectangle true oder false ist
    public boolean intersect(Rectangle rectangle) {
        return rectangle.intersects(topPipe) || rectangle.intersects(bottomPipe);
    }

    // Röhren Counter → Check, wenn Röhre durchflogen ist
    public boolean passedOn(Rectangle rectangle) {
        return rectangle.x > xKoord + width && !isPassedOn;
    }

    // Bei Bewegung der X Koordinate werden Röhren generiert
    public void moveX(int dx) {
        xKoord -= dx;
        topPipe.x -= dx;
        bottomPipe.x -= dx;
    }*/
}