package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.bean.Game;
import cn.forevermc.launcher.view.components.AbstractContentPane;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.components.control.MCSelector;
import cn.forevermc.launcher.view.components.control.MCTextField;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import com.imyeyu.fx.ui.TimiFXUI;

/**
 * 版本配置面板
 *
 * @author 夜雨
 * @since 2022-12-10 00:49
 */
public abstract class GameOptionPane extends AbstractContentPane {

	protected final MCButton installFabric, javaSelect, finish;
	protected final MCTextField java, server;
	protected final MCSelector<Boolean> autoConnect;
	protected final MCSelector<Game.Option.LoginType> loginType;

	public GameOptionPane() {
		// Java
		java = new MCTextField();
		java.setLabel("Java: ");
		java.setFocusTraversable(false);
		javaSelect = new MCButton(TimiFXUI.MULTILINGUAL.text("fmc.launcher.option.java.select"));

		// 安装 Fabric
		installFabric = new MCButton(ComponentSize.MEDIUM, TimiFXUI.MULTILINGUAL.text("fmc.launcher.pack_option.fabric.install"));

		// 自动连接
		autoConnect = new MCSelector<>();
		autoConnect.setPrefWidth(ComponentSize.MEDIUM);
		autoConnect.getItems().addAll(true, false);
		autoConnect.setStringConverter(isSelected -> {
			if (isSelected) {
				return TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.auto_connect", TimiFXUI.MULTILINGUAL.text("turn.on"));
			}
			return TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.auto_connect", TimiFXUI.MULTILINGUAL.text("turn.off"));
		});

		// 服务器地址
		server = new MCTextField();
		server.setPrefWidth(ComponentSize.MEDIUM);
		server.setLabel(TimiFXUI.MULTILINGUAL.text("fmc.launcher.option.server"));
		server.disableProperty().bind(autoConnect.valueProperty().isEqualTo(false));

		// 登录方式
		loginType = new MCSelector<>();
		loginType.setPrefWidth(ComponentSize.MEDIUM);
		loginType.setStringConverter(type -> {
			if (type == null) {
				return "";
			}
			return TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.pack_option.login_type", type.getName());
		});
		loginType.getItems().setAll(Game.Option.LoginType.values());

		// 完成
		finish = new MCButton(TimiFXUI.MULTILINGUAL.text("finish"));
		finish.setPrefWidth(ComponentSize.LARGE);

		setContentPane(new GridPane() {{
			setHgap(24);
			setVgap(12);
			setAlignment(Pos.TOP_CENTER);

			int row = 0;
			add(new BorderPane() {{
				setMargin(java, new Insets(0, 10, 0, 0));
				setCenter(java);
				setRight(javaSelect);
			}}, 0, row++, 2, 1);
			addRow(row++, installFabric, loginType);
			addRow(row, autoConnect, server);
		}});
		setBottomPaneValue(finish);
	}
}
