package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.bean.Game;
import cn.forevermc.launcher.bean.LaunchGame;
import cn.forevermc.launcher.util.Resources;
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.Insets;
import javafx.scene.control.ListCell;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.java.TimiJava;

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

	@Inject
	protected LaunchGame launchGame;

	protected final MCButton originalDL, packDL, select, destory, option, back;

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

			final MCLabel name;
			final MCLabel title;
			final ImageView activatedIcon;
			final BorderPane root;

			{
				activatedIcon = new ImageView(Resources.CHECK_SMALL);
				name = new MCLabel();
				title = new MCLabel();
				title.setTextFill(Colorful.LIGHT_GRAY);
				title.getStyleClass().clear();
				title.visibleProperty().bind(title.textProperty().isNotEmpty());
				title.managedProperty().bind(title.visibleProperty());
				root = new BorderPane();
				root.setLeft(activatedIcon);
				root.setCenter(new HBox() {{
					setPadding(new Insets(0, 0, 0, 4));
					getChildren().addAll(name, title);
				}});
			}

			@Override
			protected void updateItem(Game item, boolean empty) {
				super.updateItem(item, empty);
				if (empty || item == null) {
					setGraphic(null);
				} else {
					if (activatedIcon.visibleProperty().isBound()) {
						activatedIcon.visibleProperty().unbind();
					}
					activatedIcon.visibleProperty().bind(itemProperty().isEqualTo(launchGame));
					name.setText(item.getName());
					if (TimiJava.isEmpty(item.getOption().getName().get())) {
						title.setText("");
					} else {
						title.setText(" - " + item.getOption().getName().get());
					}
					setGraphic(root);
				}
			}
		});

		// 原版下载
		originalDL = new MCButton(ComponentSize.MEDIUM + 10, TimiFXUI.MULTILINGUAL.text("fmc.launcher.launch_select.download.mojang"));

		// 整合下载
		packDL = new MCButton(ComponentSize.MEDIUM + 10, TimiFXUI.MULTILINGUAL.text("fmc.launcher.launch_select.download.fmc"));

		// 选择
		select = new MCButton(ComponentSize.SMALL, TimiFXUI.MULTILINGUAL.text("select"));
		select.disableProperty().bind(loadingProperty().or(list.getSelectionModel().selectedItemProperty().isNull()));

		// 销毁
		destory = new MCButton(ComponentSize.SMALL, TimiFXUI.MULTILINGUAL.text("delete"));
		destory.disableProperty().bind(loadingProperty().or(list.getSelectionModel().selectedItemProperty().isNull()));

		// 设置
		option = new MCButton(ComponentSize.SMALL, TimiFXUI.MULTILINGUAL.text("fmc.launcher.launch_select.option"));
		option.disableProperty().bind(loadingProperty().or(list.getSelectionModel().selectedItemProperty().isNull()));

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

		setTitle(TimiFXUI.MULTILINGUAL.text("fmc.launcher.launch_select"));
		addBottomPaneValue(originalDL, 0, 0, 2, 1);
		addBottomPaneValue(packDL, 2, 0, 2, 1);
		addBottomPaneValue(1, destory, option, select, back);
	}
}
