package cn.forevermc.launcher.ctrl;

import cn.forevermc.launcher.ForeverMC;
import cn.forevermc.launcher.bean.APISetting;
import cn.forevermc.launcher.bean.Config;
import cn.forevermc.launcher.bean.LaunchGame;
import cn.forevermc.launcher.bean.Page;
import cn.forevermc.launcher.service.GameService;
import cn.forevermc.launcher.service.PageService;
import cn.forevermc.launcher.util.Sound;
import cn.forevermc.launcher.view.ViewMain;
import com.google.gson.Gson;
import javafx.beans.property.ObjectProperty;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import com.imyeyu.inject.TimiInject;
import com.imyeyu.inject.annotation.IOCReturn;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.SuperInject;
import com.imyeyu.io.IOSpeedService;

import java.awt.SplashScreen;

/**
 * 主控
 *
 * @author 夜雨
 * @since 2022-11-18 15:37
 */
@Slf4j
@SuperInject
public class Main extends ViewMain {

	@Inject
	private Config config;

	@Inject
	private Gson gson;

	@Inject
	private APISetting apiSetting;

	@Inject
	private PageService pageService;

	@Inject
	private GameService gameService;

	@Inject
	private LaunchGame launchGame;

	@Inject
	private ObjectProperty<MediaPlayer> bgmPlayer;

	private Stage stage;

	@Override
	public void start(Stage stage) {
		this.stage = stage;


		TimiInject.run(ForeverMC.getInjectApp()).ioc(this);

		pageService.to(Page.MENU);
		super.start(stage);

		stage.show();

		if (SplashScreen.getSplashScreen() != null) {
			SplashScreen.getSplashScreen().close();
		}

		// 音效音量
		Sound.CLICK.volumeProperty().bind(config.getMain().getSoundVolume().divide(100));
	}

	@Override
	public void stop() {
		log.info("doing shutdown..");
		stage.close();
		if (launchGame.get() != null) {
			gameService.storeOption(launchGame.get());
		}
		ForeverMC.getConfigLoader().dump();
		IOSpeedService.getInstance().shutdown();
	}

	/** 主窗体 */
	@IOCReturn
	public Stage stage() {
		return stage;
	}
}
