package cn.forevermc.launcher.view.pages;

import cn.forevermc.launcher.ForeverMC;
import cn.forevermc.launcher.bean.ComponentSize;
import cn.forevermc.launcher.bean.Game;
import cn.forevermc.launcher.util.Resources;
import cn.forevermc.launcher.view.components.AbstractPane;
import cn.forevermc.launcher.view.components.LoginTypeSelector;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.components.control.MCLabel;
import cn.forevermc.launcher.view.components.control.MCPasswordField;
import cn.forevermc.launcher.view.components.control.MCTextField;
import javafx.animation.FadeTransition;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.util.Duration;
import com.imyeyu.fx.icon.TimiFXIcon;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.BgFill;
import com.imyeyu.fx.utils.Row;

/**
 * 菜单面板
 *
 * @author 夜雨
 * @since 2022-11-21 19:33
 */
public abstract class MenuPane extends AbstractPane {

	protected final Canvas splashCanvas;
	protected final MCLabel launchGameLabel;
	protected final MCButton fmcAccount, fmcServer, launchGameSelect, launch, option, exit, langSelect, about;
	protected final ImageView title;
	protected final MCTextField name;
	protected final StringProperty splash;
	protected final MCPasswordField password;
	protected final LoginTypeSelector loginTypeSelector;

	private final StackPane root, transitionPane;

	protected boolean isFirstShow = true;

	public MenuPane() {
		splash = new SimpleStringProperty();

		// 过渡面板
		transitionPane = new StackPane(new ImageView(Resources.SPLASH_SCREEN));
		transitionPane.setBackground(new BgFill("#EF323D").build());

		// 标题
		title = new ImageView(Resources.TITLE);

		// 闪烁标语，使用 Canvas 减少动画导致抖动
		splashCanvas = new Canvas(1, 32);
		splashCanvas.setTranslateX(180);
		splashCanvas.setTranslateY(20);
		splashCanvas.setRotate(-20);

		// 账号管理
		fmcAccount = new MCButton();
		fmcAccount.getLabel().setEffect(null);
		fmcAccount.getLabel().setGraphic(new ImageView(Resources.TOTEM));
		fmcAccount.getLabel().setPadding(new Insets(1));

		// 游戏名
		name = new MCTextField() {{
			paddingProperty().unbind();
			paddingProperty().bind(Bindings.createObjectBinding(() -> new Insets(8, 28, 8, label.getWidth() + 8), label.widthProperty()));
		}};
		name.setLabel(TimiFXUI.MULTILINGUAL.text("name"));

		// 登录方式
		loginTypeSelector = new LoginTypeSelector();

		// 启动版本
		launchGameLabel = new MCLabel();
		launchGameLabel.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
		launchGameLabel.setPadding(new Insets(4));

		// 密码
		password = new MCPasswordField();
		password.setLabel(TimiFXUI.MULTILINGUAL.text("password"));
		password.disableProperty().bind(loginTypeSelector.valueProperty().isEqualTo(Game.Option.LoginType.OFFLINE));

		// 启动版本
		launchGameSelect = new MCButton();
		launchGameSelect.getLabel().setEffect(null);
		launchGameSelect.getLabel().setGraphic(TimiFXIcon.fromName("LIST", 32, Colorful.WHITE));
		launchGameSelect.getLabel().setPadding(Insets.EMPTY);
		launchGameSelect.getLabel().setPadding(new Insets(1, 2, 1, 2));

		// 服务器
		fmcServer = new MCButton();
		fmcServer.getLabel().setEffect(null);
		fmcServer.getLabel().setGraphic(new ImageView(Resources.COMMAND_BLOCK));
		fmcServer.getLabel().setPadding(new Insets(1));

		// 启动
		launch = new MCButton(TimiFXUI.MULTILINGUAL.text("launch"));
		launch.prefWidthProperty().bind(new SimpleDoubleProperty(ComponentSize.NORMAL).subtract(launchGameSelect.widthProperty()));

		// 语言
		langSelect = new MCButton();
		langSelect.getLabel().setGraphic(new ImageView(Resources.LANG));
		langSelect.getLabel().setPadding(new Insets(4));

		// 关于
		about = new MCButton();
		about.getLabel().setGraphic(new ImageView(Resources.INFO));
		about.getLabel().setPadding(new Insets(9, 15, 9, 15));

		// 设置
		option = new MCButton(TimiFXUI.MULTILINGUAL.text("option"));
		option.setPrefWidth(ComponentSize.NORMAL);

		// 退出
		exit = new MCButton(TimiFXUI.MULTILINGUAL.text("exit"));
		exit.setPrefWidth(ComponentSize.NORMAL);

		setCenter(root = new StackPane() {{
			getChildren().addAll(new BorderPane() {{
				setTop(new StackPane() {{
					setPadding(new Insets(64, 0, 0, 0));
					getChildren().addAll(title, splashCanvas);
				}});
				setCenter(new GridPane() {{
					// 纵向排版
					getRowConstraints().addAll(Row.build(), Row.build().top().height(48), Row.build());
					setHgap(7);
					setVgap(7);
					setAlignment(Pos.BOTTOM_CENTER);

					int col = 0;
					add(fmcAccount, col, 0);
					add(langSelect, col, 2);

					col++;
					addColumn(col, new StackPane() {{
						setMargin(loginTypeSelector, new Insets(0, 8, 0, 0));
						setAlignment(loginTypeSelector, Pos.CENTER_RIGHT);
						getChildren().addAll(name, loginTypeSelector);
					}}, password, option);

					col++;
					addColumn(col, new StackPane() {{
						setBackground(new BgFill("#000A").build());
						getChildren().setAll(launchGameLabel);
					}}, new BorderPane() {{
						setAlignment(launch, Pos.TOP_CENTER);
						setLeft(launchGameSelect);
						setCenter(launch);
					}}, exit);

					col++;
					add(fmcServer, col, 0);
					add(about, col, 2);
				}});
				setBottom(new MCLabel() {{
					setMargin(this, new Insets(16, 4, 2, 4));
					setText("ForeverMC " + ForeverMC.VERSION);
				}});
			}}, transitionPane);
		}});
	}

	@Override
	public void onShow() {
		if (isFirstShow) {
			FadeTransition welcomeFT = new FadeTransition(Duration.millis(650));
			welcomeFT.setNode(transitionPane);
			welcomeFT.setFromValue(1);
			welcomeFT.setToValue(0);
			welcomeFT.setDelay(Duration.millis(2000));
			welcomeFT.setOnFinished(e -> {
				root.getChildren().remove(transitionPane);
				isFirstShow = false;
			});
			welcomeFT.play();
		}
	}
}
