package com.imyeyu.fx.ui.examples.component.sidebar;

import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.components.navigation.Navigation;
import com.imyeyu.fx.ui.components.navigation.NavigationGroup;
import com.imyeyu.fx.ui.components.navigation.NavigationItem;
import com.imyeyu.fx.ui.examples.service.PageService;
import com.imyeyu.inject.annotation.Component;

import java.util.EnumMap;
import java.util.Map;

/**
 * 导航
 *
 * @author 夜雨
 * @since 2022-08-26 14:56
 */
@Component
public class Sidebar extends Navigation {

	/** 侧边栏项目映射 */
	private final Map<SidebarItem, Item> itemMap = new EnumMap<>(SidebarItem.class);

	public Sidebar(PageService pageService) {

		Item welcome = new Item(SidebarItem.WELCOME);
		Item style = new Item(SidebarItem.STYLE);
		Item extendTools = new Item(SidebarItem.EXTEND_TOOLS);
		Item bindingConfig = new Item(SidebarItem.BINDING_CONFIG);
		Item alert = new Item(SidebarItem.ALERT);
		Item popupTips = new Item(SidebarItem.POPUP_TIPS);
		Item runAsync = new Item(SidebarItem.RUN_ASYNC);

		add(welcome, style, extendTools, bindingConfig, alert, popupTips, runAsync);

		// 动画
		Item interpolator = new Item(SidebarItem.INTERPOLATOR);
		Item smoothScroll = new Item(SidebarItem.SMOOTH_SCROLL);
		add(new NavigationGroup(TimiFXUI.MULTILINGUAL.text("animation")).add(interpolator, smoothScroll));

		// 组件
		SidebarItem[] components = {SidebarItem.BUTTON, SidebarItem.DATE_TIME_PICKER, SidebarItem.EDITABLE_TABLE_CELL, SidebarItem.FILE_TREE_VIEW, SidebarItem.ICON_BUTTON, SidebarItem.ICON_PICKER, SidebarItem.LABEL_PROGRESS_BAR, SidebarItem.NAVIGATION, SidebarItem.NUMBER_FIELD, SidebarItem.FILLED_SLIDER, SidebarItem.SELECTABLE_LABEL, SidebarItem.TEXT_AREA_EDITOR, SidebarItem.TITLE_LABEL, SidebarItem.TOGGLE_ICON, SidebarItem.X_PAGINATION, SidebarItem.X_TAB_PANE, SidebarItem.X_TREE_VIEW};
		Item[] componentItems = new Item[components.length];
		for (int i = 0; i < componentItems.length; i++) {
			componentItems[i] = new Item(components[i]);
		}
		add(new NavigationGroup(TimiFXUI.MULTILINGUAL.text("component")).add(componentItems));

		// 其他
		Item draggableNode = new Item(SidebarItem.DRAGGABLE_NODE);
		Item draggableWindow = new Item(SidebarItem.DRAGGABLE_WINDOW);
		Item screen = new Item(SidebarItem.SCREEN);
		Item tray = new Item(SidebarItem.TRAY);
		add(new NavigationGroup(TimiFXUI.MULTILINGUAL.text("other")).add(draggableNode, draggableWindow, screen, tray));
		setBorder(Stroke.RIGHT);
		setMinWidth(140);

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

		// 跳转
		selectedItem.addListener((obs, o, item) -> {
			if (item instanceof Item i) {
				pageService.to(i.item);
			}
		});
	}

	/**
	 * 设置选中
	 *
	 * @param item 选中项
	 */
	public void setSelected(SidebarItem item) {
		Item selected = itemMap.get(item);
		if (selected != null) {
			setSelectedItem(selected);
			return;
		}
		throw new NullPointerException("not found item page for %s".formatted(item));
	}

	/**
	 * 列表项
	 *
	 * @author 夜雨
	 * @since 2022-02-22 19:45
	 */
	private class Item extends NavigationItem {

		/** 列表项对象 */
		final SidebarItem item;

		/**
		 * 创建侧边栏项目
		 *
		 * @param item 侧边栏项目
		 */
		public Item(SidebarItem item) {
			this.item = item;
			setText(item.text);
			itemMap.put(item, this);
		}
	}
}
