package com.gadelkareem.awsclient.application;


import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.Tag;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import java.util.ArrayList;
import java.util.List;


public class Controller {

    public TableView tableView;


    //INITIALIZE
    @FXML
    void initialize() {
        listEc2();
    }

    private void listEc2() {

        AWSCredentials awsCredentials = new DefaultAWSCredentialsProviderChain().getCredentials();
        AmazonEC2 amazonEC2 = new AmazonEC2Client(awsCredentials);
        amazonEC2.setRegion(Region.getRegion(Regions.EU_WEST_1));

        List<ObservableList<StringProperty>> rows = new ArrayList<ObservableList<StringProperty>>();
        List<String> columns = new ArrayList<String>();

        columns.add("Instance ID");
        columns.add("Group");
        columns.add("Instance Type");
        columns.add("Instance State");
        columns.add("Public IP");
        columns.add("Private IP");


        reservations:
        {
            for (Reservation reservation :
                    amazonEC2.describeInstances(new DescribeInstancesRequest()).getReservations()) {
                for (Instance instance : reservation.getInstances()) {

                    ObservableList<StringProperty> row = FXCollections.observableArrayList();

                    row.add(new SimpleStringProperty(instance.getInstanceId()));
                    row.add(new SimpleStringProperty(instance.getSecurityGroups().get(0).getGroupName()));
                    row.add(new SimpleStringProperty(instance.getInstanceType()));
                    row.add(new SimpleStringProperty(instance.getState().getName()));
                    row.add(new SimpleStringProperty(instance.getPublicIpAddress()));
                    row.add(new SimpleStringProperty(instance.getPrivateIpAddress()));

                    for (Tag tag : instance.getTags()) {
                        if (tag.getKey().equals("Name")) {
                            if (!columns.contains(tag.getKey()))
                                columns.add(0, tag.getKey());
                            row.add(0, new SimpleStringProperty(tag.getValue()));
                        } else {
                            if (!columns.contains("Tag::" + tag.getKey()))
                                columns.add("Tag::" + tag.getKey());
                            row.add(new SimpleStringProperty(tag.getValue()));
                        }
                    }
                    rows.add(row);
//                    break reservations;
                }
            }
        }


        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
            tableView.getColumns().addAll(createColumn(columnIndex, columns.get(columnIndex)));
        }

        tableView.getItems().addAll(rows);
    }

    private TableColumn<ObservableList<StringProperty>, String> createColumn(
            final int columnIndex,
            String columnTitle
    ) {

        TableColumn<ObservableList<StringProperty>, String> column = new TableColumn<ObservableList<StringProperty>, String>();
        column.setText(columnTitle);

        column.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<StringProperty>, String>, ObservableValue<String>>() {
            public ObservableValue<String> call(
                    TableColumn.CellDataFeatures<ObservableList<StringProperty>, String> cellDataFeatures) {
                ObservableList<StringProperty> values = cellDataFeatures.getValue();
                if (columnIndex >= values.size()) {
                    return new SimpleStringProperty("");
                } else {
                    return cellDataFeatures.getValue().get(columnIndex);
                }
            }
        });
        return column;
    }


}



@@ -1496,24 +1496,102 @@
 String%3E();%0A%0A
+        String firstColumnKey = %22Name%22;%0A%0A        columns.add(firstColumnKey);%0A
         colu
@@ -1610,24 +1610,24 @@
 tance ID%22);%0A
-
         colu
@@ -1788,24 +1788,67 @@
 ate IP%22);%0A%0A%0A
+        boolean hasFirstColumnKey = false;%0A
         rese
@@ -2174,24 +2174,83 @@
 rayList();%0A%0A
+                    row.add(new SimpleStringProperty(%22%22));%0A
             
@@ -2764,24 +2764,25 @@
 dress()));%0A%0A
+%0A
             
@@ -2874,22 +2874,30 @@
 .equals(
-%22Name%22
+firstColumnKey
 )) %7B%0A   
@@ -2925,37 +2925,51 @@
     
-if (!columns.contains
+row.set(0, new SimpleStringProperty
 (tag.get
 Key(
@@ -2960,31 +2960,34 @@
 erty(tag.get
-Key
+Value
 ()))
+;
 %0A           
@@ -3007,122 +3007,32 @@
     
-    columns.add(0, tag.getKey());%0A                            row.add(0, new SimpleStringProperty(tag.getValue()))
+hasFirstColumnKey = true
 ;%0A  
@@ -3453,16 +3453,262 @@
      %7D%0A%0A
+        if (!hasFirstColumnKey) %7B%0A            columns.remove(0);%0A            for (ObservableList row : rows) %7B%0A                row.remove(0);%0A            %7D%0A        %7D%0A%0A%0A        tableView.getItems().clear();%0A        tableView.getColumns().clear();%0A
 %0A       

handle empty tag name