package com.ht.ui.taginfo;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

import com.ht.ui.controls.MessageInfoLabel;

public class CmpPanel extends VBox {
	
	private MessageInfoLabel messageLabel;
	private GridPane cmpMessageBox;
	private TextField cmpTimeValue;
	
	private Label parentValueLabel;
	private Label childrenValueLabel;
	private Label infoLabel;

	public CmpPanel(EventHandler<ActionEvent> onOK) {
		super(2);
		
		Label title2 = new Label("校验比对");
		title2.getStyleClass().add("info-pane-header");

		GridPane gridPane = new GridPane();
		gridPane.setHgap(6);
		gridPane.setVgap(10);

		Label label = new Label("校核时间:");
		GridPane.setConstraints(label, 0, 0);
		GridPane.setHalignment(label, HPos.RIGHT);
		gridPane.getChildren().add(label);

		cmpTimeValue = new TextField("1");
		cmpTimeValue.setMaxWidth(60);
		GridPane.setConstraints(cmpTimeValue, 1, 0);
		GridPane.setHalignment(cmpTimeValue, HPos.LEFT);
		gridPane.getChildren().add(cmpTimeValue);

		label = new Label("小时");
		GridPane.setConstraints(label, 2, 0);
		GridPane.setHalignment(label, HPos.LEFT);
		gridPane.getChildren().add(label);

		Button ok = new Button("比对");
		GridPane.setConstraints(ok, 3, 0);
		gridPane.getChildren().add(ok);
		ok.setOnAction(onOK);

		Region spacer2 = new Region();
		spacer2.setMinHeight(10);

		createCompareMessagePanel();
		
		messageLabel = new MessageInfoLabel(2, 2);
		getChildren().addAll(title2, gridPane, spacer2, cmpMessageBox, messageLabel);
	}
	
	private void createCompareMessagePanel() {
		cmpMessageBox = new GridPane();
		cmpMessageBox.setVisible(false);
		cmpMessageBox.setHgap(6);
		cmpMessageBox.setVgap(10);

		Label label = new Label("总耗电量:");
		GridPane.setConstraints(label, 0, 0);
		GridPane.setHalignment(label, HPos.RIGHT);
		cmpMessageBox.getChildren().add(label);

		parentValueLabel = new Label();
		parentValueLabel.setStyle("-fx-text-fill:blue;");
		GridPane.setConstraints(parentValueLabel, 1, 0);
		GridPane.setHalignment(parentValueLabel, HPos.LEFT);
		cmpMessageBox.getChildren().add(parentValueLabel);

		label = new Label("各支路耗电量总计:");
		GridPane.setConstraints(label, 0, 1);
		GridPane.setHalignment(label, HPos.RIGHT);
		cmpMessageBox.getChildren().add(label);

		childrenValueLabel = new Label();
		childrenValueLabel.setStyle("-fx-text-fill:blue;");
		GridPane.setConstraints(childrenValueLabel, 1, 1);
		GridPane.setHalignment(childrenValueLabel, HPos.LEFT);
		cmpMessageBox.getChildren().add(childrenValueLabel);

		label = new Label("误差:");
		GridPane.setConstraints(label, 0, 2);
		GridPane.setHalignment(label, HPos.RIGHT);
		cmpMessageBox.getChildren().add(label);

		infoLabel = new Label();
		// errorLabel.setStyle("-fx-text-fill:red;");
		GridPane.setConstraints(infoLabel, 1, 2);
		GridPane.setHalignment(infoLabel, HPos.LEFT);
		cmpMessageBox.getChildren().add(infoLabel);

	}
	
	int getCmpTime() {
		return Integer.parseInt(cmpTimeValue.getText().trim());
	}
	
	void hiddenCmpMessage() {
		this.cmpMessageBox.setVisible(false);
	}
	
	void setParentValue(String text) {
		this.parentValueLabel.setText(text);
	}
	
	void setChildrenValue(String text) {
		this.childrenValueLabel.setText(text);
	}
	
	void showCmpInfo(String value, String style) {
		this.infoLabel.setText(value);
		infoLabel.setStyle(style);
		this.cmpMessageBox.setVisible(true);
	}
	
	public void showMessage(String msg) {
		this.messageLabel.show(msg);
	}
}
