package school_project.Menu;

import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import school_project.Controller;
import school_project.GameUI;
import school_project.Parsers.FileParserFactory;

import java.io.File;
import java.io.IOException;

public class MenuLevel extends GridPane {
    private int StartLevel;
    public MenuLevel(int choose_day) {

        //create all Object that I need
        RowConstraints[] Rows = new RowConstraints[5];
        ColumnConstraints[] Columns = new ColumnConstraints[3];
        Button[] Days = new Button[3];
        Button BckMenu = new Button("Back to menu");

        BckMenu.setOnAction(event -> Controller.switchRoot(new MenuAccueil()));
        setHalignment(BckMenu,HPos.CENTER);
        add(BckMenu,2,4);


        for (int i = 0; i < Days.length; i++){
            Days[i] = new Button("Day"+(i+1));
        }

        //it's here that I know which day I can show on the screen

        if (choose_day == 1) {
            StartLevel = 1;
            add(Days[1],0,0);
            add(Days[2],2,0);
            setHalignment(Days[1], HPos.CENTER);
            setHalignment(Days[2], HPos.CENTER);
        }

        else if (choose_day == 2) {
            StartLevel = 11;
            add(Days[0], 0, 0);
            add(Days[2], 2, 0);
            setHalignment(Days[0], HPos.CENTER);
            setHalignment(Days[2], HPos.CENTER);
        }

        else if (choose_day == 3) {
            StartLevel = 21;
            add(Days[0], 0, 0);
            add(Days[1], 2, 0);
            setHalignment(Days[0], HPos.CENTER);
            setHalignment(Days[1], HPos.CENTER);
        }

        Days[0].setOnAction(event -> Controller.switchRoot(new MenuLevel(1)));
        Days[1].setOnAction(event -> Controller.switchRoot(new MenuLevel(2)));
        Days[2].setOnAction(event -> Controller.switchRoot(new MenuLevel(3)));

        //It's here that I put all buttons where I need (base on column not row)
        for (int i = 0; i < 3; i++) {
            for (int j = 1; j < 5; j++) {
				Button levelButton = new Button("level "+(StartLevel));
                levelButton.setOnAction(event -> {
                    try {
                        String levelName = ((Button)event.getSource()).getText().replace(" ", "") + ".level";
                        System.out.println(levelName);
                        GameUI level = new GameUI(FileParserFactory.loadMapFromFile(new File(Controller.class.getResource("levels/" + levelName).getFile())));
                        Controller.switchRoot(level);
                    } catch (IOException e) {
                        System.out.println("Le niveau " + StartLevel + "n'existe pas.");
                    }
                });
                if(i==0){
                    StartLevel+=3;
                    add(levelButton,i,j);
                    setHalignment(levelButton,HPos.CENTER);
                    if(j==4){
                        StartLevel-=11;
                    }
                }
                else if(i==1&&j!=4) {
                    StartLevel += 3;
                    add(levelButton, i, j);
                    setHalignment(levelButton,HPos.CENTER);
                    if (j == 3) {
                        StartLevel -=8;

                    }
                }
                else if(i==2&&j!=4){
                    StartLevel+=3;
                    add(levelButton,i,j);
                    setHalignment(levelButton,HPos.CENTER);
                }
            }
        }

        for(int i=0;i<=2;i++){
            Columns[i] = new ColumnConstraints();
            Columns[i].setPercentWidth(33);
            getColumnConstraints().addAll(Columns[i]);
        }

        for (int i= 0;i<=4;i++){
            Rows[i] = new RowConstraints();
            Rows[i].setPercentHeight(20);
            getRowConstraints().addAll(Rows[i]);
        }

        setHgap(20);
        setVgap(20);
        setPadding(new Insets(20,10,10,20));
        getStyleClass().add("GridPane");
        getStylesheets().add(String.valueOf(getClass().getResource("StyleMenuAcceuil.css")));
    }
}

