package fi.helsinki.cs.tmc.intellij.ui.submissionresult.feedback;

import fi.helsinki.cs.tmc.core.domain.submission.FeedbackAnswer;
import fi.helsinki.cs.tmc.core.domain.submission.FeedbackQuestion;

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

import java.awt.Font;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.swing.JLabel;
import javax.swing.JTextField;



public class IntRangeQuestionPanel extends FeedbackQuestionPanel {

    private static final Logger logger = LoggerFactory.getLogger(IntRangeQuestionPanel.class);
    private FeedbackQuestion question;
    private int naValue;

    public IntRangeQuestionPanel(FeedbackQuestion question) {
        logger.info(
                "Creating int range question panel for submission feedback window. "
                        + "@IntRangeQuestionPanel");
        if (!question.isIntRange()) {
            throw new IllegalArgumentException("Invalid panel for question type");
        }

        this.question = question;

        initComponents();
        this.questionLabel.setText(question.getQuestion());
        setUpValueSlider(question);
    }

    private void setUpValueSlider(FeedbackQuestion question) {
        logger.info(
                "Setting up value slider for IntRangeQuestionPanel. " + "@IntRangeQuestionPanel");
        naValue = question.getIntRangeMin() - 1;
        valueSlider.setMinimum(naValue);
        valueSlider.setMaximum(question.getIntRangeMax());

        Dictionary<Integer, JLabel> labelTable = new Hashtable<>();

        JLabel naLabel = new JLabel("-");
        naLabel.setFont(naLabel.getFont().deriveFont(Font.PLAIN));
        labelTable.put(naValue, naLabel);

        setFeedbackValues(labelTable, naLabel);

        valueSlider.setLabelTable(labelTable);
        valueSlider.setValue(naValue);
    }

    private void setFeedbackValues(Dictionary<Integer, JLabel> labelTable, JLabel labelForFont) {
        logger.info("Setting up feedback values for value slider. " + "@IntRangeQuestionPanel");

        for (int i = question.getIntRangeMin(); i <= question.getIntRangeMax(); ++i) {
            JLabel label = new JLabel("" + i);
            label.setFont(labelForFont.getFont().deriveFont(Font.BOLD));
            labelTable.put(i, label);
        }
    }

    @Override
    public FeedbackAnswer getAnswer() {
        logger.info("Getting feedback answer value. @IntRangeQuestionPanel");
        int sliderValue = valueSlider.getValue();

        if (sliderValue == naValue) {
            return null;
        }

        return new FeedbackAnswer(question, Integer.toString(sliderValue));
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        logger.info(
                "Initializing components for IntRangeQuestionPanel " + "@IntRangeQuestionPanel");

        questionLabel = new javax.swing.JLabel();
        valueSlider = new javax.swing.JSlider();

        JTextField myTextField = new JTextField("Testin popupfactory", 20);
        questionLabel.setText("Testing (intRangeQuestionPanel");
        //questionLabel.setText(org.openide.util.NbBundle.getMessage(IntRangeQuestionPanel.class, "IntRangeQuestionPanel.questionLabel.text")); // NOI18N

        valueSlider.setMaximum(10);
        valueSlider.setMinimum(1);
        valueSlider.setMinorTickSpacing(1);
        valueSlider.setPaintLabels(true);
        valueSlider.setPaintTicks(true);
        valueSlider.setSnapToTicks(true);
        valueSlider.setValue(5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .LEADING)
                                                        .addComponent(
                                                                valueSlider,
                                                                javax.swing.GroupLayout
                                                                        .DEFAULT_SIZE,
                                                                218,
                                                                Short.MAX_VALUE)
                                                        .addComponent(questionLabel))
                                        .addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout.createSequentialGroup()
                                        .addContainerGap(
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)
                                        .addComponent(questionLabel)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(
                                                valueSlider,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JLabel questionLabel;

    private javax.swing.JSlider valueSlider;

    // End of variables declaration//GEN-END:variables
}