package com.ht.ui.user;

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.svg.action.ShowMenuAction;
import com.ht.ui.MainApplication;
import com.ht.ui.controls.RecorderNavigator;
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.UserOperation;
import com.ht.web.remoting.HessianResponse;

public class UserOperationRecordPanel extends VBox {
	private ObservableList<UserOperation> tableDataList;
	private RecorderNavigator toolBar;


	public UserOperationRecordPanel() {
		super(5);
		
		createToolBar();
		createTable();

	}

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

		final TableView<UserOperation> tableView = new TableView<UserOperation>(
				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 TableColumn<UserOperation, String> usernameCol = new TableColumn<UserOperation, String>(
				"用户名");
		usernameCol.setPrefWidth(100);
		usernameCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserOperation, String>, ObservableValue<String>>() {
					@Override
					public ObservableValue<String> call(
							CellDataFeatures<UserOperation, String> value) {
						return new SimpleStringProperty(value.getValue()
								.getUser().getUsername());
					}
				});
		final TableColumn<UserOperation, String> nameCol = new TableColumn<UserOperation, String>(
				"姓名");
		nameCol.setPrefWidth(100);
		nameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserOperation, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<UserOperation, String> value) {
				return new SimpleStringProperty(value.getValue().getUser()
						.getName());
			}
		});
		final TableColumn<UserOperation, String> deptCol = new TableColumn<UserOperation, String>(
				"部门");
		deptCol.setPrefWidth(150);
		deptCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserOperation, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<UserOperation, String> value) {
				return new SimpleStringProperty(value.getValue().getUser()
						.getDepartment());
			}
		});
		final TableColumn<UserOperation, String> infoCol = new TableColumn<UserOperation, String>(
				"操作描述");
		infoCol.setPrefWidth(350);
		infoCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserOperation, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<UserOperation, String> value) {
				return new SimpleStringProperty(value.getValue().getDescr());
			}
		});
		final TableColumn<UserOperation, String> timeCol = new TableColumn<UserOperation, String>(
				"操作时间");
		timeCol.setPrefWidth(150);
		timeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<UserOperation, String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(
					CellDataFeatures<UserOperation, String> value) {
				return new SimpleStringProperty(timeFormat.format(value
						.getValue().getDatetime()));
			}
		});
		tableView.getColumns().addAll(usernameCol, nameCol, infoCol, timeCol);

//		getChildren().add(tableView);
		
		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());
	}
	
	private HessianResponseService service = new HessianResponseService() {
		
		@Override
		protected Task<HessianResponse> createTask() {
			return new HessianResponseTask() {
				
				@Override
				protected HessianResponse call() throws Exception {
					try {
						return HessianFactoryManger.INSTANCE
								.getUserService().getUserOperate(
										toolBar.getStart(), toolBar.getEnd());
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}
				
				@Override
				public void successResponse(HessianResponse resp) {
					List<UserOperation> list = (List<UserOperation>) 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());
					}
				}
			};
		}
	};

	private void createToolBar() {
		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);
	}

}
