package com.example.happy_bird;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Slider;
import javafx.scene.image.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;

public class HappyBirdMain extends Application {

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

    @Override
    public void start(Stage stage) throws IOException {
        Pane pane = new Pane();

        Scene scene = new Scene(pane, 800, 700);
        stage.setTitle("Happy Bird");
        stage.setScene(scene);

        //Backgroundimage
        Image backgroundImage = new Image("file:src/main/resources/com/example/happy_bird/pics/Background.png");
        BackgroundImage backgroundImageImg = new BackgroundImage(
                backgroundImage,
                BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.DEFAULT,
                BackgroundSize.DEFAULT
        );

        Image bird = new Image("file:src/main/resources/com/example/happy_bird/pics/bird.png", 50, 35, false, false);
        /*ImageView birdView = new ImageView();
        birdView.setFitHeight(50);
        birdView.setFitWidth(50);
        birdView.setLayoutX(50);
        birdView.setLayoutY(325);

         */

        Image upperPipe = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeOben.png", 86, 300, false, false);
        Image lowerPipe = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeUnten.png", 86, 300, false, false);

        Background background = new Background(backgroundImageImg);
        pane.setBackground(background);

        //Headline-Label
        Label headline = new Label("Happy Bird");
        headline.setPrefSize(300, 75);
        headline.setTextAlignment(TextAlignment.CENTER);
        headline.setStyle("-fx-background-color: #DED894; " +
                "-fx-text-fill: #000000; " +
                "-fx-font-size: 35px; " +
                "-fx-border-width: 5px;" +
                "-fx-border-color: #543847;" +
                "-fx-font-weight: bold;" +
                "-fx-alignment: center;" +
                "-fx-background-radius: 20px;" +
                "-fx-border-radius: 15px;");
        headline.setLayoutX(250);
        headline.setLayoutY(100);

        pane.getChildren().add(headline);

        //Buttons:
        Button startButton = new Button("START");
        Button highscoresButton = new Button("HIGHSCORES");
        Button settingsButton = new Button("EINSTELLUNGEN");
        Button startGame = new Button("SPIEL STARTEN");

        Button buttons[] = {startButton, highscoresButton, settingsButton, startGame};
        Button menuButton = new Button("ZURÜCK");
        menuButton.setStyle("-fx-background-color: #e86000; " +
                "-fx-text-fill: #FFFFFF; " +
                "-fx-font-size: 20px; " +
                "-fx-border-width: 5px;" +
                "-fx-border-color: #FFFFFF;" +
                "-fx-font-weight: bold;" +
                "-fx-border-radius: 15px;" +
                "-fx-background-radius: 20px;");
        pane.getChildren().add(menuButton);
        menuButton.setPrefSize(200, 50);
        menuButton.setLayoutX(575);
        menuButton.setLayoutY(625);
        menuButton.setVisible(false);
        startGame.setVisible(false);


        //Button buttons[] = {startButton, highscoresButton, settingsButton};

        int yPosition = 225;
        for (Button button : buttons) {
            pane.getChildren().add(button);
            button.setPrefSize(200, 50);
            button.setLayoutX(300);
            button.setLayoutY(yPosition);
            yPosition += 100;

            button.setStyle("-fx-background-color: #e86000; " +
                    "-fx-text-fill: #FFFFFF; " +
                    "-fx-font-size: 20px; " +
                    "-fx-border-width: 5px;" +
                    "-fx-border-color: #FFFFFF;" +
                    "-fx-font-weight: bold;" +
                    "-fx-border-radius: 15px;" +
                    "-fx-background-radius: 20px;");
        }

        startGame.setVisible(false);

        Label nameLabel = new Label("Name:");
        Label acronymLabel = new Label("Kürzel:");

        Label startLabels[] = {nameLabel, acronymLabel};
        startButton.setOnAction(event -> {
            headline.setText("Start");
            for (Button button : buttons) {
                button.setVisible(false);
                menuButton.setVisible(true);
            }
        });

        int yPositionStartLabels = 225;
        for (Label label : startLabels) {
            label.setPrefSize(250, 50);
            label.setStyle("-fx-background-color: #DED894; " +
                    "-fx-text-fill: #000000; " +
                    "-fx-font-size: 20px; " +
                    "-fx-border-width: 5px;" +
                    "-fx-border-color: #543847;" +
                    "-fx-font-weight: bold;" +
                    "-fx-alignment: center;" +
                    "-fx-background-radius: 20px;" +
                    "-fx-border-radius: 15px;");

            label.setLayoutX(275);
            label.setLayoutY(yPositionStartLabels);
            yPositionStartLabels += 150;

            pane.getChildren().add(label);
            label.setVisible(false);
        }

        TextField name = new TextField();
        TextField acronym = new TextField();

        TextField inputFields[] = {name, acronym};

        int yPositionInputFields = 290;
        for (TextField feld : inputFields) {
            feld.setPrefSize(250, 50);
            feld.setStyle("-fx-font-size: 20px;" +
                    "-fx-font-weight: bold;");

            feld.setLayoutX(275);
            feld.setLayoutY(yPositionInputFields);
            yPositionInputFields += 150;

            pane.getChildren().add(feld);
            feld.setVisible(false);
        }


        startButton.setOnAction(event ->  {
            headline.setText("Start");
            for(Button button : buttons) {
                button.setVisible(false);

            }

            startGame.setVisible(true);
            menuButton.setVisible(true);
            startGame.setLayoutY(520);

            for(TextField feld : inputFields) {
                feld.setVisible(true);
            }
            for (Label label : startLabels )
            {
                label.setVisible(true);
            }
        });


        startGame.setOnAction(event ->  {
            for(TextField feld : inputFields) {
                feld.setVisible(false);
            }
            for (Label label : startLabels )
            {
                label.setVisible(false);
            }
            startGame.setVisible(false);
            menuButton.setVisible(false);
            headline.setVisible(false);
            ImageView birdView = new ImageView(bird);
            pane.getChildren().add(birdView);
            birdView.setLayoutY(325);
            birdView.setLayoutX(50);

            ImageView upperPipeView = new ImageView(upperPipe);
            pane.getChildren().add(upperPipeView);
            upperPipeView.setLayoutX(400);
            upperPipeView.setLayoutY(0);

            ImageView lowerPipeView = new ImageView(lowerPipe);
            pane.getChildren().add(lowerPipeView);
            lowerPipeView.setLayoutX(400);
            lowerPipeView.setLayoutY(430);
        });



        //Slider:
        Slider soundSlider = new Slider();
        pane.getChildren().add(soundSlider);
        soundSlider.setPrefSize(200, 5);
        soundSlider.setLayoutX(300);
        soundSlider.setLayoutY(295);
        soundSlider.setVisible(false);

        Label soundLabel = new Label("SOUND");
        soundLabel.setPrefSize(200, 55);
        soundLabel.setTextAlignment(TextAlignment.CENTER);
        soundLabel.setStyle("-fx-background-color: #DED894; " +
                "-fx-text-fill: #000000; " +
                "-fx-font-size: 20px; " +
                "-fx-border-width: 5px;" +
                "-fx-border-color: #543847;" +
                "-fx-font-weight: bold;" +
                "-fx-alignment: center;" +
                "-fx-background-radius: 20px;" +
                "-fx-border-radius: 15px;");
        soundLabel.setLayoutX(300);
        soundLabel.setLayoutY(225);

        pane.getChildren().add(soundLabel);
        soundLabel.setVisible(false);

        settingsButton.setOnAction(event -> {
            headline.setText("EINSTELLUNGEN");
            startButton.setVisible(false);
            highscoresButton.setVisible(false);
            settingsButton.setVisible(false);
            menuButton.setVisible(true);
            soundSlider.setVisible(true);
            soundLabel.setVisible(true);
        });

        highscoresButton.setOnAction(event -> {
            headline.setText("HIGHSCORES");
            startButton.setVisible(false);
            highscoresButton.setVisible(false);
            settingsButton.setVisible(false);
            menuButton.setVisible(true);
        });

        menuButton.setOnAction(event -> {
            headline.setText("HAPPY BIRD");
            startButton.setVisible(true);
            highscoresButton.setVisible(true);
            settingsButton.setVisible(true);
            menuButton.setVisible(false);
            soundSlider.setVisible(false);
            soundLabel.setVisible(false);
            startGame.setVisible(false);
            for(TextField feld : inputFields) {
                feld.setVisible(false);
            }
            for (Label label : startLabels )
            {
                label.setVisible(false);
            }
        });

        stage.show();
    }
}
