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

import com.imyeyu.fx.icon.TimiFXIcon;
import com.imyeyu.fx.icon.TimiIcon;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.TextAreaEditor;
import com.imyeyu.fx.ui.components.TrayFX;
import com.imyeyu.fx.ui.components.popup.PopupTipsService;
import com.imyeyu.fx.ui.examples.component.AbstractDemoPane;
import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.fx.ui.examples.ctrl.Main;
import com.imyeyu.fx.utils.Windows;
import com.imyeyu.inject.annotation.PostConstruct;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.awt.TrayIcon;

/**
 * 系统托盘
 *
 * @author 夜雨
 * @since 2022-09-01 14:39
 */
public class TrayFXDemo extends AbstractDemoPane {

	private final Main main;
	private final Stage stage;
	private final TrayFX trayFX;
	private final Button show, hide;

	public TrayFXDemo(Main main, Stage stage, TrayFX trayFX) {
		this.main = main;
		this.stage = stage;
		this.trayFX = trayFX;
		title.setText(SidebarItem.TRAY.getText());
		document.sync("https://doc.imyeyu.net/timi-fx/net/imyeyu/timifx/util/TrayFX.html");
		source.sync("https://git.imyeyu.net/Timi/timi-fx/src/master/src/main/java/net/imyeyu/timifx/util/TrayFX.java");
		tips.setText(TimiFXUI.MULTILINGUAL.text("fx.example.tray.tips"));

		show = new Button(TimiFXUI.MULTILINGUAL.text("show"));
		hide = new Button(TimiFXUI.MULTILINGUAL.text("remove"));
		hide.setBorder(Stroke.EX_LEFT);

		Label labelSendMsg = TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.tray.send_msg"));

		TextField msgTitle = new TextField();

		// 消息类型
		ComboBox<TrayIcon.MessageType> msgType = new ComboBox<>();
		msgType.getItems().setAll(TrayIcon.MessageType.values());
		msgType.setValue(TrayIcon.MessageType.NONE);

		// 消息
		Label labelMsg = TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("content"));
		TextAreaEditor msg = new TextAreaEditor();

		// 发送
		Button msgSend = new Button(TimiFXUI.MULTILINGUAL.text("send"));
		PopupTipsService.installText(msgSend, TimiFXUI.MULTILINGUAL.text("fx.example.tray.send_msg.tips"));

		setCenter(new GridPane() {{
			setMargin(labelSendMsg, new Insets(26, 0, 0, 0));
			setValignment(labelSendMsg, VPos.TOP);
			setVgap(8);
			setHgap(12);
			setPadding(new Insets(20));

			int row = 0;
			addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("fx.example.tray.icon")), new HBox(show, hide));
			addRow(row, labelSendMsg, new GridPane() {{
				setMargin(labelMsg, new Insets(5, 0, 0, 0));
				setValignment(labelMsg, VPos.TOP);
				setVgap(8);
				setHgap(12);
				setPadding(new Insets(20));

				int row = 0;
				addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("title")), msgTitle);
				addRow(row++, TimiFXUI.label(TimiFXUI.MULTILINGUAL.text("type")), msgType);
				addRow(row++, labelMsg, msg);
				add(msgSend, 1, row);
			}});
		}});

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

		show.setOnAction(e -> trayFX.show((RESOURCE + "icon.png").substring(1)));
		hide.setOnAction(e -> trayFX.remove());
		msgSend.setOnAction(e -> {
			if (trayFX.isShowing()) {
				trayFX.sendMessage(msgTitle.getText(), msg.getText(), msgType.getValue());
			}
		});
	}

	@PostConstruct
	public void init() {
		show.disableProperty().bind(trayFX.showingProperty());
		hide.disableProperty().bind(trayFX.showingProperty().not());

		MenuItem show = new MenuItem(TimiFXUI.MULTILINGUAL.text("show"));
		MenuItem exit = new MenuItem(TimiFXUI.MULTILINGUAL.text("exit"), TimiFXIcon.get(TimiIcon.FAIL, Colorful.RED));

		show.setOnAction(e -> Windows.requestTop(stage));
		exit.setOnAction(e -> main.stop());

		trayFX.addMenu(0, show, TimiFXUI.sep(), exit);
	}
}
