/*
 *  RPGeneration - A role playing utility generate interesting things
 *  Copyright (C) 2002 Devon D Jones
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 * GeneratorPanel.java
 *
 * Created on October 25, 2002, 3:57 PM
 */

package org.evilsoft.gaming.RPGeneration;

import javax.swing.*;
import java.util.*;

/**
 *
 * @author  devon
 */
public class GeneratorPanel extends javax.swing.JPanel {

    /** Creates new form GeneratorPanel */
    public GeneratorPanel(PersonalityFrame parent, HashMap tables) {
        this.tables = tables;
        this.parent = parent;
        initComponents();
        initRest(new DefaultTopPanel(), "Personality");
    }

    public GeneratorPanel(PersonalityFrame parent, HashMap tables, String directory) {
        this.tables = tables;
        this.parent = parent;
        initComponents();
        initRest(new DefaultTopPanel(), directory);
    }

    public GeneratorPanel(PersonalityFrame parent, HashMap tables, TopPanel newTopPanel, String directory) {
        this.tables = tables;
        this.parent = parent;
        initComponents();
        initRest(newTopPanel, directory);
    }

    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jSplitPane2 = new javax.swing.JSplitPane();
        bottomPanel = new javax.swing.JPanel();
        outputTabbedPane = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        mainOutput = new javax.swing.JTextPane();
        jToolBar = new javax.swing.JToolBar();
        bGenerate = new javax.swing.JButton();
        bClear = new javax.swing.JButton();
        topPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jSplitPane2.setDividerLocation(145);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        bottomPanel.setLayout(new java.awt.BorderLayout());

        outputTabbedPane.setPreferredSize(new java.awt.Dimension(67, 300));
        mainOutput.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                mainOutputCaretUpdate(evt);
            }
        });

        jScrollPane2.setViewportView(mainOutput);

        outputTabbedPane.addTab("Notes", jScrollPane2);

        bottomPanel.add(outputTabbedPane, java.awt.BorderLayout.CENTER);

        bGenerate.setText("Generate");
        bGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGenerateActionPerformed(evt);
            }
        });

        jToolBar.add(bGenerate);

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

        jToolBar.add(bClear);

        bottomPanel.add(jToolBar, java.awt.BorderLayout.SOUTH);

        jSplitPane2.setRightComponent(bottomPanel);

        topPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jSplitPane2.setLeftComponent(topPanel);

        add(jSplitPane2, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void mainOutputCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_mainOutputCaretUpdate
        // Add your handling code here:
        int dot = evt.getDot();
        JTextPane textPane = getCurrentJTextPane();
        parent.updateButtons(textPane, dot);
    }//GEN-LAST:event_mainOutputCaretUpdate

    private void bClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bClearActionPerformed
        if(outputTabbedPane.getSelectedIndex() != 0) {
            outputTabbedPane.remove(outputTabbedPane.getSelectedIndex());
        }
        else {
            mainOutput.setText("");
        }
    }//GEN-LAST:event_bClearActionPerformed

    private void bGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGenerateActionPerformed
        int effect = 0;
        personalities.initialize();
        personalities.doOperation(top.getOperations());
        javax.swing.JTextPane outputArea = new javax.swing.JTextPane();
        outputArea.setEditorKit(outputArea.getEditorKitForContentType("text/html"));
        
        outputArea.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                mainOutputCaretUpdate(evt);
            }
        });
        
        String title = "No Name";
        String content = "";

        if(top.getName() != "" && top.getName() != null) {
            title = top.getName();
        }
        outputTabbedPane.addTab(title, new javax.swing.JScrollPane(outputArea));
        outputTabbedPane.setSelectedIndex(outputTabbedPane.getTabCount() - 1);

        if(title != "") {
            content = "<HTML><BODY><font size=+2><b>" + title + "</b></font>";
        }
        for(int i = 0; i < personalities.size(); i++) {
            Trait trait = (Trait)personalities.get(i);
            ArrayList traits = trait.getTraits();
            if(traits != null) {
                ListIterator traitsIterator = traits.listIterator();
                boolean firstTime = true;
                while(traitsIterator.hasNext()) {
                    Detail currDetail = (Detail)traitsIterator.next();

                    if(currDetail.isPrintable()) {
                        if(firstTime) {
                            firstTime = false;
                            content = content + "\n<br>[<font color='red'><b>" + trait.getName() + "</b></font>]";
                        }
                        content = content + "\n<br><b>" + currDetail.getTrait() + "</b>";
                        ArrayList info = currDetail.getInformation();
                        ListIterator infoIterator = info.listIterator();
                        if(info.size() != 0) {
                            content = content + " (";
                        }
                        boolean firstone = true;
                        while(infoIterator.hasNext()) {
                            Information infoDetail = (Information)infoIterator.next();
                            if(firstone) {
                                if(infoDetail.getType().equals("")) {
                                    content = content + infoDetail.getData();
                                }
                                else {
                                    content = content + infoDetail.getType() + ": " + infoDetail.getData();
                                }
                                firstone = false;
                            }
                            else {
                                if(infoDetail.getType().equals("")) {
                                    content = content + ", " + infoDetail.getData();
                                }
                                else {
                                    content = content + ", " + infoDetail.getType() + ": " + infoDetail.getData();
                                }
                            }
                        }
                        if(info.size() != 0) {
                            content = content + ")";
                        }
                    }
                }
                if(!firstTime) {
                    content = content + "\n<br>";
                }
            }
        }
        content = content + "\n</BODY></HTML>";
        outputArea.setText(content);
    }//GEN-LAST:event_bGenerateActionPerformed

    private void initRest(TopPanel newTopPanel, String directory) {
        top = newTopPanel;
        top.setTables(tables);
        topPanel.add(top);
        personalities = new PersonalityArrayList(tables, "./" + directory);
    }
    
    public JTextPane getCurrentJTextPane() {
        JScrollPane scrollPane = (JScrollPane)outputTabbedPane.getSelectedComponent();
        return (JTextPane)scrollPane.getViewport().getView();
    }
    
    public Action getActionByName(javax.swing.text.JTextComponent textComponent, String name) {
        Action[] actionsArray = textComponent.getActions();
        for (int i = 0; i < actionsArray.length; i++) {
            Action a = actionsArray[i];
            if(a.getValue(Action.NAME).equals(name)) {
                return a;
            }
        }
        return null;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel topPanel;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JButton bGenerate;
    private javax.swing.JToolBar jToolBar;
    private javax.swing.JTextPane mainOutput;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton bClear;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JTabbedPane outputTabbedPane;
    // End of variables declaration//GEN-END:variables
    private TopPanel top;
    private PersonalityArrayList personalities;
    private boolean panesClear = true;
    private HashMap tables;
    private PersonalityFrame parent;
}

