import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.control.*;
import je.NumberField;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.scene.paint.Color;
import javafx.geometry.Insets;
import javafx.scene.layout.*;
import javafx.scene.Cursor;

/**
 *
 * Beschreibung
 *
 * @version 1.0 vom 31.05.2022
 * @author Rr
 */

public class GuiEinBlock extends Application {
  // Anfang Attribute
  private Pane pane1 = new Pane();
    private Label lBlock = new Label();
    private Label lSchwierigkeit = new Label();
    private Label lDaten = new Label();
    private TextArea taDaten = new TextArea();
    private Label lHash = new Label();
    private TextField tfHash = new TextField();
    private Button bBerechneHash = new Button();
    private Button bMine = new Button();
    private Spinner<Integer> spSchwierigkeit = new Spinner<>(0, 10, 5, 1);
    private Label lNonce = new Label();
    private NumberField nfNonce = new NumberField();
    private Spinner<Integer> spBlock = new Spinner<>(0, 10, 0, 1);
  // Ende Attribute
  private Block einBlock = new Block(0, 0, "", "Gerhard: 50 Coins an Lutz");
  
  public void start(Stage primaryStage) { 
    Pane root = new Pane();
    Scene scene = new Scene(root, 500, 204);
    // Anfang Komponenten
    pane1.setLayoutX(0);
    pane1.setLayoutY(0);
    pane1.setPrefHeight(208);
    pane1.setPrefWidth(512);
    root.getChildren().add(pane1);
    lBlock.setLayoutX(208);
    lBlock.setLayoutY(8);
    lBlock.setPrefHeight(20);
    lBlock.setPrefWidth(46);
    lBlock.setText("Block");
    pane1.getChildren().add(lBlock);
    lSchwierigkeit.setLayoutX(344);
    lSchwierigkeit.setLayoutY(8);
    lSchwierigkeit.setPrefHeight(20);
    lSchwierigkeit.setPrefWidth(86);
    lSchwierigkeit.setText("Schwierigkeit");
    pane1.getChildren().add(lSchwierigkeit);
    spSchwierigkeit.setLayoutX(432);
    spSchwierigkeit.setLayoutY(5);
    spSchwierigkeit.setPrefHeight(27);
    spSchwierigkeit.setPrefWidth(54);
    spSchwierigkeit.setOnMouseClicked(
      (event) -> {spSchwierigkeit_MouseClicked(event);} 
    );
    pane1.getChildren().add(spSchwierigkeit);
    lNonce.setLayoutX(8);
    lNonce.setLayoutY(8);
    lNonce.setPrefHeight(20);
    lNonce.setPrefWidth(46);
    lNonce.setText("Nonce");
    pane1.getChildren().add(lNonce);
    nfNonce.setLayoutX(56);
    nfNonce.setLayoutY(6);
    nfNonce.setPrefHeight(25);
    nfNonce.setPrefWidth(75);
    pane1.getChildren().add(nfNonce);
    lDaten.setLayoutX(8);
    lDaten.setLayoutY(40);
    lDaten.setPrefHeight(20);
    lDaten.setPrefWidth(46);
    lDaten.setText("Daten");
    pane1.getChildren().add(lDaten);
    taDaten.setLayoutX(56);
    taDaten.setLayoutY(40);
    taDaten.setPrefHeight(84);
    taDaten.setPrefWidth(432);
    taDaten.setOnKeyReleased(
      (event) -> {taDaten_KeyReleased(event);} 
    );
    pane1.getChildren().add(taDaten);
    lHash.setLayoutX(8);
    lHash.setLayoutY(132);
    lHash.setPrefHeight(20);
    lHash.setPrefWidth(46);
    lHash.setText("Hash");
    pane1.getChildren().add(lHash);
    tfHash.setLayoutX(56);
    tfHash.setLayoutY(132);
    tfHash.setPrefHeight(25);
    tfHash.setPrefWidth(432);
    tfHash.setEditable(false);
    tfHash.setDisable(false);
    pane1.getChildren().add(tfHash);
    bBerechneHash.setLayoutX(160);
    bBerechneHash.setLayoutY(160);
    bBerechneHash.setPrefHeight(25);
    bBerechneHash.setPrefWidth(107);
    bBerechneHash.setOnAction(
      (event) -> {bBerechneHash_Action(event);} 
    );
    bBerechneHash.setText("Berechne Hash");
    pane1.getChildren().add(bBerechneHash);
    bMine.setLayoutX(272);
    bMine.setLayoutY(160);
    bMine.setPrefHeight(25);
    bMine.setPrefWidth(75);
    bMine.setOnAction(
      (event) -> {bMine_Action(event);} 
    );
    bMine.setText("Mine");
    pane1.getChildren().add(bMine);
    primaryStage.setOnShowing(
      (event) -> {primaryStage_Showing(event);} 
    );
    spBlock.setLayoutX(248);
    spBlock.setLayoutY(5);
    spBlock.setPrefHeight(27);
    spBlock.setPrefWidth(54);
    spBlock.setOnMouseClicked(
      (event) -> {spBlock_MouseClicked(event);} 
    );
    pane1.getChildren().add(spBlock);
    // Ende Komponenten
    
    primaryStage.setOnCloseRequest(e -> System.exit(0));
    primaryStage.setTitle("GuiEinBlock");
    primaryStage.setScene(scene);
    primaryStage.show();
  } // end of public GuiEinBlock
  
  // Anfang Methoden
  
  public static void main(String[] args) {
    launch(args);
  } // end of main
  
  public void primaryStage_Showing(Event evt) {
    ModelToView();
  } 
  
  public void bBerechneHash_Action(Event evt) {
    ViewToModel();
    einBlock.berechneHash();
    tfHash.setText(einBlock.getHash());
    ZeigeGültigkeit();
  } 

  public void bMine_Action(Event evt) {
    ViewToModel();
    einBlock.mine();
    ModelToView();
  }   
  
  public void ModelToView() {
    nfNonce.setInt(einBlock.getNonce());
    taDaten.setText(einBlock.getDaten());
    tfHash.setText(einBlock.getHash());
    ZeigeGültigkeit();
  }
  
  public void ViewToModel() {
    einBlock.setNonce(nfNonce.getInt());
    einBlock.setNr(spBlock.getValue());
    einBlock.setSchwierigkeit(spSchwierigkeit.getValue());
    einBlock.setDaten(taDaten.getText());
  }
  
  public void ZeigeGültigkeit() {
    if (einBlock.gültig())
      pane1.setStyle("-fx-background-color: #DBF0D4;");
    else 
      pane1.setStyle("-fx-background-color: #F7D9D9;");
  }
  
  public void taDaten_KeyReleased(KeyEvent evt) {
    einBlock.setDaten(taDaten.getText());
    bBerechneHash_Action(null);
    ZeigeGültigkeit();
  } 

  public void spSchwierigkeit_MouseClicked(MouseEvent evt) {
    einBlock.setSchwierigkeit(spSchwierigkeit.getValue());
    ZeigeGültigkeit();      
  } 

  public void spBlock_MouseClicked(MouseEvent evt) {
    einBlock.setNr(spBlock.getValue());
    ZeigeGültigkeit();    
  } 

  // Ende Methoden
} 
