package project_3;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class PrintHash {
	
	/*This Class print hashTable Information on GUI*/

	private HashTable hashTable;

	Stage stage = new Stage();
	GridPane pane =new GridPane();

	private TableView<City> table = new TableView<City>();
	private TableColumn<City, String> nameColoum =new TableColumn<City, String>("Name");
	private TableColumn<City, Double> noColoum = new TableColumn<City, Double>("No of Tourists");
	private Label tableSize ;
	private Button tabelFun = new Button("Table Function");
	Button print = new Button("Print On File");
	Label l = new Label();

	public PrintHash(HashTable hashTable) {
		super();
		this.hashTable = hashTable;
	}
	public void show(){

		stage.initModality(Modality.APPLICATION_MODAL);

		pane.setAlignment(Pos.CENTER);
		//pane.setHgap(0);
		pane.setVgap(15);

		nameColoum.setCellValueFactory(new PropertyValueFactory<>("name"));
		nameColoum.setMinWidth(200);

		noColoum.setCellValueFactory(new PropertyValueFactory<>("touristNo"));
		noColoum.setMinWidth(200);
		
	
		tableSize	= new Label("Table size : " + hashTable.getSize());
		l.setFont(Font.font("verdana",FontWeight.BOLD,12));
		l.setTextFill(Paint.valueOf("gold"));
		tabelFun.setOnAction(e->{
		
			l.setText("Hash Function :\n "+hashTable.getUsedFun());

		});
		tableSize.setTextFill(Paint.valueOf("#ff00ff"));
		tableSize.setFont(Font.font("verdana",FontWeight.BOLD,20));
		
		
		tabelFun.setFont(Font.font("verdana",FontWeight.BOLD,10));
		print.setFont(Font.font("verdana",FontWeight.BOLD,10));		
		print.setOnAction(e->{
			Alert alert = new Alert(AlertType.INFORMATION);
			new PrintOnFile(hashTable).print();
			alert.setContentText("successfully Printed :)");
			alert.show();
		});
		//get();
		table.setItems(get());
		table.getColumns().addAll(nameColoum,noColoum);
		pane.add(tableSize, 0, 0);
		pane.add(tabelFun, 0, 2);
		pane.add(table,0, 1);
		pane.add(print,1 ,2);
		pane.add(l, 0, 3);
		Scene s = new Scene(pane,750,600);
		 s.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());
		stage.setScene(s);
		stage.setTitle("Hash Information");
		stage.show();

	}
	public  ObservableList<City> get(){
		ObservableList<City> array = FXCollections.observableArrayList();

		for (int i = 0; i <hashTable.getSize() ; i++) {
			if(hashTable.getData(i) == null){
				array.add(null);
			}
			else
				array.add((City)(((HashNode)hashTable.getData(i)).getData()));
		}


		return array;

	}


}
