package cn.forevermc.launcher.ctrl.pane;

import cn.forevermc.launcher.bean.Config;
import cn.forevermc.launcher.bean.Page;
import cn.forevermc.launcher.service.MCPopupTipsService;
import cn.forevermc.launcher.service.PageService;
import cn.forevermc.launcher.view.pages.OptionPane;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.inject.annotation.Controller;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.InvokeForInjected;
import com.imyeyu.inject.annotation.SuperInject;

/**
 * 选项
 *
 * @author 夜雨
 * @since 2022-11-24 22:27
 */
@Controller
@SuperInject
public class Option extends OptionPane {

	@Inject
	private Config config;

	@Inject
	private PageService pageService;

	@Inject
	private MCPopupTipsService mcPopupTipsService;

	public Option() {
		// 内存
		final double maxMem60p = (memory.getMax() / 1024 * .6) * 1024; // 总内存 60%
		final double maxMem80p = (memory.getMax() / 1024 * .8) * 1024; // 总内存 80%
		memory.valueProperty().addListener((obs, o, newMemory) -> {
			final int mb = newMemory.intValue();
			if (mb < 2048 || (maxMem60p < mb && mb <= maxMem80p)) {
				memory.getLabel().setTextFill(Colorful.ORANGE);
			} else {
				if (maxMem80p < mb) {
					memory.getLabel().setTextFill(Colorful.RED);
				} else {
					memory.getLabel().setTextFill(Colorful.GREEN);
				}
			}
			memory.getLabel().setText(TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.memory", mb));
		});

		// 多线程下载
		multiDownload.valueProperty().addListener((obs, o, size) -> {
			if (size.intValue() < 4) {
				multiDownload.getLabel().setText(TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.multi_download", 1));
			} else {
				multiDownload.getLabel().setText(TimiFXUI.MULTILINGUAL.textArgs("fmc.launcher.option.multi_download", size.intValue()));
			}
		});

		// 启动版本
		launchSelect.setOnAction(e -> pageService.to(Page.GAME_SELECT));

		// 完成
		finish.setOnAction(e -> pageService.to(Page.MENU));
	}

	@InvokeForInjected
	private void config() {
		memory.valueProperty().bindBidirectional(config.getLauncher().getMemory());
		gameSource.valueProperty().bindBidirectional(config.getMain().getGameDownloadSource());
		multiDownload.valueProperty().bindBidirectional(config.getLauncher().getMultiDownload());
		runtimeSource.valueProperty().bindBidirectional(config.getMain().getRuntimeDownloadSource());
		autoStartup.valueProperty().bindBidirectional(config.getLauncher().getAutoStartup());
		bgmVolume.valueProperty().bindBidirectional(config.getMain().getBgmVolume());
		soundVolume.valueProperty().bindBidirectional(config.getMain().getSoundVolume());
	}

	@Override
	public void onShow() {
		finish.requestFocus();
	}
}
