/* 
 * Diplomová práce ÚSI VUT 
 * Forenzní analýza webového prohlížeče
 *
 * Autor: Michal Bača
 * Datum: 20.5.2015
 */
package yafft;

import java.io.File;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import yafft.Utils.FFProfile;
import yafft.Utils.ImportExportOptions;

/**
 * FXML Controller class
 *
 * @author michal
 */
public class YafftImExWizardController implements Initializable {

    @FXML
    private Tab ImExWizardDirsTab;
    @FXML
    private TextField ImExWizardFFDir;
    @FXML
    private TextField ImExWizardWorkDirLabel;
    @FXML
    private Button ImExWizardWorkDir;
    @FXML
    private Tab ImExWizardOptionsTab;
    @FXML
    private HBox ImExWizardOptionsImportLabel1;
    @FXML
    private HBox ImExWizardOptionsImportLabel2;
    @FXML
    private Label optionsTypeLabel;
    @FXML
    private TextField ImExWizardExportDirTextfield;

    DirectoryChooser dirChooser;

    Boolean doExport = false;

    List<String> list;

    SingleSelectionModel<Tab> tabs;
    int tabCount;
    int selectedTab;

    Stage stage;

    YafftMainWindowController mainWindowController;

    ObservableList<String> options;
    @FXML
    private Label ImExWizardFFDirStatusLabel;
    @FXML
    private Label ImExWizardExportFFversionLabel;
    @FXML
    private ComboBox<FFProfile> ImExWizardExportProfileSelector;
    @FXML
    private TabPane ImExWizardTabs;
    @FXML
    private Button imExWizardCancelButton;
    @FXML
    private Button imExWizardNextButton;
    @FXML
    private Button imExWizardFinishButton;
    @FXML
    private Label ImExWizardFFHomeDirLabel;
    @FXML
    private CheckBox imExOptionsCookies;
    @FXML
    private CheckBox imExOptionsDownloads;
    @FXML
    private CheckBox imExOptionsForms;
    @FXML
    private CheckBox imExOptionsHistory;
    @FXML
    private CheckBox imExOptionsBookmarks;
    @FXML
    private CheckBox imExOptionsMakeCopyAndQuit;
    @FXML
    private CheckBox imExOptionsPasswords;
    @FXML
    private HBox exportDirHbox;

    public void setMainWindowController(YafftMainWindowController mainWindowController) {
        this.mainWindowController = mainWindowController;
    }

    public Boolean isExport() {
        return doExport;
    }

    public void setExport() {
        this.doExport = true;
        optionsTypeLabel.setText("Exportovat: ");
        //TODO

    }

    public void setImport() {
        this.doExport = false;
        optionsTypeLabel.setText("Importovat: ");
        //TODO
    }

    public void setStage(Stage s) {
        this.stage = s;
    }

    /**
     * Initializes the controller class.
     *
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO

        dirChooser = new DirectoryChooser();

        //exportProgress.setVisible(false);
        //exportProgress.setProgress(0.0);
        ImExWizardFFDirStatusLabel.setVisible(false);
        tabs = ImExWizardTabs.getSelectionModel();
        ImExWizardTabs.getTabs().stream().forEach((t) -> {
            t.setDisable(true);
        });
        ImExWizardTabs.getTabs().get(0).setDisable(false);
        this.selectedTab = 0;

        tabs.selectedItemProperty().addListener((tab, prev, current) -> {
            imExWizardNextButton.setDisable(false);
            //System.out.println("Tab " + current.getText() + " selected; " + previous.getText());

        });

        this.tabCount = ImExWizardTabs.getTabs().size() - 1;

        imExWizardFinishButton.setDisable(true);

    }

    public void myInit() {

        if (!this.doExport) {
            exportDirHbox.setVisible(false);
            //optionsTypeLabel.setText("IMPORT");          

        } else {
            // imExOptionsMakeCopyAndQuit.setVisible(false);
            ImExWizardDirsTab.setDisable(true);

            ImExWizardTabs.getTabs().get(1).setDisable(false);
            this.selectedTab = 1;
            ImExWizardTabs.getSelectionModel().select(1);
            //optionsTypeLabel.setText("EXPORT");

            imExOptionsCookies.setSelected(ImportExportOptions.getCookies());
            imExOptionsBookmarks.setSelected(ImportExportOptions.getBookmarks());
            imExOptionsDownloads.setSelected(ImportExportOptions.getDownloads());
            imExOptionsForms.setSelected(ImportExportOptions.getForms());
            imExOptionsHistory.setSelected(ImportExportOptions.getHistory());
            imExOptionsPasswords.setSelected(ImportExportOptions.getPasswords());

            if (ImportExportOptions.getWorkingDirectory() != null) {
                ImExWizardExportDirTextfield.setText(ImportExportOptions.getWorkingDirectory().toString());
            }

        }

        ImExWizardFFDir.setText(this.mainWindowController.getInfo().getFFPath().toString());

//        ImExWizardExportFFversionLabel.setText(Integer.toString(this.mainWindowController.getInfo().getFFProfiles().get(0).getVersionInt()));
        ObservableList<FFProfile> profiles = FXCollections.observableArrayList();
//        profiles.addAll(this.mainWindowController.getInfo().getFFProfiles());
       // ImExWizardExportProfileSelector.setItems(profiles);

        if (this.doExport == true && ImportExportOptions.getProfile() != null) {
            //this.mainWindowController.print("Pokusím se nastavit profil pro export");
          
            profiles.addAll(this.mainWindowController.getInfo().getFFProfiles());
            ImExWizardExportProfileSelector.setItems(profiles);

            FFProfile p = ImportExportOptions.getProfile();
            int index = ImportExportOptions.getProfieIndex();
            ImExWizardExportProfileSelector.getSelectionModel().select(index);
            //          ImExWizardExportProfileSelected(new ActionEvent());
            //ImExWizardExportProfileSelector.getSelectionModel().select(ImportExportOptions.getProfile());
            //ImExWizardFFHomeDirLabel.setText(ImportExportOptions.getProfile().toString());
        }

    }

    @FXML
    private void ImExWizardFFDirChanged(ActionEvent event) {
        dirChooser.setTitle("Vyberte cílový adresář");
        File selectedDir = dirChooser.showDialog(null);
        if (selectedDir != null) {
            ImExWizardFFDir.setText(selectedDir.toString());
            //check if its valid FireFox directory
            File f = new File(selectedDir + java.io.File.separator + "profiles.ini");

            if (!f.exists()) {
                ImExWizardFFDirStatusLabel.setVisible(true);
            } else {
                ImExWizardFFDirStatusLabel.setVisible(false);
                this.mainWindowController.getInfo().setFFPath(selectedDir.toPath());
                this.mainWindowController.getInfo().updateProfilePaths();

                ObservableList<FFProfile> profiles = FXCollections.observableArrayList();
                profiles.addAll(this.mainWindowController.getInfo().getFFProfiles());
                ImExWizardExportProfileSelector.setItems(profiles);

            }

        }

    }

    @FXML
    private void ImExWizardWorkDirSelected(ActionEvent event) {
        dirChooser.setTitle("Vyberte pracovní adresář");
        File selectedDir = dirChooser.showDialog(null);
        if (selectedDir != null) {
            ImExWizardWorkDirLabel.setText(selectedDir.toString());
            ImExWizardExportDirTextfield.setText(selectedDir.toString());
            ImportExportOptions.setWorkingDirectory(selectedDir.toPath());
            this.mainWindowController.setWorkDirLabel("Pracovní adresář: " + selectedDir.toPath());
        }
    }

    @FXML
    private void ImExWizardExportDirChanged(ActionEvent event) {
        dirChooser.setTitle("Vyberte cílový adresář");
        File selectedDir = dirChooser.showDialog(null);
        if (selectedDir != null) {
            ImExWizardExportDirTextfield.setText(selectedDir.toString());
            ImportExportOptions.setExportDirectory(selectedDir.toPath());
            this.mainWindowController.setWorkDirLabel("Pracovní adresář: " + selectedDir.toPath());
        }
    }

    @FXML
    private void ImExWizardExportProfileSelected(ActionEvent event) {
        int selectedIndex = ImExWizardExportProfileSelector.getSelectionModel().getSelectedIndex();
        int size = this.mainWindowController.getInfo().getFFProfiles().size();
        if (selectedIndex > (size - 1)) {
            selectedIndex = size;
        }
        FFProfile selected = this.mainWindowController.getInfo().getFFProfiles().get(selectedIndex);
        ImportExportOptions.setProfile(selected);
        ImportExportOptions.setProfieIndex(selectedIndex);
        ImExWizardFFHomeDirLabel.setText(selected.toString());

    }

    @FXML
    private void imExWizardCancelButtonClicked(ActionEvent event) {
        this.mainWindowController.print("Wizard canceled.");
        this.stage.close();
    }

    @FXML
    private void imExWizardNextButtonClicked(ActionEvent event) {
        this.tabs.selectNext();
        this.selectedTab++;
        //enable newly selected tab
        if (this.tabs.isSelected(this.selectedTab)) {
            ImExWizardTabs.getTabs().get(selectedTab).setDisable(false);
        }

        //pokud vybrán poslední
        if (this.tabs.isSelected(tabCount)) {
            imExWizardNextButton.setDisable(true);
            imExWizardFinishButton.setDisable(false);
        } else {
            imExWizardNextButton.setDisable(false);
        }

    }

    @FXML
    private void imExWizardFinishButtonClicked(ActionEvent event) {
        //this.mainWindowController.print("Wizard finished.");
        //exportProgress.setVisible(true);
      
        //save all the options
        ImportExportOptions.setBookmarks(imExOptionsBookmarks.isSelected());
        ImportExportOptions.setCookies(imExOptionsCookies.isSelected());
        ImportExportOptions.setDownloads(imExOptionsDownloads.isSelected());
        ImportExportOptions.setForms(imExOptionsForms.isSelected());
        ImportExportOptions.setHistory(imExOptionsHistory.isSelected());
        ImportExportOptions.setPasswords(imExOptionsPasswords.isSelected());

        ImportExportOptions.setCreateCopyAndExit(imExOptionsMakeCopyAndQuit.isSelected());
        ImportExportOptions.setExportDirectory(Paths.get(ImExWizardExportDirTextfield.getText()));

        ImportExportOptions.print();

        this.mainWindowController.getInfo().updateProfilePaths();

        if (this.isExport()) {
            this.mainWindowController.exportData(event);
        } else {
            this.mainWindowController.importData(event);
        }

        this.stage.close();
    }

}
