/*
 * #%L
 * gitools-ui-app
 * %%
 * Copyright (C) 2013 Universitat Pompeu Fabra - Biomedical Genomics group
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package org.gitools.ui.analysis.groupcomparison.wizard;

import org.gitools.core.analysis.groupcomparison.GroupComparisonAnalysis;
import org.gitools.core.matrix.model.IMatrixLayer;
import org.gitools.core.matrix.model.IMatrixLayers;
import org.gitools.core.stats.mtc.BenjaminiHochbergFdr;
import org.gitools.core.stats.mtc.Bonferroni;
import org.gitools.core.stats.mtc.MTC;
import org.gitools.core.stats.test.MannWhitneyWilxoxonTest;
import org.gitools.core.stats.test.Test;
import org.gitools.ui.IconNames;
import org.gitools.ui.platform.IconUtils;
import org.gitools.ui.platform.wizard.AbstractWizardPage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @noinspection ALL
 */
public class GroupComparisonSelectAttributePage extends AbstractWizardPage {


    private static class TestElement {
        public final Test test;

        public TestElement(Test test) {
            this.test = test;
        }

        @Override
        public String toString() {
            return this.test.getName();
        }

        public Test getTest() {
            return this.test;
        }
    }

    private static class MTCElement {
        public final MTC mtc;

        public MTCElement(MTC mtc) {
            this.mtc = mtc;
        }

        @Override
        public String toString() {
            return this.mtc.getName();
        }

        public MTC getMTC() {
            return this.mtc;
        }
    }

    public class AttrOption {
        private String name;
        private IMatrixLayer attr;

        /**
         * @noinspection UnusedDeclaration
         */
        public AttrOption(String name) {
            this.name = name;
        }

        public AttrOption(IMatrixLayer attr) {
            this.attr = attr;
        }

        public IMatrixLayer getAttr() {
            return attr;
        }

        @Override
        public String toString() {
            return attr != null ? attr.getName() : name;
        }
    }

    public GroupComparisonSelectAttributePage() {
        super();

        initComponents();

        setTitle("Select data and statistical test");

        setLogo(IconUtils.getImageIconResourceScaledByHeight(IconNames.LOGO_METHOD, 96));

        setComplete(true);

        testCbox.setModel(new DefaultComboBoxModel(new TestElement[]{new TestElement(new MannWhitneyWilxoxonTest())}));

        mtcCb.setModel(new DefaultComboBoxModel(new MTCElement[]{new MTCElement(new BenjaminiHochbergFdr()), new MTCElement(new Bonferroni())}));

        columnGroupingCb.setModel(new DefaultComboBoxModel(GroupComparisonAnalysis.getColumnGroupingMethods()));
    }

    public Test getTest() {
        TestElement testElement = (TestElement) testCbox.getModel().getSelectedItem();
        return testElement.getTest();
    }

    public MTC getMtc() {
        MTCElement mtcElement = (MTCElement) mtcCb.getModel().getSelectedItem();
        return mtcElement.getMTC();
    }

    @NotNull
    public String getColumnGrouping() {
        return (String) columnGroupingCb.getSelectedItem();
    }

    /**
     * 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() {

        applyGroup = new javax.swing.ButtonGroup();
        attributeLabel = new javax.swing.JLabel();
        attributeCb = new javax.swing.JComboBox();
        testCbox = new javax.swing.JComboBox();
        attributeLabel1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        mtcCb = new javax.swing.JComboBox();
        attributeLabel2 = new javax.swing.JLabel();
        columnGroupingCb = new javax.swing.JComboBox();
        attributeLabel3 = new javax.swing.JLabel();
        attributeLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        attributeLabel.setText("Take values to compare from:");

        testCbox.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Mann-Whitney-Wilcoxon"}));
        testCbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testCboxActionPerformed(evt);
            }
        });

        attributeLabel1.setText("Select Test");

        jLabel1.setText("Multiple test correction");

        mtcCb.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Bonferroni", "Benjamini Hochberg FDR"}));
        mtcCb.setSelectedIndex(1);

        attributeLabel2.setText("Group columns by:");

        attributeLabel3.setText("Data:");

        attributeLabel4.setText("Statistics:");

        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(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(attributeLabel2).addComponent(attributeLabel)).addGap(45, 45, 45).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(columnGroupingCb, 0, 275, Short.MAX_VALUE).addComponent(attributeCb, javax.swing.GroupLayout.Alignment.LEADING, 0, 275, Short.MAX_VALUE))).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1).addComponent(attributeLabel1)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(testCbox, 0, 349, Short.MAX_VALUE).addComponent(mtcCb, 0, 349, Short.MAX_VALUE))).addComponent(attributeLabel4).addComponent(attributeLabel3)).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(attributeLabel3).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(attributeLabel).addComponent(attributeCb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(attributeLabel2).addComponent(columnGroupingCb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(attributeLabel4).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(testCbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(attributeLabel1)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(mtcCb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void testCboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testCboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_testCboxActionPerformed

    @NotNull
    @Override
    public JComponent createControls() {
        return this;
    }


    public void setAttributes(@Nullable IMatrixLayers attrs) {

        if (attrs != null) {
            AttrOption[] attrOptions = new AttrOption[attrs.size()];

            for (int i = 0; i < attrs.size(); i++)
                attrOptions[i] = new AttrOption(attrs.get(i));

            attributeCb.setModel(new DefaultComboBoxModel(attrOptions));
            attributeCb.setSelectedIndex(0);
            attributeCb.setEnabled(true);
            attributeCb.setVisible(true);
            attributeLabel.setVisible(true);
        } else {
            dissableAttrCb();
        }
    }

    private void dissableAttrCb() {
        attributeCb.setModel(new DefaultComboBoxModel());
        attributeCb.setEnabled(false);
        attributeCb.setVisible(false);
        attributeLabel.setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup applyGroup;
    private javax.swing.JComboBox attributeCb;
    private javax.swing.JLabel attributeLabel;
    private javax.swing.JLabel attributeLabel1;
    private javax.swing.JLabel attributeLabel2;
    private javax.swing.JLabel attributeLabel3;
    private javax.swing.JLabel attributeLabel4;
    private javax.swing.JComboBox columnGroupingCb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JComboBox mtcCb;
    private javax.swing.JComboBox testCbox;
    // End of variables declaration//GEN-END:variables

    public int getAttributeIndex() {
        return attributeCb.getSelectedIndex();
    }

}
