package view;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import model.DataGoods;
import model.Goods;

import java.util.HashMap;
import java.util.Map;

public class CategoryManagementView extends AnchorPane {

    private TreeView<String> categoryTreeView;
    private BorderPane contentArea;
    private Map<String, Goods> goodsMap; // 用于快速查找商品

    public CategoryManagementView(BorderPane contentArea) {
        this.contentArea = contentArea;
        initializeComponents();
        loadCategoriesAndGoods();
    }

    private void initializeComponents() {
        categoryTreeView = new TreeView<>();
        categoryTreeView.setShowRoot(false); // 隐藏根节点
        AnchorPane.setTopAnchor(categoryTreeView, 0.0);
        AnchorPane.setBottomAnchor(categoryTreeView, 0.0);
        AnchorPane.setLeftAnchor(categoryTreeView, 0.0);
        AnchorPane.setRightAnchor(categoryTreeView, 0.0);
        this.getChildren().add(categoryTreeView);
    }

    private void loadCategoriesAndGoods() {
        DataGoods dataGoods = DataGoods.getInstance();
        ObservableList<Goods> allGoods = FXCollections.observableArrayList(dataGoods.getAllGoods());
        ObservableList<String> categories = FXCollections.observableArrayList(dataGoods.getAllCategories());

        // 构建树结构
        TreeItem<String> root = new TreeItem<>("Root"); // 设置根节点

        // 使用Map来存储类别节点
        Map<String, TreeItem<String>> categoryItems = new HashMap<>();
        goodsMap = new HashMap<>(); // 初始化商品映射

        // 首先创建所有类别节点
        for (String category : categories) {
            TreeItem<String> categoryItem = new TreeItem<>(category);
            categoryItems.put(category, categoryItem);
            root.getChildren().add(categoryItem);
        }

        // 然后将商品添加到对应的类别下
        for (Goods goods : allGoods) {
            String category = goods.getCategory();
            TreeItem<String> categoryItem = categoryItems.get(category);
            
            if (categoryItem != null) {
                // 构建显示文本
                String displayText = goods.getName() + " (" + goods.getSpecification() + ")";
                TreeItem<String> goodsItem = new TreeItem<>(displayText);
                categoryItem.getChildren().add(goodsItem);
                
                // 将商品存储到映射中以便快速查找
                goodsMap.put(displayText, goods);
            }
        }

        categoryTreeView.setRoot(root);

        // 展开所有类别节点
        for (TreeItem<String> categoryItem : root.getChildren()) {
            categoryItem.setExpanded(true);
        }
    }

    private void showGoodsDetails(Goods goods) {
        // 创建一个 VBox 来显示商品详细信息
        VBox detailsBox = new VBox(10);
        detailsBox.setPadding(new Insets(10));

        Label nameLabel = new Label("名称: " + goods.getName());
        Label specificationLabel = new Label("规格: " + goods.getSpecification());
        Label categoryLabel = new Label("类别: " + goods.getCategory());
        Label quantityLabel = new Label("数量: " + DataGoods.getInstance().getStock(goods.getId()));
        Label supplierLabel = new Label("供应商: " + goods.getSupplier());
        Label remarkLabel = new Label("备注: " + goods.getRemark());

        detailsBox.getChildren().addAll(
            nameLabel, 
            specificationLabel, 
            categoryLabel, 
            quantityLabel,
            supplierLabel,
            remarkLabel
        );

        // 添加返回按钮
        Button backButton = new Button("返回");
        backButton.setOnAction(event -> {
            // 清空 contentArea 并重新加载分类视图
            contentArea.setCenter(categoryTreeView); // 重新显示分类视图
        });

        detailsBox.getChildren().add(backButton); // 将返回按钮添加到最后

        // 将详细信息显示在 contentArea 中
        contentArea.setCenter(detailsBox);
    }
}
