package be.iminds.ilabt.jfed.experimenter_gui.slice;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.experiment.SfaExperimentPart;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import org.controlsfx.glyphfont.FontAwesome;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: twalcari
 * Date: 1/11/2016
 * Time: 12:07
 */
public class QoeDialog extends Dialog<List<QoeDialog.QoeReply>> {

    final List<ExperimentPartQoe> experimentPartQoes = new ArrayList<>();

    public QoeDialog(Experiment experiment, Collection<SfaExperimentPart> experimentParts) {
        setTitle("Rate this experiment");
        setHeaderText(String.format("Please rate your experience while using '%s'", experiment.getName()));

        setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.STAR, Color.GOLD));

        for (SfaExperimentPart experimentPart : experimentParts) {
            experimentPartQoes.add(new ExperimentPartQoe(experimentPart));
        }

        TilePane content = new TilePane(10, 10);

        int prefColumns;
        if (experimentParts.size() > 6)
            prefColumns = 3;
        else if (experimentParts.size() > 2)
            prefColumns = 2;
        else
            prefColumns = 1;
        content.setPrefColumns(prefColumns);

        content.getChildren().addAll(experimentPartQoes);
        getDialogPane().setContent(content);

        getDialogPane().getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL);


        setResultConverter(param -> {
            if (param != ButtonType.OK)
                return null;

            return experimentPartQoes.stream().map(ExperimentPartQoe::getResult).collect(Collectors.toList());
        });
    }


    public static class QoeReply {

        private final SfaExperimentPart experimentPart;
        private final int overallScore;
        private final int qualityScore;
        private final int availabilityScore;


        public QoeReply(SfaExperimentPart experimentPart, int overallScore, int qualityScore, int availabilityScore) {
            this.experimentPart = experimentPart;
            this.overallScore = overallScore;
            this.qualityScore = qualityScore;
            this.availabilityScore = availabilityScore;
        }

        public SfaExperimentPart getExperimentPart() {
            return experimentPart;
        }

        public int getOverallScore() {
            return overallScore;
        }

        public int getQualityScore() {
            return qualityScore;
        }

        public int getAvailabilityScore() {
            return availabilityScore;
        }
    }


}
