package turgen.plugin.kblock;

import java.awt.event.ItemEvent;
import java.io.File;
import turgen.plugin.*;
import java.util.StringTokenizer;
import javax.swing.*;
import turgen.Utils;


public class KBlockPluginPanel extends javax.swing.JPanel {
    
    
    public InOutProvider ioProvider;
    
    /** Creates new form KBlockPluginPanel */
    public KBlockPluginPanel() {
        initComponents();
    }
    
    /** 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;

        jlbFiller = 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();
        jLabel2 = new javax.swing.JLabel();
        jcoBinLoad = new javax.swing.JComboBox();
        btnCheckLoader = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        add(jlbFiller, 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.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        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 = 1;
        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 = 1;
        gridBagConstraints.gridwidth = 2;
        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);

        jLabel2.setText("Binary loader:");
        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);
        pBinaryFile.add(jLabel2, gridBagConstraints);

        jcoBinLoad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NONE", "MINITBL", "NANOTBL", "NANOTBL[UR]", "NANOTBL[U2]" }));
        jcoBinLoad.setToolTipText("<HTML>\nChoose one of built-in binary loaders that will be prepent<BR>\nUseful for binary files\n<HTML>\n");
        jcoBinLoad.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                on_loader_change(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pBinaryFile.add(jcoBinLoad, gridBagConstraints);

        btnCheckLoader.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        btnCheckLoader.setMnemonic('c');
        btnCheckLoader.setText("Check loader");
        btnCheckLoader.setToolTipText("<HTML>\nCheck whether selected binary loader<BR>\nwill be destroyed by input binary file<BR>\n</HTML>");
        btnCheckLoader.setEnabled(false);
        btnCheckLoader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckLoaderActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        pBinaryFile.add(btnCheckLoader, gridBagConstraints);

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

    private void on_loader_change(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_on_loader_change
        if (evt.getStateChange()==ItemEvent.SELECTED) {
            if (jcoBinLoad.getSelectedIndex()==0) {
                btnCheckLoader.setEnabled(false);
            }
            else {
                btnCheckLoader.setEnabled(true);
            }
        }
    }//GEN-LAST:event_on_loader_change

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

        
        int sl = jcoBinLoad.getSelectedIndex();
        String msg="";
        String fname=ioProvider.getInFile();

        switch(sl) {
            /*MiniTBL*/
            case 1: {
                msg=Utils.checkForBinaryLoader(fname, 2818, 3489, 1792, 1792+1023,Integer.MAX_VALUE);
                break;
            }
            /*NanoTBL*/
            case 2: {
                msg=Utils.checkForBinaryLoader(fname, 2818, 3318, 1792, 1792+1023,Integer.MAX_VALUE);
                break;
            }
            /*NanoTBL[UR]*/
            case 3: {
                msg=Utils.checkForBinaryLoader(fname, 1794, 2349, 49152, 49152+1023,Integer.MAX_VALUE);
                break;
            }
            /*NanoTBL[U2]*/
            case 4: {
                msg=Utils.checkForBinaryLoader(fname, 1794, 2349, 63488, 63488+1023,Integer.MAX_VALUE);
                break;
            }
        }
        JOptionPane.showMessageDialog(null, msg,"Loader check",JOptionPane.INFORMATION_MESSAGE);

        


    }//GEN-LAST:event_btnCheckLoaderActionPerformed

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

        File f = new File(ioProvider.getInFile());
        
        /*Testing existence*/
        String msg = Utils.checkIfHeaderCanBeSet(f);
        if (msg!=null) {
            JOptionPane.showMessageDialog(null, msg,"Unable to set header",JOptionPane.ERROR_MESSAGE);
            return;
        }

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

        jtfName.setText(nm.substring(0, numChars));

    }//GEN-LAST:event_onGuessHeader
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheckLoader;
    private javax.swing.JButton btnGuessHeader;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox jcoBinLoad;
    private javax.swing.JLabel jlbFiller;
    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
    
    
    void clear() {
        jtfName.setText("");
        jtfPauses.setText("");
        jcoBinLoad.setSelectedIndex(0);
        
    }
    
    
    public KBlockPlayListItem 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];
            }
        }
        
        int bl=jcoBinLoad.getSelectedIndex();
        
        return new KBlockPlayListItem(ioProvider.getInFile(),ioProvider.getOutFile(),ioProvider.getPulses(),jtfName.getText(),pauses,bl);
    }
    
    public void fill(KBlockPlayListItem dpr) {
        jtfName.setText(dpr.fname);
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<dpr.silenceList.length;i++) {
            if (i!=0) sb.append(',');
            sb.append(Integer.toString(dpr.silenceList[i]));
        }
        jtfPauses.setText(sb.toString());
        jcoBinLoad.setSelectedIndex(dpr.loader);
        
    }
    
    
    public String verify() {
        return null;
    }

    
    
}
