package cn.forevermc.launcher.bean;

import cn.forevermc.launcher.service.FMCLoginService;
import cn.forevermc.launcher.service.GameService;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.SimpleObjectProperty;
import com.imyeyu.inject.annotation.Bean;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.InvokeForInjected;

/**
 * 启动游戏对象
 *
 * @author 夜雨
 * @since 2024-07-20 19:24
 */
@Bean
public class LaunchGame extends SimpleObjectProperty<Game> {

	@Inject
	private Config config;

	@Inject
	private GameService gameService;

	@Inject
	private FMCLoginService fmcLoginService;

	/** true 为使用新标题 */
	private final BooleanBinding newTitle;

	/** true 为使用新 UI */
	private final BooleanBinding newUI;

	/** true 为远古版本 */
	private final BooleanBinding legacy;

	/** true 为官方原版 */
	private final BooleanBinding original;

	/** true 为使用 Fabric */
	private final BooleanBinding fabric;

	/** true 为支持 ForeverMC 登录 */
	private final BooleanBinding supportFMCLogin;

	public LaunchGame() {
		newTitle = Bindings.createBooleanBinding(() -> {
			if (get() == null) {
				return false;
			}
			Game.MetaData.RealVersion realVersion = gameService.getRealVersion(get());
			return 1 <= realVersion.getRoot() && 20 <= realVersion.getMain(); // 1.20 <= 使用新标题
		}, this);
		newUI = Bindings.createBooleanBinding(() -> {
			if (get() == null) {
				return false;
			}
			Game.MetaData.RealVersion realVersion = gameService.getRealVersion(get());
			return 1 <= realVersion.getRoot() && 21 <= realVersion.getMain(); // 1.21 <= 使用新 UI
		}, this);
		legacy = Bindings.createBooleanBinding(() -> {
			if (get() == null) {
				return false;
			}
			return get().isLegacy();
		}, this);
		original = Bindings.createBooleanBinding(() -> {
			if (get() == null) {
				return false;
			}
			return get().isOriginal();
		}, this);
		fabric = Bindings.createBooleanBinding(() -> {
			if (get() == null) {
				return false;
			}
			return get().isFabric();
		}, this);
		supportFMCLogin = Bindings.createBooleanBinding(() -> {
			if (fmcLoginService == null) {
				return false;
			}
			return fmcLoginService.isSupportFMCLogin(get());
		}, this, FMCLoginService.loginModMapProperty(), FMCLoginService.fabricAPIMapProperty());
	}

	@InvokeForInjected
	private void injected() {
		String defLaunch = config.getLauncher().getGame().get();
		config.getLauncher().getGame().bind(Bindings.createStringBinding(() -> {
			if (get() == null) {
				return defLaunch;
			}
			return get().getName();
		}, this));
	}

	public boolean isOffline() {
		return get().getOption().getLoginType().get() == Game.Option.LoginType.OFFLINE;
	}

	public boolean isOnline() {
		return !isOffline();
	}

	public boolean isFMCLogin() {
		return get().getOption().getLoginType().get() == Game.Option.LoginType.FOREVER_MC;
	}

	public boolean isMojangLogin() {
		return get().getOption().getLoginType().get() == Game.Option.LoginType.MOJANG;
	}

	public boolean isNewTitle() {
		return newTitle.get();
	}

	public BooleanBinding newTitleProperty() {
		return newTitle;
	}

	public boolean isNewUI() {
		return newUI.get();
	}

	public BooleanBinding newUIProperty() {
		return newUI;
	}

	public boolean isLegacy() {
		return legacy.get();
	}

	public BooleanBinding legacyProperty() {
		return legacy;
	}

	public boolean isOriginal() {
		return original.get();
	}

	public BooleanBinding originalProperty() {
		return original;
	}

	public boolean isFabric() {
		return fabric.get();
	}

	public BooleanBinding fabricProperty() {
		return fabric;
	}

	public boolean isSupportFMCLogin() {
		return supportFMCLogin.get();
	}

	public boolean isNotSupportFMCLogin() {
		return !supportFMCLogin.get();
	}

	public BooleanBinding supportFMCLoginProperty() {
		return supportFMCLogin;
	}
}
