package cn.forevermc.launcher.view.components;

import cn.forevermc.launcher.ForeverMC;
import cn.forevermc.launcher.bean.LaunchGame;
import cn.forevermc.launcher.view.components.control.MCLabel;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import com.imyeyu.fx.utils.BgFill;
import com.imyeyu.fx.utils.BorderStroke;
import com.imyeyu.inject.TimiInject;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.StaticInject;

/**
 * 内容面板，具有标题和底部操作面板
 *
 * @author 夜雨
 * @since 2024-07-14 17:38
 */
@StaticInject
public abstract class AbstractContentPane extends AbstractPane {

	@Inject
	private static LaunchGame launchGame;

	protected final GridPane bottom;
	protected final StackPane content;

	private final Label title;
	private final Region shadowTop, shadowBottom;
	private final StackPane strokeWhitePane;

	public AbstractContentPane() {
		title = new MCLabel();
		title.setPadding(new Insets(24, 0, 16, 0));

		// 黑边
		StackPane strokeBlackPane = new StackPane();
		strokeBlackPane.setBorder(new BorderStroke(Color.valueOf("#000A")).width(2, 0, 2, 0).build());
		{
			// 白边
			strokeWhitePane = new StackPane();
			strokeWhitePane.setBackground(new BgFill("#0007").build());
			{
				// 旧 UI 阴影
				shadowTop = new Region();
				shadowBottom = new Region();
				{
					shadowTop.setMaxHeight(8);
					shadowTop.setBackground(new BgFill("#0006", "#0000").toBottom().build());
					shadowTop.setPickOnBounds(false);
					shadowTop.setFocusTraversable(true);
					shadowTop.setMouseTransparent(true);
					shadowBottom.setMaxHeight(8);
					shadowBottom.setBackground(new BgFill("#0000", "#0006").toBottom().build());
					shadowBottom.setPickOnBounds(false);
					shadowBottom.setFocusTraversable(true);
					shadowBottom.setMouseTransparent(true);
				}

				// 内容面板
				content = new StackPane();
				StackPane.setAlignment(shadowTop, Pos.TOP_CENTER);
				StackPane.setAlignment(shadowBottom, Pos.BOTTOM_CENTER);
				strokeWhitePane.getChildren().addAll(content, shadowTop, shadowBottom);
			}
			strokeBlackPane.getChildren().addAll(strokeWhitePane);
		}

		bottom = new GridPane();
		bottom.setHgap(8);
		bottom.setVgap(8);
		bottom.setAlignment(Pos.TOP_CENTER);
		bottom.setPadding(new Insets(12));

		setAlignment(title, Pos.CENTER);
		setTop(title);
		setCenter(strokeBlackPane);
		setBottom(bottom);

		ForeverMC.getInjectApp().addAfterInjectListener(timiInject -> {
			strokeWhitePane.borderProperty().bind(Bindings.createObjectBinding(() -> {
				if (launchGame.isNewUI()) {
					return new BorderStroke(Color.valueOf("#FFF4")).width(2, 0, 2, 0).build();
				}
				return new BorderStroke(Color.valueOf("#000C")).width(2, 0, 2, 0).build();
			}, launchGame));
			shadowTop.visibleProperty().bind(launchGame.newUIProperty().not());
			shadowBottom.visibleProperty().bind(launchGame.newUIProperty().not());
		});
	}

	protected void setTitle(String title) {
		this.title.setText(title);
	}

	protected void setContentPane(Node node) {
		content.getChildren().setAll(node);
	}

	protected void addBottomPaneValue(Node child, int columnIndex, int rowIndex, int colSpan, int rowSpan) {
		bottom.add(child, columnIndex, rowIndex, colSpan, rowSpan);
	}

	protected void addBottomPaneValue(int rowIndex, Node... children) {
		bottom.addRow(rowIndex, children);
	}

	protected void setBottomPaneValue(Node... children) {
		bottom.getChildren().clear();
		bottom.addRow(0, children);
	}
}
