package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.bean.Game;
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.geometry.Pos;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.utils.Time;

/**
 * 原版选择面板
 *
 * @author 夜雨
 * @since 2022-11-28 10:09
 */
public abstract class OriginalSelectPane extends AbstractListPane<Game> {

	protected final MCButton download, back;

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

			private final MCLabel version, date;
			private final BorderPane root;

			{
				version = new MCLabel();
				date = new MCLabel();
				root = new BorderPane();
				BorderPane.setAlignment(version, Pos.CENTER_LEFT);
				root.setCenter(version);
				root.setRight(date);
			}

			@Override
			protected void updateItem(Game item, boolean empty) {
				super.updateItem(item, empty);
				if (empty || item == null) {
					setGraphic(null);
				} else {
					version.setText(item.getId());
					date.setText(Time.toDateTime(item.getReleaseAt()));
					setGraphic(root);
				}
			}
		});

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

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

		setTitle(TimiFXUI.MULTILINGUAL.text("fmc.launcher.download.mojang.title"));
		setBottomPaneValue(download, back);
	}
}
