package com.ht.ui.alarm;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

import com.ht.ui.MainApplication;
import com.ht.ui.controls.RecorderNavigator;
import com.ht.ui.user.UserLoginDialog;
import com.ht.ui.util.HessianResponseService;
import com.ht.ui.util.HessianResponseTask;
import com.ht.ui.util.MessageConstants;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.mongo.domain.Alarm;
import com.ht.web.remoting.HessianResponse;

public class AlarmRecordPanel extends VBox {
	private ObservableList<Alarm> tableDataList;
	private RecorderNavigator toolBar;
	private HessianResponseService service;
	private TableView<Alarm> tableView;

	public AlarmRecordPanel() {
		super(5);

		createToolBar();
		createTable();
		
		ProgressIndicator progress = new ProgressIndicator();
		progress.setMaxSize(150, 150);
		Region veil = new Region();
		veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
		
		StackPane stackPane = new StackPane();
		VBox.setVgrow(stackPane, Priority.ALWAYS);
		stackPane.getChildren().addAll(tableView, veil, progress);
		getChildren().add(stackPane);
		
		progress.progressProperty().bind(service.progressProperty());
        veil.visibleProperty().bind(service.runningProperty());
        progress.visibleProperty().bind(service.runningProperty());
	}

	@SuppressWarnings("unchecked")
	private void createTable() {
		tableDataList = FXCollections.observableArrayList();

		tableView = new TableView<Alarm>(tableDataList);
		tableView.setPlaceholder(new Label("遥测超限报警记录为空"));
		tableView.setItems(tableDataList);
//		VBox.setVgrow(tableView, Priority.ALWAYS);

		final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		final DecimalFormat decimalFormat = new DecimalFormat("0.##");

		final TableColumn<Alarm, String> tagPathCol = new TableColumn<Alarm, String>(
				"变量位置");
		tagPathCol.setPrefWidth(400);
		tagPathCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
					@Override
					public ObservableValue<String> call(
							CellDataFeatures<Alarm, String> value) {
						String path = value.getValue().getTagPath();
						return new SimpleStringProperty(path.substring(0,
								path.lastIndexOf('/')));
					}
				});
		final TableColumn<Alarm, String> tagNameCol = new TableColumn<Alarm, String>(
				"变量名称");
		tagNameCol.setPrefWidth(100);
		tagNameCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
					@Override
					public ObservableValue<String> call(
							CellDataFeatures<Alarm, String> value) {
						return new SimpleStringProperty(value.getValue()
								.getTagName());
					}
				});
		final TableColumn<Alarm, String> infoCol = new TableColumn<Alarm, String>(
				"报警信息");
		infoCol.setPrefWidth(150);
		infoCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<Alarm, String> value) {
				return new SimpleStringProperty(value.getValue().getInfo());
			}
		});
		final TableColumn<Alarm, String> settingCol = new TableColumn<Alarm, String>(
				"设定值");
		settingCol.setPrefWidth(80);
		settingCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
					@Override
					public ObservableValue<String> call(
							CellDataFeatures<Alarm, String> value) {
						return new SimpleStringProperty(decimalFormat
								.format(value.getValue().getSettingValue()));
					}
				});
		final TableColumn<Alarm, String> valueCol = new TableColumn<Alarm, String>(
				"设定值");
		valueCol.setPrefWidth(80);
		valueCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<Alarm, String> value) {
				return new SimpleStringProperty(decimalFormat.format(value
						.getValue().getValue()));
			}
		});
		final TableColumn<Alarm, String> timeCol = new TableColumn<Alarm, String>(
				"动作时间");
		timeCol.setPrefWidth(180);
		timeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<Alarm, String> value) {
				return new SimpleStringProperty(timeFormat.format(value
						.getValue().getDatetime()));
			}
		});
		final TableColumn<Alarm, String> resumeTimeCol = new TableColumn<Alarm, String>(
				"解除时间");
		resumeTimeCol.setPrefWidth(180);
		resumeTimeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Alarm, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<Alarm, String> value) {
				if (value.getValue().getResumeTime() != null) {
					return new SimpleStringProperty(timeFormat.format(value.getValue().getResumeTime()));
				} else {
					return new SimpleStringProperty();
				}
			}
		});
		tableView.getColumns().addAll(tagPathCol, tagNameCol, infoCol,
				settingCol, valueCol, timeCol, resumeTimeCol);

//		getChildren().add(tableView);
	}

	private void createToolBar() {

		service = new HessianResponseService() {
			
			@Override
			protected Task<HessianResponse> createTask() {
				return new HessianResponseTask() {
					
					@Override
					protected HessianResponse call() throws Exception {
						 try {
							return HessianFactoryManger.INSTANCE
										.getAlarmService().getAlarmRecord(
												toolBar.getStart(), toolBar.getEnd());
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					
					@Override
					public void successResponse(HessianResponse resp) {

							List<Alarm> list = (List<Alarm>) resp.getData();

							toolBar.setInfo("" +list.size());
							tableDataList.addAll(list);
					}
					
					@Override
					public void nullResponse() {

						MainApplication.getMainApp().showMessage(
								MessageConstants.ERROR_APP_ERROR);
					}
					
					@Override
					public void failureResponse(HessianResponse resp) {
							if (resp.isLogin()) {
								MainApplication.getMainApp().showMessage(
										resp.getMsg());
							} else {
								UserLoginDialog.getInstance().showDialog(null,
										resp.getMsg());
							}
						
					}
				};
			}
		};
		Runnable run = new Runnable() {

			@Override
			public void run() {
				tableDataList.clear();
				service.restart();
			}
		};
		toolBar = new RecorderNavigator(run);

		getChildren().add(toolBar);
	}

	public void showData(Date date) {
		toolBar.setDate(date);
	}

}
