package com.ht.ui.user;

import java.util.Date;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.geometry.Side;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;

import com.ht.util.UserSession;
import com.ht.web.mongo.domain.User;

public class UserManagerViewer extends VBox {

	private TabPane tabPane;
	private UserInfoEditor userInfoPanel;
//	private UserGroupEditor userGroupPanel;
//	private UserEditor userPanel;

	public UserManagerViewer() {
		setId("UserManagerViewer");
		getStyleClass().add("window");
		
		Rectangle2D rect = Screen.getPrimary().getVisualBounds();
		setMaxWidth(rect.getWidth() * 0.618);
//		StackPane.setMargin(this, new Insets(20));

		tabPane = new TabPane();
		tabPane.setSide(Side.TOP);
		tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
		tabPane.setPrefHeight(rect.getHeight());
		
		VBox.setMargin(tabPane, new Insets(15, 0, 15, 0));
		
		final Tab tab1 = new Tab();
		tab1.setText("基本信息");
		userInfoPanel = new UserInfoEditor();
		tab1.setContent(userInfoPanel);

		final Tab tab2 = new Tab();
		tab2.setText("用户组");
		final UserGroupEditor userGroupPanel = new UserGroupEditor();
		tab2.setContent(userGroupPanel);

		final Tab tab3 = new Tab();
		tab3.setText("用户");
		final UserEditor userPanel = new UserEditor();
		tab3.setContent(userPanel);

		final Tab tab4 = new Tab();
		tab4.setText("操作记录");
		final UserOperationRecordPanel userOperation = new UserOperationRecordPanel();
		tab4.setContent(userOperation);
		
		tabPane.getTabs().addAll(tab1, tab2, tab3, tab4);

		tabPane.getSelectionModel().selectedItemProperty()
				.addListener(new ChangeListener<Tab>() {
					@Override
					public void changed(
							ObservableValue<? extends Tab> observable,
							Tab oldValue, Tab newValue) {
						User user = UserSession.INSTANCE.getUser();
						if (newValue.equals(tab1)) {

						} else if (user == null) {
							// 会话超时
							UserLoginDialog.getInstance().showDialog(null,
									"您已自动退出, 请重新登录");
							tabPane.getSelectionModel().select(tab1);
						} else {

							if (user.getGroup().getUserEdit()) {

								if (newValue.equals(tab2)) {// 用户组管理
									userGroupPanel.reload();
								} else if (newValue.equals(tab3)) {// 用户管理
									userPanel.reload();
								} else {// 操作记录
									userOperation.showData(new Date());
								}
							} else {

								UserLoginDialog.getInstance().showDialog(null,
										"需要更高权限, 是否切换用户?");
								tabPane.getSelectionModel().select(tab1);
							}

						}

					}
				});
		// visibleProperty().addListener(new ChangeListener<Boolean>() {
		// @Override
		// public void changed(ObservableValue<? extends Boolean> observable,
		// Boolean oldValue, Boolean newValue) {
		// if (newValue) {
		//
		// User user = ClientSessionManager.INSTANCE.getUserService()
		// .getUser();
		// tabPane.getTabs().clear();
		// tabPane.getTabs().add(tab1);
		// if (user.getGroup().getUserEdit()) {
		// tabPane.getTabs().addAll(tab4, tab3, tab2);
		// }
		// }
		//
		// }
		// });

		getChildren().add(tabPane);
	}
//
//	public UserEditor getUserPanel() {
//		return userPanel;
//	}
//
//	public UserGroupEditor getUserGroupPanel() {
//		return userGroupPanel;
//	}

	public UserInfoEditor getUserInfoPanel() {
		return userInfoPanel;
	}
}
