package com.example.bricklog.view;

import com.example.bricklog.model.Build;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.ArrayList;
import java.util.List;

public class GalleryLayout extends BorderPane {
    private final GridPane gridView;
    private final ListView<String> listView;
    private boolean isGridView;

    public GalleryLayout() {
        ToggleGroup toggleGroup = new ToggleGroup();

        ToggleButton gridButton = new ToggleButton("Grid");
        ToggleButton listButton = new ToggleButton("List");

        gridButton.setToggleGroup(toggleGroup);
        listButton.setToggleGroup(toggleGroup);

        gridButton.setSelected(true);
        isGridView = true;

        HBox toggleBox = new HBox(gridButton, listButton);
        toggleBox.setSpacing(10);
        toggleBox.setPadding(new Insets(10));
        this.setTop(toggleBox);

        gridView = createGridView();
        listView = createListView();

        isGridView = true;
        this.setCenter(gridView);

        toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
            isGridView = newValue == gridButton;
            toggleLayout(isGridView);
        });

        this.getStylesheets().add(getClass().getResource("/css/gallery.css").toExternalForm());
    }

    private GridPane createGridView() {
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10));
        grid.setHgap(10);
        grid.setVgap(10);

        List<Build> builds = loadData();

        for (int i = 0; i < builds.size(); i++) {
            Build build = builds.get(i);

            VBox vbox = new VBox();
            ImageView imageView = new ImageView();
            try {
                imageView.setImage(new Image(build.getImageUrl()));
            } catch (Exception e) {
                imageView.setImage(new Image("path/to/placeholder.jpg"));
            }

            imageView.setFitWidth(150);
            imageView.setFitHeight(150);
            imageView.setPreserveRatio(true);

            vbox.getChildren().addAll(imageView, new Label(build.getType()));
            grid.add(vbox, i % 3, i / 3);
        }
        return grid;
    }

    private ListView<String> createListView() {
        ListView<String> list = new ListView<>();
        List<Build> builds = loadData();

        for (Build build : builds) {
            list.getItems().add(String.format(
                "Name: %s, Type: %s, Image: %s",
                build.getName(), build.getType(), build.getImageUrl()
            ));
        }
        return list;
    }

    private List<Build> loadData() {
        List<Build> builds = new ArrayList<>();
        builds.add(new Build("Build 1", "path/to/image1.jpg", "Kitset"));
        builds.add(new Build("Build 2", "path/to/image2.jpg", "MoC"));
        return builds;
    }

    private void toggleLayout(boolean isGrid) {
        this.setCenter(isGrid ? gridView : listView);
    }
}
