package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.service.FMCLoginService;
import cn.forevermc.launcher.view.components.AbstractListPane;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.components.control.MCLabel;
import javafx.scene.control.ListCell;
import javafx.scene.layout.VBox;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.utils.Time;

/**
 * @author 夜雨
 * @since 2024-05-21 19:40
 */
public abstract class PlayerSelectPane extends AbstractListPane<FMCLoginService.MinecraftPlayer> {

	protected final MCButton confirm, cancel;

	public PlayerSelectPane() {
		// 列表
		list.setCellFactory(cell -> new ListCell<>() {

			private final MCLabel name, lastLoginAt;
			private final VBox root;

			{
				name = new MCLabel();
				lastLoginAt = new MCLabel();
				lastLoginAt.getStyleClass().add("last-login-at");
				root = new VBox(4);
				root.getChildren().addAll(name, lastLoginAt);
			}

			@Override
			protected void updateItem(FMCLoginService.MinecraftPlayer item, boolean empty) {
				super.updateItem(item, empty);
				if (empty || item == null) {
					setGraphic(null);
				} else {
					name.setText(item.getName());
					if (item.getLastLoginAt() != null) {
						lastLoginAt.setText("最近登录: " + Time.toDateTime(item.getLastLoginAt()));
					}
					setGraphic(root);
				}
			}
		});
		list.getStyleClass().add("player");

		// 下载
		confirm = new MCButton(TimiFXUI.MULTILINGUAL.text("confirm"));
		confirm.setPrefWidth(ComponentSize.MEDIUM);
		confirm.disableProperty().bind(list.getSelectionModel().selectedItemProperty().isNull());

		// 返回
		cancel = new MCButton(TimiFXUI.MULTILINGUAL.text("cancel"));
		cancel.setPrefWidth(ComponentSize.MEDIUM);

		setTitle(TimiFXUI.MULTILINGUAL.text("fmc.launcher.player.select.title"));
		setBottomPaneValue(confirm, cancel);
	}
}
