package ctbrec.ui;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import ctbrec.GlobalThreadPool;
import ctbrec.Model;
import ctbrec.recorder.download.StreamSource;
import ctbrec.ui.controls.Dialogs;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class StreamSourceSelectionDialog {
    private static final StreamSource BEST = new BestStreamSource();

    private StreamSourceSelectionDialog() {
    }

    public static void show(Scene parent, Model model, Function<Model, Void> onSuccess, Function<Throwable, Void> onFail) {
        Task<List<StreamSource>> selectStreamSource = new Task<List<StreamSource>>() {
            @Override
            protected List<StreamSource> call() throws Exception {
                List<StreamSource> sources = model.getStreamSources();
                Collections.sort(sources);
                sources.add(BEST);
                return sources;
            }
        };
        selectStreamSource.setOnSucceeded(e -> {
            List<StreamSource> sources;
            try {
                sources = selectStreamSource.get();
                int selectedIndex = model.getStreamUrlIndex() > -1 ? Math.min(model.getStreamUrlIndex(), sources.size() - 1) : sources.size() - 1;
                ChoiceDialog<StreamSource> choiceDialog = new ChoiceDialog<>(sources.get(selectedIndex), sources);
                choiceDialog.setTitle("Stream Quality");
                choiceDialog.setHeaderText("Select your preferred stream quality");
                choiceDialog.setResizable(true);
                Stage stage = (Stage) choiceDialog.getDialogPane().getScene().getWindow();
                stage.getScene().getStylesheets().addAll(parent.getStylesheets());
                InputStream icon = Dialogs.class.getResourceAsStream("/icon.png");
                stage.getIcons().add(new Image(icon));
                Optional<StreamSource> selectedSource = choiceDialog.showAndWait();
                if (selectedSource.isPresent()) {
                    int index = -1;
                    if (selectedSource.get() != BEST) {
                        index = sources.indexOf(selectedSource.get());
                    }
                    model.setStreamUrlIndex(index);
                    onSuccess.apply(model);
                }
            } catch (ExecutionException e1) {
                onFail.apply(e1);
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
                onFail.apply(e1);
            }
        });
        selectStreamSource.setOnFailed(e -> onFail.apply(selectStreamSource.getException()));
        GlobalThreadPool.submit(selectStreamSource);
    }

    private static class BestStreamSource extends StreamSource {
        @Override
        public String toString() {
            return "Best";
        }
    }
}
