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

import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.ui.examples.component.sidebar.Sidebar;
import com.imyeyu.fx.ui.examples.service.PageService;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.inject.annotation.PostConstruct;
import javafx.scene.layout.BorderPane;

/**
 * 根布局
 *
 * @author 夜雨
 * @since 2022-08-26 14:57
 */
@Component
public class RootLayout extends BorderPane implements TimiFXUI {

	private final Sidebar sidebar;
	private final PageService pageService;

	public RootLayout(Sidebar sidebar, PageService pageService) {
		this.sidebar = sidebar;
		this.pageService = pageService;
		setBorder(Stroke.TOP);
	}

	@PostConstruct
	public void init() {
		setLeft(sidebar);
		pageService.activatedPageProperty().addListener((obs, prev, now) -> {
			if (now != null && now.getIOCPage() instanceof AbstractPane pane) {
				pane.show();
				setCenter(pane);
			}
			if (prev != null && prev.getIOCPage() instanceof AbstractPane pane) {
				pane.hide();
			}
		});
	}
}
