package cn.forevermc.launcher.ctrl.pane;

import cn.forevermc.launcher.bean.Config;
import cn.forevermc.launcher.bean.LaunchGame;
import cn.forevermc.launcher.util.Resources;
import cn.forevermc.launcher.view.pages.LaunchingPane;
import javafx.animation.FadeTransition;
import javafx.beans.binding.Bindings;
import javafx.scene.layout.Background;
import javafx.stage.Stage;
import javafx.util.Duration;
import lombok.extern.slf4j.Slf4j;
import com.imyeyu.fx.utils.BgFill;
import com.imyeyu.inject.annotation.Controller;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.InvokeForInjected;

/**
 * 启动过渡页
 *
 * @author 夜雨
 * @since 2022-11-23 17:32
 */
@Slf4j
@Controller
public class Launching extends LaunchingPane {

	/** 正常背景 */
	private static final Background BG_NORMAL = new BgFill("#EF323D").build();

	/** 远古版本背景 */
	private static final Background BG_ANCIENT = BG.WHITE;

	@Inject
	private Config config;

	@Inject
	private Stage stage;

	@Inject
	private LaunchGame launchGame;

	private final FadeTransition bgTransition, mojangTransition;

	public Launching() {
		// 背景过渡
		bgTransition = new FadeTransition(Duration.millis(500), root);
		bgTransition.setDelay(Duration.millis(500));
		bgTransition.setFromValue(0);
		bgTransition.setToValue(1);

		// LOGO 过渡
		mojangTransition = new FadeTransition(Duration.millis(800), mojang);
		mojangTransition.setDelay(Duration.millis(1000));
		mojangTransition.setFromValue(0);
		mojangTransition.setToValue(1);
	}

	@InvokeForInjected
	private void injected() {
		// Logo 图片
		mojang.imageProperty().bind(Bindings.createObjectBinding(() -> {
			if (launchGame.get() == null || launchGame.get().isLegacy()) {
				return Resources.MOJANG_ANCIENT;
			}
			return Resources.MOJANG;
		}, launchGame));
		// 背景
		root.backgroundProperty().bind(Bindings.createObjectBinding(() -> {
			if (mojang.getImage().equals(Resources.MOJANG)) {
				return BG_NORMAL;
			}
			return BG_ANCIENT;
		}, mojang.imageProperty()));
	}

	@Override
	public void onShow() {
		bgTransition.play();
		mojangTransition.play();
	}
}
