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

import com.imyeyu.fx.task.RunAsync;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.alert.AbstractAlert;
import com.imyeyu.fx.ui.components.alert.AlertButton;
import com.imyeyu.fx.ui.components.alert.AlertConfirm;
import com.imyeyu.fx.ui.components.alert.AlertFileBlendSelector;
import com.imyeyu.fx.ui.components.alert.AlertFilePathSelector;
import com.imyeyu.fx.ui.components.alert.AlertFileSelector;
import com.imyeyu.fx.ui.components.alert.AlertLoading;
import com.imyeyu.fx.ui.components.alert.AlertTextArea;
import com.imyeyu.fx.ui.components.alert.AlertTextField;
import com.imyeyu.fx.ui.components.alert.AlertTips;
import com.imyeyu.fx.ui.components.alert.AlertType;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.fx.utils.grid.Column;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.java.bean.timi.TimiCode;
import com.imyeyu.java.bean.timi.TimiException;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;

/**
 * 弹出窗体
 *
 * @author 夜雨
 * @since 2022-09-01 14:39
 */
@Component
public class AlertDemo extends AbstractDemoPane {

	public AlertDemo() {
		title.setText(SidebarItem.ALERT.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/component/alert/package-summary.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/component/alert");

		// 一般
		Button info = new Button(TimiFXUI.MULTILINGUAL.text("info"));
		info.setBorder(Stroke.EX_RIGHT);
		Button warning = new Button(TimiFXUI.MULTILINGUAL.text("warning"));
		warning.setBorder(Stroke.EX_RIGHT);
		Button warningDanger = new Button(TimiFXUI.MULTILINGUAL.text("warning"));
		warningDanger.setBorder(Stroke.EX_RIGHT);
		Button error = new Button(TimiFXUI.MULTILINGUAL.text("error"));
		error.setBorder(Stroke.EX_RIGHT);
		Button deadlyError = new Button(TimiFXUI.MULTILINGUAL.text("fx.example.alert.deadly_error"));

		// 询问
		Button confirmInfo = new Button(TimiFXUI.MULTILINGUAL.text("info"));
		confirmInfo.setBorder(Stroke.EX_RIGHT);
		Button confirmWarning = new Button(TimiFXUI.MULTILINGUAL.text("warning"));
		confirmWarning.setBorder(Stroke.EX_RIGHT);
		Button confirmWarningDanger = new Button(TimiFXUI.MULTILINGUAL.text("warning"));
		confirmWarningDanger.setBorder(Stroke.EX_RIGHT);
		Button confirmError = new Button(TimiFXUI.MULTILINGUAL.text("error"));
		confirmError.setBorder(Stroke.EX_RIGHT);
		Button confirmCancel = new Button(TimiFXUI.MULTILINGUAL.text("fx.example.alert.can_cancel"));

		// 输入
		Button textField = new Button(TimiFXUI.MULTILINGUAL.text("fx.example.alert.text_field"));
		textField.setBorder(Stroke.EX_RIGHT);
		Button textArea = new Button(TimiFXUI.MULTILINGUAL.text("fx.example.alert.text_area"));

		// 文件
		Button file = new Button(TimiFXUI.MULTILINGUAL.text("file.select"));
		file.setBorder(Stroke.EX_RIGHT);
		Button directory = new Button(TimiFXUI.MULTILINGUAL.text("file.select.directory"));
		directory.setBorder(Stroke.EX_RIGHT);
		Button fileBlend = new Button(TimiFXUI.MULTILINGUAL.text("fx.example.alert.file_blend"));

		// 加载中
		Button loading = new Button(TimiFXUI.MULTILINGUAL.text("loading"));
		loading.setBorder(Stroke.EX_RIGHT);

		// 自定义
		Button custom = new Button(TimiFXUI.MULTILINGUAL.text("custom"));

		setCenter(new GridPane() {{
			getColumnConstraints().addAll(Column.key(), Column.key());
			setHgap(12);
			setVgap(6);
			setPadding(new Insets(20));

			int row = 0;
			addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("commonly")), new HBox() {{
				getChildren().addAll(info, warning, warningDanger, error, deadlyError);
			}});
			addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("confirmation")), new HBox() {{
				getChildren().addAll(confirmInfo, confirmWarning, confirmWarningDanger, confirmError, confirmCancel);
			}});
			addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("input")), new HBox(textField, textArea));
			addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("file")), new HBox() {{
				setSpacing(6);
				setAlignment(Pos.CENTER_LEFT);
				getChildren().addAll(new HBox(file, directory, fileBlend), TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.alert.multi_tips")));
			}});
			addRow(row, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("other")), new HBox(loading, custom));
		}});

		// ---------- 事件 ----------
		
		String text = "TimiFX";

		// 一般
		info.setOnAction(e -> AlertTips.info(getScene().getWindow(), text));
		warning.setOnAction(e -> AlertTips.warn(getScene().getWindow(), text));
		warningDanger.setOnAction(e -> AlertTips.warnDanger(getScene().getWindow(), text));
		error.setOnAction(e -> AlertTips.error(getScene().getWindow(), text));
		deadlyError.setOnAction(e -> AlertTextArea.error(getScene().getWindow(), new TimiException(TimiCode.ERROR)));

		AlertConfirm alertConfirm = new AlertConfirm(text) {

			@Override
			protected void onConfirm() {
				AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("yes"));
			}

			@Override
			protected void onCancel() {
				AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("cancel"));
			}
		};
		alertConfirm.setOnCloseRequest(e -> AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("cancel")));

		// 询问
		confirmInfo.setOnAction(e -> {
			alertConfirm.setType(AlertType.INFORMATION);
			alertConfirm.autoSize().showRelativeCenter(getScene().getWindow());
		});
		confirmWarning.setOnAction(e -> {
			alertConfirm.setType(AlertType.WARNING);
			alertConfirm.autoSize().showRelativeCenter(getScene().getWindow());
		});
		confirmWarningDanger.setOnAction(e -> {
			alertConfirm.setType(AlertType.WARNING_DANGER);
			alertConfirm.autoSize().showRelativeCenter(getScene().getWindow());
		});
		confirmError.setOnAction(e -> {
			alertConfirm.setType(AlertType.ERROR);
			alertConfirm.autoSize().showRelativeCenter(getScene().getWindow());
		});
		confirmCancel.setOnAction(e -> {
			AlertTips alert = new AlertTips(text);
			alert.setType(AlertType.WARNING_DANGER);
			alert.setButton(AlertButton.yes(), AlertButton.no(), AlertButton.cancel());
			alert.setOnActionEvent(action -> {
				switch (action) {
					case YES    -> AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("yes"));
					case NO     -> AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("no"));
					case CANCEL -> AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("cancel"));
				}
				return true;
			});
			alert.setOnCloseRequest(closeE -> AlertTips.info(getScene().getWindow(), TimiFXUI.MULTILINGUAL.text("cancel")));
			alert.autoSize().showRelativeCenter(getScene().getWindow());
		});

		// 输入
		textField.setOnAction(e -> AlertTextField.confirm(getScene().getWindow(), text, value -> AlertTips.info(getScene().getWindow(), value)));
		textArea.setOnAction(e -> AlertTextArea.info(getScene().getWindow(), text));

		// 文件
		file.setOnAction(e -> new AlertFileSelector(SelectionMode.SINGLE).showRelativeCenter(getScene().getWindow()));
		directory.setOnAction(e -> new AlertFilePathSelector(SelectionMode.SINGLE).showRelativeCenter(getScene().getWindow()));
		fileBlend.setOnAction(e -> new AlertFileBlendSelector(SelectionMode.MULTIPLE).showRelativeCenter(getScene().getWindow()));

		// 加载中
		loading.setOnAction(e -> {
			AlertLoading alert = new AlertLoading(TimiFXUI.MULTILINGUAL.text("loading"));
			alert.showRelativeCenter(getScene().getWindow());
			RunAsync.later(alert::close, 2000);
		});

		// 自定义
		CustomAlert customAlert = new CustomAlert();
		custom.setOnAction(e -> customAlert.showRelativeCenter(getScene().getWindow()));
	}

	/**
	 * 自定义窗体
	 *
	 * @author 夜雨
 * @since 2022-09-02 15:21
	 */
	private static class CustomAlert extends AbstractAlert {

		public CustomAlert() {
			ListView<String> list = new ListView<>();
			ComboBox<String> comboBox = new ComboBox<>();
			CheckBox checkBox = new CheckBox("CheckBox");
			checkBox.setSelected(true);

			root.setPadding(new Insets(8, 12, 8, 12));
			root.setTop(TimiFXUI.label("Custom Tips"));
			root.setCenter(list);

			setType(AlertType.INFORMATION);
			setTitle(TimiFXUI.MULTILINGUAL.text("custom"));
			setWidth(520);
			setHeight(460);
			setRightButtons(AlertButton.confirm(), AlertButton.cancel());

			btnPane.setPadding(new Insets(12, 0, 0, 0));
			leftButtons.getChildren().addAll(comboBox, checkBox);
			for (int i = 0; i < 20; i++) {
				list.getItems().add("item " + i);
				comboBox.getItems().add("item " + i);
			}
			comboBox.getSelectionModel().select(0);

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

			setOnActionEvent(action -> true);
		}
	}
}
