package application;







import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.beans.binding.When;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableNumberValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.beans.binding.Bindings;

public class Controll implements Initializable {



	String vyberKS = " -c";
	String vyberVysielan = " -t ";
	String output;
	String bufferHod = "";
	String oknoHod = "";
	String segmentHod = "";
	String nodelayHod = "";
	String tofHod = "";
	String udpHod = "";
	String sirkapasmaHod= "";





	ObservableList<String> outFormatList = FXCollections.observableArrayList("b", "B", "k", "K", "m", "M");
	ObservableList<String> bityBajtyList = FXCollections.observableArrayList("K", "M");


	@FXML
	private ChoiceBox outFormat;
	@FXML
	private ChoiceBox BBB;
	@FXML
	private ChoiceBox BBO;
	@FXML
	private ChoiceBox BBS;
	@FXML
	private ChoiceBox BBP;  
	
	
	
	@FXML
	private CheckBox tofCB;
	@FXML
	private CheckBox buffer;
	@FXML
	private CheckBox okno;
	@FXML
	private CheckBox segment;
	@FXML
	private CheckBox nodelay;
	@FXML
	private CheckBox sirkapasma;
	@FXML
	private CheckBox UDP;

	@FXML
	public TextArea vystup ;

	@FXML
	private Button SpustiB;
	@FXML
	private Button SpustiB1;
	@FXML
	private Button SpustiB11;
	@FXML
	private Button SpustiB111;
	@FXML
	private Button SpustiB1111;


	@FXML
	private RadioButton rbKlient;
	@FXML
	private RadioButton rbServer;


	@FXML
	private RadioButton rbVysielByt ;

	@FXML
	private RadioButton rbVysielSek;


	@FXML
	private TextField prikazTF;
	@FXML
	private TextField adresTF;
	@FXML
	private TextField portTF;
	@FXML
	private TextField pocSpojTF;
	@FXML
	private TextField vysielanieTF;
	@FXML
	private TextField reportIntTF;
	@FXML
	private TextField bufferTF;
	@FXML
	private TextField oknoTF;
	@FXML
	private TextField segmetnTF;
	@FXML			  	
	private TextField ttlTF;
	@FXML
	private TextField tosTF;
	@FXML
	private TextField sirkapasmaTF;
	
	
	@FXML
	LineChart<String,Number> Graf;



	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		outFormat.setItems(outFormatList);
		BBB.setItems(bityBajtyList);
		BBO.setItems(bityBajtyList);
		BBP.setItems(bityBajtyList);
		



		rbVysielByt.setSelected(true);
		rbKlient.setSelected(true);


		BBB.setDisable(true);
		BBO.setDisable(true);
		BBP.setDisable(true);
		bufferTF.setDisable(true);
		oknoTF.setDisable(true);
		segmetnTF.setDisable(true);
		tosTF.setDisable(true);
		sirkapasmaTF.setDisable(true);
		



		outFormat.getSelectionModel().selectFirst();
		BBB.getSelectionModel().selectFirst();
		BBO.getSelectionModel().selectFirst();
		BBP.getSelectionModel().selectFirst();


		



	}


	public void vyberKS(ActionEvent event) {
		if (rbKlient.isSelected()) {
			adresTF.setDisable(false);
			portTF.setDisable(false);
			rbVysielByt.setDisable(false);
			rbVysielSek.setDisable(false);
			vysielanieTF.setDisable(false);
			ttlTF.setDisable(false);
			vyberKS = " -c";
		}
		if (rbServer.isSelected()) {
			pocSpojTF.setDisable(false);
			adresTF.setDisable(true);
			rbVysielByt.setDisable(true);
			rbVysielSek.setDisable(true);
			vysielanieTF.setDisable(true);
			ttlTF.setDisable(true);
			vyberKS = " -s";
	}
	}
	
	public void NoDelay(ActionEvent event) {
		if (nodelay.isSelected()) {

			nodelayHod = " -N " ;
		} else {
			nodelayHod = "";
		}
		}
	
	public void ToF(ActionEvent event) {
		if (tofCB.isSelected()) {
			tosTF.setDisable(false);
			tosTF.setText(" -S 0x0");
		} else {
			nodelayHod = "";
			tosTF.setDisable(true);
			tosTF.setText("");
		}
		}
	
	public void UDP_SP(ActionEvent event) {
		if (UDP.isSelected()) {
			udpHod = " -u";
		} else {
			udpHod = "";
		}
		if (sirkapasma.isSelected()){
			sirkapasmaTF.setDisable(false);	
			BBP.setDisable(false);
			sirkapasmaHod = " -b "+ sirkapasmaTF.getText()+BBP.getValue();
		} else {
			sirkapasmaHod= "";
		}
		}
	

	public void vyberVysielania(ActionEvent event) {
		if (rbVysielByt.isSelected()) {

			vyberVysielan = " -n " ;
		}

		if (rbVysielSek.isSelected())
			vyberVysielan = " -t ";
		}

	public void vyberTCP(ActionEvent event) {
		if(buffer.isSelected()){
			BBB.setDisable(false);
			bufferTF.setDisable(false);
			bufferHod = " -l " + bufferTF.getText()+ BBB.getValue();

		} else {
			BBB.setDisable(true);
			bufferTF.setDisable(true);
			bufferHod = "";
		}

		if(okno.isSelected()){
			BBO.setDisable(false);
			oknoTF.setDisable(false);
			oknoHod = " -w " + oknoTF.getText()+ BBO.getValue();
		} else {
			BBO.setDisable(true);
			oknoTF.setDisable(true);
			oknoHod = "";
		}

		if(segment.isSelected()){
		
			segmetnTF.setDisable(false);
			segmentHod = " -M " + segmetnTF.getText();
		} else {
			
			segmetnTF.setDisable(true);
			segmentHod = "";
		}
	}


	
	
	public void prikaz(ActionEvent event) {
		if (rbKlient.isSelected()) {

			prikazTF.setText("iperf\\iperf3.exe"+ vyberKS +  " " + adresTF.getText()+ " -P 1"  + " -i " +reportIntTF.getText()
                    + " -p " +portTF.getText() + bufferHod+ segmentHod +  oknoHod + " -f " + outFormat.getValue() + vyberVysielan + vysielanieTF.getText()+ nodelayHod + tosTF.getText()
                    + sirkapasmaHod + udpHod);
		}
		if (rbServer.isSelected()) {
			prikazTF.setText("iperf\\iperf3.exe"+ vyberKS + " -i " +reportIntTF.getText()
                    + " -p " +portTF.getText() + bufferHod + segmentHod +  oknoHod + " -f " + outFormat.getValue() + tosTF.getText()+ sirkapasmaHod + udpHod);
		}
	}


    public void SpustiButton2(ActionEvent event) {

        ExecuteShellComand obj = new ExecuteShellComand();

        if (rbKlient.isSelected()) {
            String command = "iperf\\iperf3.exe"+ vyberKS +  " " + adresTF.getText()+ " -P 1"  + " -i " +reportIntTF.getText()
                    + " -p " +portTF.getText() + bufferHod+ segmentHod +  oknoHod + " -f " + outFormat.getValue() + vyberVysielan + vysielanieTF.getText()+ nodelayHod + tosTF.getText()
                    + sirkapasmaHod + udpHod;
            String output = obj.executeCommand(command);
			vystup.setText(output);
			System.out.println(output);
			//vystup.get();
        }

        if (rbServer.isSelected()){
            String command ="iperf\\iperf3.exe"+ vyberKS + " -i " +reportIntTF.getText()
                    + " -p " +portTF.getText() + bufferHod + segmentHod +  oknoHod + " -f " + outFormat.getValue() + tosTF.getText()+ sirkapasmaHod + udpHod;
            String output = obj.executeCommand(command);
			vystup.setText(output);
			System.out.println(output);
        }
    }
	
	

	public  void Zapis(ActionEvent event)
	{
		try{
			// Create file
			FileWriter fstream = new FileWriter("Vypis merania.txt");
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(vystup.getText());
			//Close the output stream
			out.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	public void Vymaz(ActionEvent event){
		vystup.clear();
	}
	
	public void Zavri(ActionEvent event){
		 Platform.exit();
	}
	
	public void graf(ActionEvent event){
		

		XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
		series.getData().add(new XYChart.Data<String, Number>("nikdz",20));
		series.getData().add(new XYChart.Data<String, Number>("Zlte" ,30));
		series.getData().add(new XYChart.Data<String, Number>("Muhehe",50));
		Graf.getData().add(series);
			}


	}

	
	


