package ctbrec.ui.settings;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ctbrec.Config;
import ctbrec.Model;
import ctbrec.ui.controls.Dialogs;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class IgnoreListDialog extends Dialog<Void> {

    private static final transient Logger LOG = LoggerFactory.getLogger(IgnoreListDialog.class);

    private Scene parent;
    private ListView<Model> ignoreList;

    public IgnoreListDialog(Scene parent) {
        this.parent = parent;
        createGui();
        loadIgnoredModels();
    }

    private void createGui() {
        setTitle("Ignore List");
        getDialogPane().getButtonTypes().addAll(ButtonType.OK);
        initModality(Modality.APPLICATION_MODAL);
        setResizable(true);
        InputStream icon = Dialogs.class.getResourceAsStream("/icon.png");
        Stage stage = (Stage) getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(icon));
        if(parent != null) {
            stage.getScene().getStylesheets().addAll(parent.getStylesheets());
        }

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        ignoreList = new ListView<>();
        ignoreList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        ignoreList.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
            if (event.getCode() == KeyCode.DELETE) {
                removeSelectedModels();
            }
        });
        grid.add(ignoreList, 0, 0);

        Button remove = new Button("Remove");
        remove.setOnAction(evt -> removeSelectedModels());
        grid.add(remove, 0, 1);

        getDialogPane().setContent(grid);
        setResizable(true);
    }

    private void removeSelectedModels() {
        List<Model> selectedModels = ignoreList.getSelectionModel().getSelectedItems();
        if(selectedModels.isEmpty()) {
            return;
        } else {
            Config.getInstance().getSettings().modelsIgnored.removeAll(selectedModels);
            ignoreList.getItems().removeAll(selectedModels);
            LOG.debug(Config.getInstance().getSettings().modelsIgnored.toString());
            try {
                Config.getInstance().save();
            } catch (IOException e) {
                LOG.warn("Couldn't save config", e);
            }
        }
    }

    private void loadIgnoredModels() {
        List<Model> ignored = Config.getInstance().getSettings().modelsIgnored;
        ignoreList.getItems().addAll(ignored);
        Collections.sort(ignoreList.getItems());
    }
}
