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

import com.imyeyu.fx.draggable.DraggableWindow;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.TextAreaEditor;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.fx.utils.BorderBuilder;
import com.imyeyu.inject.annotation.Component;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;

/**
 * @author 夜雨
 * @since 2023-04-13 17:39
 */
@Component
public class DraggableWindowDemo extends AbstractDemoPane {

	private final Popup popup;

	public DraggableWindowDemo() {
		title.setText(SidebarItem.DRAGGABLE_NODE.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/util/DraggableWindow.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/util/DraggableWindow.java");
		tips.setText(TimiFXUI.MULTILINGUAL.text("fx.example.draggable_window.tips"));

		popup = new Popup();
		popup.getContent().add(new BorderPane() {{
			setBorder(BorderBuilder.solid(Colorful.RED).width(2).build());
			setBackground(BG.WHITE);
			setTop(new StackPane() {{
				setBorder(Stroke.BOTTOM);
				setPadding(new Insets(4, 0, 4, 0));
				setBackground(BG.TITLE_FILL);
				getChildren().add(new Label(TimiFXUI.MULTILINGUAL.text("fx.example.drag_me")));

				new DraggableWindow(popup, this);
			}});
			setBottom(TimiFXUI.label("TimiFX"));
			setMargin(getBottom(), new Insets(12, 60, 12, 60));
		}});

		Button toggle = new Button();
		toggle.textProperty().bind(Bindings.when(popup.showingProperty()).then(TimiFXUI.MULTILINGUAL.text("close")).otherwise(TimiFXUI.MULTILINGUAL.text("show")));

		TextAreaEditor demo = getTextAreaEditor();

		setCenter(new BorderPane() {{
			setPadding(new Insets(20));
			setTop(new VBox() {{
				setPadding(new Insets(0, 0, 12, 0));
				getChildren().add(toggle);
			}});
			setCenter(demo);
		}});

		// ---------- 事件 ----------

		toggle.setOnAction(e -> {
			if (popup.isShowing()) {
				popup.hide();
			} else {
				popup.show(getScene().getWindow());
				popup.centerOnScreen();
			}
		});
	}

	private static TextAreaEditor getTextAreaEditor() {
		TextAreaEditor demo = new TextAreaEditor();
		demo.setEditable(false);
		demo.setText("""
Popup popup = new Popup();
popup.getContent().add(new BorderPane() {{
	setBorder(new XBorder(RED).width(2).build());
	setBackground(BG_WHITE);
	setTop(new StackPane() {{
		setBorder(BORDER_BOTTOM);
		setPadding(new Insets(4, 0, 4, 0));
		setBackground(BG_TITLE_FILL);
		getChildren().add(new Label("Drag Me"));

		new DraggableWindow(popup, this);
	}});
	setBottom(TimiFXUI.label("TimiFX"));
	setMargin(getBottom(), new Insets(12, 60, 12, 60));
}});
				""".trim());
		return demo;
	}

	@Override
	protected void onHide() {
		popup.hide();
	}
}
