package com.imyeyu.fx.ui.components.popup;

import com.imyeyu.fx.ui.components.popup.tips.AbstractPopupTips;
import com.imyeyu.fx.ui.components.popup.tips.PopupTipsImage;
import com.imyeyu.fx.ui.components.popup.tips.PopupTipsLabel;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

/**
 * 弹窗提示
 * <p>示例：
 * <pre>
 *     PopupTipsService.installText(node, "文本提示");
 *     PopupTipsService.installImage(node, new Image("/tips.png")); // 图片提示
 *     PopupTipsService.install(node, new AbstractPopupTips&lt;&gt;(new Button("自定义组件提示")));
 * </pre>
 *
 * @author 夜雨
 * @since 2021-04-22 19:48
 */
public final class PopupTipsService extends AbstractPopupTipsService<StackPane> {

	private static PopupTipsService service; // 单例对象

	/** 主布局 */
	private BorderPane main;

	private PopupTipsService() {
		showOnRoot = node -> main.setCenter(node);
	}

	@Override
	protected StackPane createRoot() {
		DropShadow shadow = new DropShadow();
		shadow.setRadius(6);
		shadow.setOffsetX(0);
		shadow.setOffsetY(0);
		shadow.setSpread(.05);
		shadow.setColor(Color.valueOf("#3333"));

		main = new BorderPane();
		main.setEffect(shadow);

		StackPane root = new StackPane();
		root.setBackground(Background.EMPTY);
		root.getChildren().add(main);
		return root;
	}

	/**
	 * 获取单例对象
	 *
	 * @return 单例对象
	 */
	public static synchronized PopupTipsService getInstance() {
		if (service == null) {
			service = new PopupTipsService();
		}
		return service;
	}

	/**
	 * 为组件安装文本弹窗提示
	 *
	 * @param node 组件
	 * @param text 图片
	 * @return 标签弹出提示
	 */
	public static PopupTipsLabel installText(Node node, String text) {
		PopupTipsLabel tips = new PopupTipsLabel(text);
		installTips(node, tips);
		return tips;
	}

	/**
	 * 为组件安装绑定文本弹窗提示
	 *
	 * @param node     组件
	 * @param property 文本绑定属性
	 * @return 标签弹出提示
	 */
	public static PopupTipsLabel installBindingText(Node node, StringProperty property) {
		PopupTipsLabel tips = new PopupTipsLabel();
		tips.getNode().textProperty().bind(property);
		tips.enableProperty().bind(property.isNotEmpty());
		installTips(node, tips);
		return tips;
	}

	/**
	 * 为组件安装绑定文本弹窗提示
	 *
	 * @param node    组件
	 * @param binding 文本绑定属性
	 * @return 标签弹出提示
	 */
	public static PopupTipsLabel installBindingText(Node node, StringBinding binding) {
		PopupTipsLabel tips = new PopupTipsLabel();
		tips.getNode().textProperty().bind(binding);
		tips.enableProperty().bind(binding.isNotEmpty());
		installTips(node, tips);
		return tips;
	}

	/**
	 * 为组件安装绑定文本弹窗提示
	 *
	 * @param node      组件
	 * @param when      条件
	 * @param then      条件 true 时显示文本
	 * @param otherwise 条件 false 时显示文本
	 * @return 标签弹出提示
	 */
	public static PopupTipsLabel installBindingText(Node node, BooleanProperty when, String then, String otherwise) {
		PopupTipsLabel tips = new PopupTipsLabel();
		tips.getNode().textProperty().bind(Bindings.when(when).then(then).otherwise(otherwise));
		installTips(node, tips);
		return tips;
	}

	/**
	 * 为组件安装图片弹窗提示
	 *
	 * @param node  组件
	 * @param image 图片
	 * @return 图片弹出提示
	 */
	public static PopupTipsImage installImage(Node node, Image image) {
		PopupTipsImage tips = new PopupTipsImage(image);
		installTips(node, tips);
		return tips;
	}

	/**
	 * 为组件安装通用提示
	 *
	 * @param node 组件
	 * @param tips 提示
	 */
	public static void installTips(Node node, AbstractPopupTips<?> tips) {
		getInstance().install(node, tips);
	}
}