/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Henri Haverinen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 */

package main.java.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import main.java.helper.FileHelper;
import main.java.model.ConnectionPOJO;
import main.java.model.Context;
import main.java.model.DriverPOJO;

import java.awt.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by Henri on 18.3.2016.
 */
public class AliasWindowController {

    @FXML
    public ComboBox driverBox, aliasBox;

    @FXML
    private TextField aliasNameField, urlField, userField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private void addAlias(ActionEvent event) {
        String aliasNameFieldText = aliasNameField.getText();
        DriverPOJO driverBoxSelection = (DriverPOJO) driverBox.getSelectionModel().getSelectedItem();
        String urlFieldText = urlField.getText();
        String userFieldText = userField.getText();
        String passwordFieldText = passwordField.getText();

        if (!aliasNameFieldText.isEmpty() && driverBoxSelection != null && !urlFieldText.isEmpty() && !userFieldText.isEmpty() && !passwordFieldText.isEmpty()) {
            ConnectionPOJO cpojo = new ConnectionPOJO(aliasNameFieldText, driverBoxSelection.getDriverName(), userFieldText, passwordFieldText, urlFieldText);
            Context.getInstance().getConnections().get().add(cpojo);
            FileHelper helper = new FileHelper();
            List<ConnectionPOJO> pojos =  Context.getInstance().getConnections().get().stream().collect(Collectors.toList());
            try {
                helper.writeObjectsToJsonFile(pojos, "conf/aliases.json");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @FXML
    private void modifyAlias(ActionEvent event) {

    }

    @FXML
    private void deleteAlias(ActionEvent event) {
        ConnectionPOJO connectionPOJO = (ConnectionPOJO) aliasBox.getSelectionModel().getSelectedItem();

        if (connectionPOJO != null) {
            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
            alert.setTitle("Delete confirmation");
            alert.setHeaderText("Are you sure you want to delete alias '" + connectionPOJO.getAliasName() + "'");
            alert.setContentText("If you are, just hit the OK!");
            Optional<ButtonType> result = alert.showAndWait();

            if (result.get() == ButtonType.OK) {
                Context.getInstance().getConnections().get().remove(connectionPOJO);
                FileHelper helper = new FileHelper();
                List<ConnectionPOJO> pojos =  Context.getInstance().getConnections().get().stream().collect(Collectors.toList());
                try {
                    helper.writeObjectsToJsonFile(pojos, "conf/aliases.json");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @FXML
    private void selectAlias(ActionEvent event) {
        ConnectionPOJO connectionPOJO = (ConnectionPOJO) aliasBox.getSelectionModel().getSelectedItem();
        aliasNameField.setText(connectionPOJO.getAliasName());
        //driverbox
        urlField.setText(connectionPOJO.getDatabaseUrl());
        userField.setText(connectionPOJO.getUsername());
        passwordField.setText(connectionPOJO.getPassword());
    }

}

@@ -1754,152 +1754,19 @@
 if (
-!aliasNameFieldText.isEmpty() && driverBoxSelection != null && !urlFieldText.isEmpty() && !userFieldText.isEmpty() && !passwordFieldText.isEmpty
+checkFields
 ()) 
@@ -2256,32 +2256,92 @@
 aliases.json%22);%0A
+                aliasBox.getSelectionModel().select(cpojo);%0A
             %7D ca
@@ -2488,16 +2488,1386 @@
 vent) %7B%0A
+        ConnectionPOJO connectionPOJO = (ConnectionPOJO) aliasBox.getSelectionModel().getSelectedItem();%0A%0A        if (connectionPOJO != null && checkFields()) %7B%0A            Alert alert = new Alert(Alert.AlertType.CONFIRMATION);%0A            alert.setTitle(%22Modify confirmation%22);%0A            alert.setHeaderText(%22Are you sure you want to modify alias '%22 + connectionPOJO.getAliasName() + %22'%22);%0A            alert.setContentText(%22If you are, just hit the OK!%22);%0A            Optional%3CButtonType%3E result = alert.showAndWait();%0A%0A            if (result.get() == ButtonType.OK) %7B%0A                connectionPOJO.setAliasName(aliasNameField.getText());%0A                connectionPOJO.setDatabaseUrl(urlField.getText());%0A                connectionPOJO.setDriver(((DriverPOJO) driverBox.getSelectionModel().getSelectedItem()).getDriverName());%0A                connectionPOJO.setUsername(userField.getText());%0A                connectionPOJO.setPassword(userField.getText());%0A%0A                FileHelper helper = new FileHelper();%0A                List%3CConnectionPOJO%3E pojos =  Context.getInstance().getConnections().get().stream().collect(Collectors.toList());%0A                try %7B%0A                    helper.writeObjectsToJsonFile(pojos, %22conf/aliases.json%22);%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D
 %0A    %7D%0A%0A
@@ -5413,10 +5413,303 @@
 %0A    %7D%0A%0A
+    private boolean checkFields() %7B%0A        return (!aliasNameField.getText().isEmpty() && driverBox.getSelectionModel().getSelectedItem()%0A                != null && !urlField.getText().isEmpty() && !userField.getText().isEmpty()%0A                && !passwordField.getText().isEmpty());%0A    %7D%0A%0A
 %7D%0A

Support modifying existing aliases