package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.bean.GameDownloadSource;
import cn.forevermc.launcher.bean.RuntimeDownloadSource;
import cn.forevermc.launcher.view.components.AbstractScrollPane;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.components.control.MCLabel;
import cn.forevermc.launcher.view.components.control.MCSelector;
import cn.forevermc.launcher.view.components.control.MCSlider;
import cn.forevermc.launcher.view.components.control.MCTextField;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.GridPane;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.Column;
import com.imyeyu.utils.OS;

/**
 * 选项面板
 *
 * @author 夜雨
 * @since 2022-11-24 22:27
 */
public abstract class OptionPane extends AbstractScrollPane {

	protected final MCButton launchSelect, finish;
	protected final MCSlider memory, bgmVolume, soundVolume, multiDownload;
	protected final MCTextField proxyHost, proxyPort;
	protected final MCSelector<Boolean> autoStartup;
	protected final MCSelector<GameDownloadSource> gameSource;
	protected final MCSelector<RuntimeDownloadSource> runtimeSource;

	public OptionPane() {
		// 内存
		memory = new MCSlider();
		memory.setMin(1024);
		memory.setMax(OS.getSystemMemorySize() / Math.pow(1024, 2));
		memory.setStep(512);
		memory.setPrefWidth(ComponentSize.MEDIUM);

		// 版本
		launchSelect = new MCButton(TimiFXUI.MULTILINGUAL.text("fmc.launcher.option.launch_pack"));
		launchSelect.setPrefWidth(ComponentSize.MEDIUM);

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

		// 背景音量
		bgmVolume = new MCSlider();
		bgmVolume.setPrefWidth(ComponentSize.MEDIUM);
		bgmVolume.getLabel().textProperty().bind(bgmVolume.valueProperty().asString(TimiFXUI.MULTILINGUAL.text("fmc.launcher.option.bgm")));

		// 音效音量
		soundVolume = new MCSlider();
		soundVolume.setPrefWidth(ComponentSize.MEDIUM);
		soundVolume.getLabel().textProperty().bind(soundVolume.valueProperty().asString(TimiFXUI.MULTILINGUAL.text("fmc.launcher.option.sound")));

		MCLabel network = new MCLabel(TimiFXUI.MULTILINGUAL.text("network"));

		// 游戏下载源
		gameSource = new MCSelector<>();
		gameSource.setPrefWidth(ComponentSize.MEDIUM);
		gameSource.setStringConverter(type -> {
			if (type == null) {
				return "";
			}
			return TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.game_source", type.toString());
		});
		gameSource.getItems().setAll(GameDownloadSource.values());

		// 运行时下载源
		runtimeSource = new MCSelector<>();
		runtimeSource.setPrefWidth(ComponentSize.MEDIUM);
		runtimeSource.setStringConverter(type -> {
			if (type == null) {
				return "";
			}
			return TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.runtime_source", type.getName());
		});
		runtimeSource.getItems().setAll(RuntimeDownloadSource.values());

		// 多线程下载
		multiDownload = new MCSlider();
		multiDownload.setMin(0);
		multiDownload.setMax(64);
		multiDownload.setStep(4);
		multiDownload.setPrefWidth(ComponentSize.MEDIUM);

		proxyHost = new MCTextField();
		proxyHost.setLabel(TimiFXUI.MULTILINGUAL.text("proxy.host"));

		proxyPort = new MCTextField();
		proxyPort.setLabel(TimiFXUI.MULTILINGUAL.text("proxy.port"));

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

		setTitle(TimiFXUI.MULTILINGUAL.text("option"));
		setScrollContent(new GridPane() {{
			setMargin(network, new Insets(16, 0, 0, 0));
			getColumnConstraints().addAll(Column.build(HPos.CENTER), Column.build(HPos.CENTER));
			setHgap(20);
			setVgap(12);
			setAlignment(Pos.TOP_CENTER);

			int row = 0;
			add(memory, 0, row++, 2, 1);
			addRow(row++, autoStartup, launchSelect);
			addRow(row++, soundVolume, bgmVolume);
			add(network, 0, row++, 2, 1);
			addRow(row++, runtimeSource, gameSource);
			addRow(row++, proxyHost, proxyPort);
			addRow(row, multiDownload);
		}});
		setBottomPaneValue(finish);
	}
}
