package com.brg.controller;

import com.brg.common.Config;
import com.brg.domain.DatabaseType;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;
import java.util.ResourceBundle;

public class SettingsTab implements Initializable, TabControllerImpl {

    @FXML private ComboBox<DatabaseType> exportOptions;
    @FXML private TextField targetHost, targetUsername, targetPassword, targetService, targetPort;
    private ArrayList<DatabaseType> arrDatabaseType;
    private Properties properties;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.updateContents();
    }

    @Override
    public void updateContents() {
        loadSettings();
        String selected = properties.getProperty("target_type");
        exportOptions.getItems().addAll(FXCollections.observableArrayList(getGetDatabaseType()));
        exportOptions.setValue(selectedDatabaseType(selected));

    }

    public ArrayList<DatabaseType> getGetDatabaseType() {
        arrDatabaseType = new ArrayList<DatabaseType>();

        for (DatabaseType dbt : DatabaseType.values()) {
            arrDatabaseType.add(dbt);
        }

        return arrDatabaseType;
    }

    private void loadSettings() {
        try {
            properties = Config.getInstance().getConfig();
            setSettings();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private DatabaseType selectedDatabaseType(String dbt) {
        switch (dbt) {
            case "ORACLE": return DatabaseType.ORACLE;
            case "MYSQL": return DatabaseType.MYSQL;
            default: return DatabaseType.ORACLE;
        }
    }

    private void setSettings() {
        targetHost.setText(properties.getProperty("target_host"));
        targetUsername.setText(properties.getProperty("target_username"));
        targetPassword.setText(properties.getProperty("target_password"));
        targetService.setText(properties.getProperty("target_service"));
        targetPort.setText(properties.getProperty("target_port"));
    }

    public void saveSettings() {

    }
}

@@ -1404,32 +1404,94 @@
 aseType;%0A    %7D%0A%0A
+%0A    /**%0A     * load default config properties%0A     *%0A     */%0A
     private void
@@ -1941,32 +1941,92 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * set default config properties%0A     *%0A     */%0A
     private void
@@ -2414,37 +2414,671 @@
     
-public void saveSettings() %7B%0A
+/**%0A     * save config properties%0A     *%0A     */%0A    public void saveSettings() %7B%0A        properties.setProperty(%22target_host%22, targetHost.getText());%0A        properties.setProperty(%22target_username%22, targetUsername.getText());%0A        properties.setProperty(%22target_password%22, targetPassword.getText());%0A        properties.setProperty(%22target_service%22, targetService.getText());%0A        properties.setProperty(%22target_port%22, targetPort.getText());%0A        properties.setProperty(%22target_type%22, exportOptions.getValue().toString());%0A%0A        try %7B%0A            Config.getInstance().save();%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

Save settings tab.