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

import com.imyeyu.java.bean.CallbackArg;
import javafx.scene.control.TextField;
import javafx.stage.Window;

/**
 * 输入弹窗
 *
 * @author 夜雨
 * @since 2022-01-07 15:43
 */
public class AlertTextField extends AbstractAlertInput<TextField> {

	/**
	 * 输入弹窗
	 *
	 * @param type    弹窗类型
	 */
	public AlertTextField(AlertType type) {
		this(type, "", AlertButton.confirm(), AlertButton.cancel());
	}

	/**
	 * 输入弹窗
	 *
	 * @param content 提示
	 */
	public AlertTextField(String content) {
		this(AlertType.INFORMATION, AlertType.INFORMATION.getTitle(), content, "", AlertButton.confirm(), AlertButton.cancel());
	}

	/**
	 * 输入弹窗
	 *
	 * @param content 提示
	 * @param buttons    可控按钮
	 */
	public AlertTextField(String content, AlertButton... buttons) {
		this(AlertType.INFORMATION, AlertType.INFORMATION.getTitle(), content, "", buttons);
	}

	/**
	 * 输入弹窗
	 *
	 * @param type    弹窗类型
	 * @param content 内容
	 * @param buttons    可控按钮
	 */
	public AlertTextField(AlertType type, String content, AlertButton... buttons) {
		super(new TextField(), type, type.getTitle(), content, "", buttons);
	}

	/**
	 * 输入弹窗
	 *
	 * @param type    弹窗类型
	 * @param title   标题
	 * @param content 内容
	 * @param text    预设输入框文本
	 * @param buttons    可控按钮
	 */
	public AlertTextField(AlertType type, String title, String content, String text, AlertButton... buttons) {
		super(new TextField(), type, title, content, text, buttons);
	}

	/**
	 * 输入确认弹窗
	 *
	 * @param owner    依赖窗体
	 * @param content  内容
	 * @param confirm  确认事件
	 * @return 弹窗对象
	 */
	public static AlertTextField confirm(Window owner, String content, CallbackArg<String> confirm) {
		return confirm(owner, content, confirm, null);
	}

	/**
	 * 输入确认弹窗
	 *
	 * @param owner     依赖窗体
	 * @param content   内容
	 * @param confirm   确认事件
	 * @param otherwise 其他事件
	 * @return 弹窗对象
	 */
	public static AlertTextField confirm(Window owner, String content, CallbackArg<String> confirm, CallbackArg<String> otherwise) {
		AlertTextField alert = new AlertTextField(AlertType.CONFIRMATION, content, AlertButton.yes(), AlertButton.no());
		alert.setOnActionEvent(action -> {
			if (action == AlertButton.Action.YES) {
				confirm.handler(alert.getInput().getText());
			} else {
				if (otherwise != null) {
					otherwise.handler(alert.getInput().getText());
				}
			}
			return true;
		});
		alert.autoSize().showRelativeCenter(owner);
		return alert;
	}
}
