package com.imyeyu.fx.ui.examples.view.pages;

import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.RadioButtonGroup;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.inject.annotation.Component;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 * 按钮组件示例
 *
 * @author 夜雨
 * @since 2026-06-25
 */
@Component
public class ButtonDemo extends AbstractDemoPane {

	public ButtonDemo() {
		title.setText(SidebarItem.BUTTON.getText());
		document.setText("暂无");
		document.setUrl("");
		source.setText("src/main/java/com/imyeyu/fx/ui/components/RadioButtonGroup.java");
		source.setUrl("");
		tips.setText("演示 RadioButtonGroup 的按钮模式和滑块模式，按钮列表、选中状态和允许取消选择都是组件对外暴露的标准属性");

		RadioButtonGroup buttonModeGroup = new RadioButtonGroup();
		buttonModeGroup.addButton("全部");
		buttonModeGroup.addButton("启用");
		buttonModeGroup.addButton("停用");
		buttonModeGroup.setSelectedIndex(0);

		RadioButtonGroup sliderModeGroup = new RadioButtonGroup();
		sliderModeGroup.setMode(RadioButtonGroup.Mode.SLIDER);
		sliderModeGroup.addButton("左对齐");
		sliderModeGroup.addButton("居中");
		sliderModeGroup.addButton("右对齐");
		sliderModeGroup.setSelectedIndex(1);

		RadioButtonGroup clearableGroup = new RadioButtonGroup();
		clearableGroup.setMode(RadioButtonGroup.Mode.SLIDER);
		clearableGroup.setAllowClearSelection(true);
		clearableGroup.addButton("日");
		clearableGroup.addButton("周");
		clearableGroup.addButton("月");
		clearableGroup.addButton("年");
		clearableGroup.setSelectedIndex(2);

		Label selectedButtonText = TimiFXUI.label();
		Label selectedIndexText = TimiFXUI.label();
		selectedButtonText.setText("当前选中: " + clearableGroup.getSelectedButton().getText());
		selectedIndexText.setText("当前下标: " + clearableGroup.getSelectedIndex());
		clearableGroup.selectedButtonProperty().addListener((obs, o, button) -> {
			if (button == null) {
				selectedButtonText.setText("当前选中: 无");
				return;
			}
			selectedButtonText.setText("当前选中: " + button.getText());
		});
		clearableGroup.selectedIndexProperty().addListener((obs, o, index) -> selectedIndexText.setText("当前下标: " + index.intValue()));

		RadioButtonGroup customButtonGroup = new RadioButtonGroup();
		ToggleButton primary = new ToggleButton("概览");
		ToggleButton analysis = new ToggleButton("分析");
		ToggleButton history = new ToggleButton("历史");
		customButtonGroup.getButtons().addAll(primary, analysis, history);
		customButtonGroup.setAlignment(Pos.CENTER_LEFT);
		customButtonGroup.setSelectedButton(analysis);

		setCenter(new VBox() {{
			setSpacing(12);
			setPadding(new Insets(20));
			getChildren().addAll(
				new GridPane() {{
					setHgap(12);
					setVgap(10);

					int row = 0;
					addRow(row++, TimiFXUI.label("按钮模式"), buttonModeGroup);
					addRow(row++, TimiFXUI.label("滑块模式"), sliderModeGroup);
					addRow(row++, TimiFXUI.label("允许取消"), new HBox(clearableGroup));
					addRow(row, TimiFXUI.label("外部按钮"), customButtonGroup);
				}},
				selectedButtonText,
				selectedIndexText
			);
		}});
	}
}
