package turgen.plugin.plturbo;

import java.awt.event.ItemEvent;
import turgen.plugin.csturbo.*;
import turgen.plugin.*;
import javax.swing.*;
import java.io.*;
import turgen.dtb.DOS2Binary;
import turgen.Utils;

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

        jrbgHandling = new javax.swing.ButtonGroup();
        pTHeader = new javax.swing.JPanel();
        jtfName = new javax.swing.JTextField();
        jlbName = new javax.swing.JLabel();
        btnGuessHeader = new javax.swing.JButton();
        pBinaryFile = new javax.swing.JPanel();
        jlbPauses = new javax.swing.JLabel();
        jtfPauses = new javax.swing.JTextField();
        jcoBinLoad = new javax.swing.JComboBox();
        btnCheckLoader = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        pFiller = new javax.swing.JPanel();

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

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

        jtfName.setColumns(22);
        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 = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pTHeader.add(jtfName, gridBagConstraints);

        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, 4, 0);
        pTHeader.add(jlbName, gridBagConstraints);

        btnGuessHeader.setMnemonic('h');
        btnGuessHeader.setText("Auto set header");
        btnGuessHeader.setToolTipText("<HTML>\nAutomatically fill the turbo header using:<BR>\n1.Analysis of input file<BR>\n2.Selected conversion type<BR>\n</HTML>\n\n");
        btnGuessHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                on_guess_header(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pTHeader.add(btnGuessHeader, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(pTHeader, 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);

        jcoBinLoad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NONE", "HTBL+" }));
        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) {
                jcoBinLoadon_loader_change(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pBinaryFile.add(jcoBinLoad, gridBagConstraints);

        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);

        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);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(pBinaryFile, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 1.0;
        add(pFiller, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    private void on_guess_header(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_on_guess_header

        
        /*Clearing text fields*/
        jtfName.setText("");

        /*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;
        }
        
        DOS2Binary dtb = new DOS2Binary(ioProvider.getInFile());
        try {
            dtb.analyzeFromFile();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Unable to auto set header: Input file is not a binary file !","ERROR",JOptionPane.ERROR_MESSAGE);
            return;
        }


        /*File name*/
        String fn=f.getName();
        if (fn.length()>39) fn=fn.substring(0,39);

        /*File name is common*/
        jtfName.setText(fn);
    

    }//GEN-LAST:event_on_guess_header

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

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

        int sl = jcoBinLoad.getSelectedIndex();
        String fname=ioProvider.getInFile();
        
        /*Check whether loader won't be destroyed*/
        switch(sl) {

            /*None*/
            case PlHardTurboPlugin.BL_NONE: {
                break;
            }
            /*HTBL+*/
            case PlHardTurboPlugin.BL_HTBLP: {
                String msg=Utils.checkForBinaryLoader(fname, 1999, 2408, 1999, 2408, Integer.MAX_VALUE);
                JOptionPane.showMessageDialog(null, msg);
                break;
            }
        }
        
    }//GEN-LAST:event_btnCheckLoaderActionPerformed
    
    
    
    // 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 jlbName;
    private javax.swing.JLabel jlbPauses;
    private javax.swing.ButtonGroup jrbgHandling;
    private javax.swing.JTextField jtfName;
    private javax.swing.JTextField jtfPauses;
    private javax.swing.JPanel pBinaryFile;
    private javax.swing.JPanel pFiller;
    private javax.swing.JPanel pTHeader;
    // End of variables declaration//GEN-END:variables
    
    
    void clear() {
        jtfName.setText("");
        jcoBinLoad.setSelectedIndex(0);
    }
    
    
    public PlHardTurboPlaylistItem getRecord() {
        PlHardTurboPlaylistItem trpi = new PlHardTurboPlaylistItem(ioProvider.getInFile(),ioProvider.getOutFile(),ioProvider.getPulses(),jtfName.getText(),jcoBinLoad.getSelectedIndex(),Utils.parseNumberList(jtfPauses.getText().trim()));
        return trpi;
    }
    
    
    public void fill(PlHardTurboPlaylistItem trpi) {
        jtfName.setText(trpi.fname);
        jcoBinLoad.setSelectedIndex(trpi.binaryLoader);
        jtfPauses.setText(Utils.createNumberList(trpi.silenceList));

    }
    
    public String verify() {
        
        if (verifyIsBinary()==false) {
            return HT_NOT_BINARY_MSG;
        }
        return null;
    }
    
    
    private void setupToolTips() {
        
    }

    boolean verifyIsBinary() {

         /*Load and analyze file*/
        DOS2Binary ae = new DOS2Binary(ioProvider.getInFile());
        try {
            ae.analyzeFromFile();
        }
        catch (Exception e) {
            return false;
        }

        return true;

    }

    private static final String HT_NOT_BINARY_MSG="Input file is not a binary file";

}


   



