package cn.forevermc.launcher.ctrl.pane;

import cn.forevermc.launcher.bean.Page;
import cn.forevermc.launcher.service.PageService;
import cn.forevermc.launcher.view.components.control.MCButton;
import cn.forevermc.launcher.view.pages.DialogPane;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import com.imyeyu.inject.annotation.Controller;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.SuperInject;
import com.imyeyu.java.bean.Callback;

/**
 * 会话，按钮显示属性绑定事件，存在则显示
 *
 * @author 夜雨
 * @since 2022-11-28 17:34
 */
@Controller
@SuperInject
public class Dialog extends DialogPane {

	@Inject
	private PageService pageService;

	private final ObjectProperty<Callback> confirmEvent, denyEvent, cancelEvent, closeEvent;

	public Dialog() {
		confirmEvent = new SimpleObjectProperty<>();
		denyEvent = new SimpleObjectProperty<>();
		cancelEvent = new SimpleObjectProperty<>();
		closeEvent = new SimpleObjectProperty<>();

		confirm.visibleProperty().bind(confirmEvent.isNotNull());
		deny.visibleProperty().bind(denyEvent.isNotNull());
		cancel.visibleProperty().bind(cancelEvent.isNotNull());
		close.visibleProperty().bind(closeEvent.isNotNull());

		confirm.setOnAction(e -> confirmEvent.get().handler());
		deny.setOnAction(e -> denyEvent.get().handler());
		cancel.setOnAction(e -> cancelEvent.get().handler());
		close.setOnAction(e -> closeEvent.get().handler());
	}

	/** 显示会话 */
	public void eject() {
		pageService.to(Page.DIALOG);
	}

	/**
	 * 显示会话
	 *
	 * @param content 内容
	 */
	public void show(String content) {
		show("", content);
	}

	/**
	 * 显示会话
	 *
	 * @param title   标题
	 * @param content 内容
	 */
	public void show(String title, String content) {
		setTitle(title);
		setContent(content);
		setCloseEvent(() -> pageService.back());
		eject();
	}

	@Override
	public void onHide() {
		title.setText("");
		content.setText("");
		confirmEvent.set(null);
		denyEvent.set(null);
		cancelEvent.set(null);
		closeEvent.set(null);
		buttonPane.getChildren().setAll(confirm, deny, cancel, close);
	}

	/** @param title 设置标题 */
	public void setTitle(String title) {
		super.title.setText(title);
	}

	/** @param content 设置内容 */
	public void setContent(String content) {
		super.content.setText(content);
	}

	/** @param buttons 设置按钮 */
	public void setButtons(MCButton... buttons) {
		buttonPane.getChildren().setAll(buttons);
	}

	/** 清除按钮 */
	public void clearButtons() {
		buttonPane.getChildren().clear();
	}

	/** @param confirmEvent 确认事件 */
	public void setConfirmEvent(Callback confirmEvent) {
		this.confirmEvent.set(confirmEvent);
	}

	/** @param denyEvent 拒绝事件 */
	public void setDenyEvent(Callback denyEvent) {
		this.denyEvent.set(denyEvent);
	}

	/** @param cancelEvent 取消事件 */
	public void setCancelEvent(Callback cancelEvent) {
		this.cancelEvent.set(cancelEvent);
	}

	/** @param closeEvent 关闭事件 */
	public void setCloseEvent(Callback closeEvent) {
		this.closeEvent.set(closeEvent);
	}

	/** @return 自定义组件 */
	public Node getGraphic() {
		return graphic.get();
	}

	/** @return 自定义组件属性 */
	public ObjectProperty<Node> graphicProperty() {
		return graphic;
	}

	/** @param graphic 设置自定义组件 */
	public void setGraphic(Node graphic) {
		this.graphic.set(graphic);
	}
}