package turgen.plugin.plturbo;

import java.io.File;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import turgen.plugin.SimplePlaylistItem;
import turgen.plugin.InOutProvider;


public class PlTurboPluginPanel extends javax.swing.JPanel {
   
    public InOutProvider ioProvider;
    private int maxFileNameLength;
    
    /** Creates new form PlTurboPluginPanel */
    public PlTurboPluginPanel(int maxFileNameLength) {
        initComponents();
        this.maxFileNameLength=maxFileNameLength;
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        pHeader = new javax.swing.JPanel();
        jlbName = new javax.swing.JLabel();
        jtfName = new javax.swing.JTextField();
        btnGuessHeader = new javax.swing.JButton();
        pBinaryFile = new javax.swing.JPanel();
        jlbPauses = new javax.swing.JLabel();
        jtfPauses = new javax.swing.JTextField();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        add(jLabel1, gridBagConstraints);

        pHeader.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Header block"));
        pHeader.setLayout(new java.awt.GridBagLayout());

        jlbName.setText("File name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pHeader.add(jlbName, gridBagConstraints);

        jtfName.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfName.setToolTipText("File name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pHeader.add(jtfName, gridBagConstraints);

        btnGuessHeader.setMnemonic('h');
        btnGuessHeader.setText("Auto set header");
        btnGuessHeader.setToolTipText("");
        btnGuessHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onGuessHeader(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pHeader.add(btnGuessHeader, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(pHeader, gridBagConstraints);

        pBinaryFile.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Binary file"));
        pBinaryFile.setLayout(new java.awt.GridBagLayout());

        jlbPauses.setText("Silence list:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pBinaryFile.add(jlbPauses, gridBagConstraints);

        jtfPauses.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfPauses.setToolTipText("<HTML>\nPlace <b>comma-separated list of numebrs</b> here. <br>\nEach number is <b>length of silence</b> (in seconds) generated <b>after INIT segments.</b><br>\nThis is useful only if the MOTOR CTRL signal is ignored by device you are using.<br>\n</HTML>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        pBinaryFile.add(jtfPauses, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(pBinaryFile, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void onGuessHeader(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onGuessHeader


        /*Testing existence*/
        File f = new File(ioProvider.getInFile());
        if (!f.exists()) {
            JOptionPane.showMessageDialog(null, "Unable to auto set header: Input file does not exist !","ERROR",JOptionPane.ERROR_MESSAGE);
            return;
        }
        /*Is really file?*/
        if (!f.isFile()) {
            JOptionPane.showMessageDialog(null, "Unable to auto set header: Input file is not a normal file !","ERROR",JOptionPane.ERROR_MESSAGE);
            return;
        }

        /*Copy up to 16 characters*/
        String nm=f.getName();
        int numChars=Math.min(maxFileNameLength, nm.length());

        jtfName.setText(nm.substring(0, numChars));
    }//GEN-LAST:event_onGuessHeader
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuessHeader;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jlbName;
    private javax.swing.JLabel jlbPauses;
    private javax.swing.JTextField jtfName;
    private javax.swing.JTextField jtfPauses;
    private javax.swing.JPanel pBinaryFile;
    private javax.swing.JPanel pHeader;
    // End of variables declaration//GEN-END:variables
    
    
    public void clear() {
        jtfName.setText("");
    }
    
    
    
    public SimplePlaylistItem getRecord() {

        StringTokenizer tok = new StringTokenizer(jtfPauses.getText(),",");

        int cnt=tok.countTokens();
        int[] pauses = new int[cnt];
        for(int k=0;k<cnt;k++) {
            try {
                pauses[k]=Integer.parseInt(tok.nextToken());
            }
            catch(Exception e) {
                pauses = new int[0];
            }
        }
        return new SimplePlaylistItem(ioProvider.getInFile(),ioProvider.getOutFile(),ioProvider.getPulses(),jtfName.getText(),pauses);
    }
    
    public void fill(SimplePlaylistItem dpr) {
        jtfName.setText(dpr.fname);
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<dpr.pauses.length;i++) {
            if (i!=0) sb.append(',');
            sb.append(Integer.toString(dpr.pauses[i]));
        }
        jtfPauses.setText(sb.toString());
    }
    
    public String verify() {
        return null;
    }
}
