package com.imyeyu.fx.ui.examples.service;

import com.imyeyu.fx.ui.examples.component.sidebar.SidebarItem;
import com.imyeyu.inject.annotation.Service;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

/**
 * 页面服务
 *
 * @author 夜雨
 * @since 2021-12-26 10:57
 */
@Service
public class PageService {

	private SidebarItem prev;

	private final ObjectProperty<SidebarItem> activatedPageProperty;

	public PageService() {
		activatedPageProperty = new SimpleObjectProperty<>();
		activatedPageProperty.addListener((obs, prev, now) -> {
			this.prev = prev;
		});
	}

	/**
	 * 跳转页面
	 *
	 * @param page 页面
	 */
	public void to(SidebarItem page) {
		activatedPageProperty.set(page);
	}

	/** 返回上一个页面 */
	public void back() {
		to(prev);
	}

	/** @return 当前页面监听 */
	public ReadOnlyObjectProperty<SidebarItem> activatedPageProperty() {
		return activatedPageProperty;
	}
}
