/*
 * Copyright (C) 2018 Tom Almy
 *
 * 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/>.
 */
package us.almy.jelbumpro;

import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.UIManager;

/**
 *
 * @author tom
 */
public class JelbumPro extends javax.swing.JFrame {
JumbleWordPanel[] panels;
final String[] panelNames = {"First Word", "Second Word", "Third Word", "Fourth Word", "Fifth Word", "Sixth Word"};
LinkedList<Integer> finalPattern = new LinkedList<>();

    /**
     * Creates new form JelbumPro
     */
    public JelbumPro() {
        initComponents();
        panels = new JumbleWordPanel[] {jumbleWordPanel2, jumbleWordPanel3, jumbleWordPanel1, jumbleWordPanel6,
        jumbleWordPanel4, jumbleWordPanel5};
        for (int i = 0; i < panels.length; i++) {
            panels[i].setLabel(panelNames[i]);
        }
    }

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

        jumbleWordPanel2 = new us.almy.jelbumpro.JumbleWordPanel();
        jumbleWordPanel1 = new us.almy.jelbumpro.JumbleWordPanel();
        jumbleWordPanel4 = new us.almy.jelbumpro.JumbleWordPanel();
        jumbleWordPanel3 = new us.almy.jelbumpro.JumbleWordPanel();
        jumbleWordPanel6 = new us.almy.jelbumpro.JumbleWordPanel();
        jumbleWordPanel5 = new us.almy.jelbumpro.JumbleWordPanel();
        patternField = new javax.swing.JTextField();
        solveButton = new javax.swing.JButton();
        finalButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        answerTextArea = new javax.swing.JTextArea();
        jumbleText = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jelbum Pro");

        solveButton.setText("Solve Words");
        solveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                solveButtonActionPerformed(evt);
            }
        });

        finalButton.setText("Solve");
        finalButton.setEnabled(false);
        finalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finalButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Pattern");

        answerTextArea.setEditable(false);
        answerTextArea.setColumns(20);
        answerTextArea.setRows(5);
        jScrollPane1.setViewportView(answerTextArea);

        jumbleText.setText("????");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jumbleWordPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jumbleWordPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE))
                    .addComponent(jumbleText, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(patternField, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jumbleWordPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jumbleWordPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(finalButton)
                            .addComponent(solveButton)
                            .addComponent(jumbleWordPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(clearButton))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(jumbleWordPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(370, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jumbleWordPanel3, 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.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jumbleWordPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jumbleWordPanel6, 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.LEADING)
                            .addComponent(jumbleWordPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jumbleWordPanel5, 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(jLabel1)
                            .addComponent(patternField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jumbleText))
                    .addComponent(solveButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(finalButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(clearButton)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(jumbleWordPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(474, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        for (JumbleWordPanel panel : panels) {
            panel.clearPanel();
        }
        patternField.setText("");
        answerTextArea.setText("");
        finalButton.setEnabled(false);
    }//GEN-LAST:event_clearButtonActionPerformed

    private void solveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_solveButtonActionPerformed
        boolean verified = true;
        int selectedCharacters = 0;
        for (JumbleWordPanel panel : panels) {
            verified &= panel.verifyData();
            selectedCharacters += panel.selectCount;
        }
        if (!verified) return;
        
        finalPattern.clear();
        int count = 0;
        int patternLength = 0;
        String rawPattern = patternField.getText();
        for (int i = rawPattern.length()-1; i >= 0; i--) {
            boolean c = rawPattern.charAt(i) != ' ';
            if (c) { // pattern character
                count++;
            } else if (count > 0) {
                finalPattern.addFirst(count);
                patternLength += count;
                count = 0;
            } else {
                // ignore extra spaces
            }
        }
        if (count > 0) {
            finalPattern.addFirst(count);
            patternLength += count;
        }
        
        if (patternLength != selectedCharacters) {
            answerTextArea.setText("pattern length must match selected characters");
            return;
        }
        answerTextArea.setText("");
        
        boolean solved = true;
        for (JumbleWordPanel panel : panels) {
            solved &= panel.process();
        }
        if (solved) finalButton.setEnabled(true);
    }//GEN-LAST:event_solveButtonActionPerformed

    private void finalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finalButtonActionPerformed
        JelbumSolver solver = new JelbumSolver();
        solver.readWords();
        StringBuilder sb = new StringBuilder();
        LinkedList <String> answerText = new LinkedList<>();
        for (JumbleWordPanel panel : panels) {
            sb.append(panel.getSelectedSolution());
        }
        AWord finalWord = solver.makeAWord(sb.toString());
        jumbleText.setText(sb.toString());
        solver.solve("", finalPattern, finalWord, answerText);
        if (answerText.isEmpty()) {
            answerTextArea.setText("NO SOLUTION FOUND");            
        } else {
            Iterator it = answerText.iterator();
            while (it.hasNext()) {
                answerTextArea.append((String)it.next() + "\n");
            }
        }
        
    }//GEN-LAST:event_finalButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
             try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }
        ;

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JelbumPro().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea answerTextArea;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton finalButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jumbleText;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel1;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel2;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel3;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel4;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel5;
    private us.almy.jelbumpro.JumbleWordPanel jumbleWordPanel6;
    private javax.swing.JTextField patternField;
    private javax.swing.JButton solveButton;
    // End of variables declaration//GEN-END:variables

}
