package com.imyeyu.fx.ui.examples.view.pages;

import com.imyeyu.fx.ui.MinecraftFont;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.alert.AlertConfirm;
import com.imyeyu.fx.ui.components.alert.AlertTips;
import com.imyeyu.fx.ui.examples.TimiFXExamples;
import com.imyeyu.fx.ui.examples.bean.Config;
import com.imyeyu.fx.ui.examples.component.AbstractPane;
import com.imyeyu.fx.ui.examples.component.TimiVersionLabel;
import com.imyeyu.fx.ui.examples.ctrl.Main;
import com.imyeyu.fx.ui.examples.util.Resources;
import com.imyeyu.fx.utils.FXApplication;
import com.imyeyu.fx.utils.grid.Column;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.inject.annotation.PostConstruct;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.Language;
import com.imyeyu.utils.OS;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;

/**
 * 欢迎页
 *
 * @author 夜雨
 * @since 2022-08-26 14:55
 */
@Component
public class Welcome extends AbstractPane implements OS.FileSystem {

	private final Config config;
	private final ComboBox<Language.Enum> language;

	public Welcome(Config config) {
		this.config = config;
		Label title = new Label(TimiFXUI.MULTILINGUAL.text("fx.example.title"), new ImageView(Resources.ICON_X64));
		title.setMaxWidth(Double.MAX_VALUE);
		title.setAlignment(Pos.CENTER);
		title.setBackground(BG.TITLE_FILL);
		title.setBorder(Stroke.BOTTOM);
		title.setPadding(new Insets(8, 0, 8, 0));
		MinecraftFont.css(title, MinecraftFont.X32);


		// 提示
		Label tips = new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.tips"));
		tips.setAlignment(Pos.CENTER);

		// 语言
		Label labelLang = new Label(TimiFXUI.MULTILINGUAL.text("lang"));
		labelLang.setTextFill(Colorful.GRAY);

		language = new ComboBox<>();
		language.getItems().addAll(Language.Enum.values());
		language.setConverter(new StringConverter<>() {

			@Override
			public String toString(Language.Enum language) {
				return language.toString();
			}

			@Override
			public Language.Enum fromString(String string) {
				return null;
			}
		});

		// 开发者
		Label develop = new Label(TimiFXUI.MULTILINGUAL.text("developer.arg"));
		develop.setAlignment(Pos.CENTER);
		Label copyright = new Label(TimiFXUI.MULTILINGUAL.text("copyright"));
		copyright.setAlignment(Pos.CENTER);
		Label versionTimiFX = new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.version.timifx"));
		TimiVersionLabel version = new TimiVersionLabel(TimiFXExamples.VERSION, TimiFXExamples.class.getSimpleName()) {{
			version.setGraphic(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.version")));
		}};

		setTop(title);
		setCenter(new VBox() {{
			setSpacing(20);
			setPadding(new Insets(20));
			setAlignment(Pos.TOP_CENTER);
			getChildren().addAll(tips, new VBox() {{
				setPadding(new Insets(40));
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.tips0")));
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.tips1")));
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.tips2")));
				getChildren().add(new GridPane() {{
					getColumnConstraints().addAll(Column.builder().right().build(), Column.value());
					setHgap(8);
					setVgap(6);
					setPadding(new Insets(12, 0, 4, 0));

					int row = 0;
					addRow(row++, new Label("timi-java"), TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.timijava")));
					addRow(row, new Label("timi-fx-icon"), TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.welcome.timifx_icon")));
				}});
			}});
		}});
		setBottom(new VBox() {{
			setSpacing(6);
			setPadding(new Insets(16));
			setAlignment(Pos.CENTER);
			getChildren().addAll(new HBox() {{
				setSpacing(6);
				setAlignment(Pos.CENTER);
				getChildren().addAll(TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("lang")), language);
			}});
			getChildren().addAll(versionTimiFX, version, develop, copyright);
		}});
	}

	@PostConstruct
	private void init() {
		language.valueProperty().bind(config.getLanguage());

		// 修改语言
		language.valueProperty().addListener((obs, o, newLang) -> {
			new AlertConfirm(TimiFXUI.MULTILINGUAL.text("tips.restart")) {

				@Override
				protected void onConfirm() {
					// JRE
					String jre = System.getProperty("java.home") + SEP + "bin" + SEP + "java";
					// 启动参数
					String param = " -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -jar ";
					// 启动 Jar
					String jar = IO.getJarAbsolutePath(getClass());
					// 重启
					try {
						FXApplication.restart(TimiFXExamples.getInject().getBean(Main.class), jre + param + jar);
					} catch (Exception ex) {
						AlertTips.error(this, TimiFXUI.MULTILINGUAL.text("tips.restart.error"));
					}
				}
			}.showRelativeCenter(getScene().getWindow());
		});
	}
}
