package com.ht.ui.alarm;

import java.util.Date;

import com.ht.ui.user.UserLoginDialog;
import com.ht.util.UserSession;
import com.ht.web.mongo.domain.User;

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;

public class AlarmRecordViewer extends VBox {

	private TabPane tabPane;
	
	public AlarmRecordViewer() {
		
		this.setVisible(false);
		setId("AlarmRecordViewer");
		getStyleClass().add("window");
		
		Rectangle2D rect = Screen.getPrimary().getVisualBounds();
		//setMaxWidth(rect.getWidth() * 0.618 > 950 ? rect.getWidth() * 0.618 : 950);
//		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("遥信变位记录");
		final OnOffShiftRecordPanel onOffShift = new OnOffShiftRecordPanel();
		onOffShift.showData(new Date());
		tab1.setContent(onOffShift);

		final Tab tab2 = new Tab();
		tab2.setText("遥测越限报警");
		final AlarmRecordPanel alarm = new AlarmRecordPanel();
		tab2.setContent(alarm);

		final Tab tab3 = new Tab();
		tab3.setText("故障报警");
		final FaultRecordPanel fault = new FaultRecordPanel();
		tab3.setContent(fault);

		final Tab tab4 = new Tab();
		tab4.setText("SOE报警");
		final SOERecordPanel soe = new SOERecordPanel();
		tab4.setContent(soe);
		
		tabPane.getTabs().addAll(tab1, tab2, tab3, tab4);
		
		getChildren().add(tabPane);
		
		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 (user == null) {
					// 会话超时
					UserLoginDialog.getInstance().showDialog(null,
							"您已自动退出, 请重新登录");
					tabPane.getSelectionModel().select(tab1);
				} else {

					if (user.getGroup().getAlarmRecord()) {

						if (newValue.equals(tab1)) {// 遥信变位记录
							onOffShift.showData(new Date());
						} else if (newValue.equals(tab2)) {// 遥测越限报警
							alarm.showData(new Date());
						} else if (newValue.equals(tab3)) {// 故障报警
							fault.showData(new Date());
						} else {// SOE报警
							soe.showData(new Date());
						}
					} else {

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

				}

			}
		});
		
	}

}
