package ufrcoderadvance;

import java.awt.Color;
import java.awt.Font;

import javax.swing.table.*;

public class frmViewAll extends javax.swing.JInternalFrame {

    public frmViewAll() {
        initComponents();
    }
    Global GL = new Global();
    int result = 0;
    static final int RES_LIGHT_OK = 1,
            RES_SOUND_OK = 4,
            ERR_LIGHT = 2,
            ERR_SOUND = 2,
            DL_OK = 0,
            AUTH1A = 96,
            AUTH1B = 97,
            MAX_BYTES = 752;
    final static String ANY_DATA = "You must enter any data !",
            APPROPRIATE_FORMAT = "You must enter the appropriate format !";

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        stbFunction = new javax.swing.JPanel();
        lblFunctionError = new javax.swing.JLabel();
        lblFunction_explain = new javax.swing.JLabel();
        lblFunction_code = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblViewAll = new javax.swing.JTable();
        btnRead = new javax.swing.JButton();
        chkHex = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setTitle("frmViewAll");
        setFocusCycleRoot(false);
        setFocusable(false);
        setName("frmViewAll"); // NOI18N
        setNormalBounds(new java.awt.Rectangle(0, 0, 555, 378));
        setPreferredSize(new java.awt.Dimension(555, 378));
        setRequestFocusEnabled(false);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });

        stbFunction.setBackground(new java.awt.Color(187, 185, 185));
        stbFunction.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblFunctionError.setBackground(new java.awt.Color(153, 153, 153));
        lblFunctionError.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblFunctionError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFunctionError.setText("Function Error:");
        lblFunctionError.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblFunction_explain.setBackground(new java.awt.Color(153, 153, 153));
        lblFunction_explain.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblFunction_explain.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFunction_explain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblFunction_code.setBackground(new java.awt.Color(153, 153, 153));
        lblFunction_code.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblFunction_code.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFunction_code.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout stbFunctionLayout = new javax.swing.GroupLayout(stbFunction);
        stbFunction.setLayout(stbFunctionLayout);
        stbFunctionLayout.setHorizontalGroup(
            stbFunctionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(stbFunctionLayout.createSequentialGroup()
                .addComponent(lblFunctionError, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblFunction_code, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblFunction_explain, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE))
        );
        stbFunctionLayout.setVerticalGroup(
            stbFunctionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblFunctionError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lblFunction_explain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lblFunction_code, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        tblViewAll.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tblViewAll.setFont(new java.awt.Font("Dialog", 1, 11)); // NOI18N
        tblViewAll.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "S", "BS", "Bl", "0", "1", "2", "3", "Title 8", "Title 9", "Title 10", "Title 11", "Title 12", "Title 13", "Title 14", "Title 15", "Title 16", "Title 17", "Title 18", "Title 19"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblViewAll.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        tblViewAll.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblViewAll);
        tblViewAll.getColumnModel().getColumn(0).setResizable(false);
        tblViewAll.getColumnModel().getColumn(0).setPreferredWidth(25);
        tblViewAll.getColumnModel().getColumn(1).setResizable(false);
        tblViewAll.getColumnModel().getColumn(1).setPreferredWidth(27);
        tblViewAll.getColumnModel().getColumn(2).setResizable(false);
        tblViewAll.getColumnModel().getColumn(2).setPreferredWidth(26);
        tblViewAll.getColumnModel().getColumn(3).setResizable(false);
        tblViewAll.getColumnModel().getColumn(3).setPreferredWidth(25);
        tblViewAll.getColumnModel().getColumn(4).setResizable(false);
        tblViewAll.getColumnModel().getColumn(5).setResizable(false);
        tblViewAll.getColumnModel().getColumn(6).setResizable(false);
        tblViewAll.getColumnModel().getColumn(7).setResizable(false);
        tblViewAll.getColumnModel().getColumn(8).setResizable(false);
        tblViewAll.getColumnModel().getColumn(9).setResizable(false);
        tblViewAll.getColumnModel().getColumn(10).setResizable(false);
        tblViewAll.getColumnModel().getColumn(11).setResizable(false);
        tblViewAll.getColumnModel().getColumn(12).setResizable(false);
        tblViewAll.getColumnModel().getColumn(13).setResizable(false);
        tblViewAll.getColumnModel().getColumn(14).setResizable(false);
        tblViewAll.getColumnModel().getColumn(15).setResizable(false);
        tblViewAll.getColumnModel().getColumn(16).setResizable(false);
        tblViewAll.getColumnModel().getColumn(17).setResizable(false);
        tblViewAll.getColumnModel().getColumn(18).setResizable(false);

        btnRead.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        btnRead.setText("READ");
        btnRead.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnRead.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReadActionPerformed(evt);
            }
        });

        chkHex.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        chkHex.setText("Hex");
        chkHex.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(stbFunction, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(302, Short.MAX_VALUE)
                .addComponent(chkHex)
                .addGap(33, 33, 33)
                .addComponent(btnRead, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chkHex)
                    .addComponent(btnRead, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(stbFunction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

    private void ReadValue(boolean first_read) {
        
        TableColumn column = null;


        byte block_count = 0;
        byte sector_count = 0;
        byte bis_count = 0;
        byte key_index = 0;
        byte auth_mode = 96;
        byte[] block_data = new byte[16];
        tblViewAll.getTableHeader().setBackground(Color.LIGHT_GRAY);
        tblViewAll.getTableHeader().setFont(new Font("Verdana", 1, 11));
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setBackground(Color.LIGHT_GRAY);
        r.setFont(new Font("Verdana", 1, 11));
        for (int br = 0; br < 3; br++) {
            tblViewAll.getColumnModel().getColumn(br).setCellRenderer(r);
        }

        for (int br = 3; br < 19; br++) {
            column = tblViewAll.getColumnModel().getColumn(br);
            column.setPreferredWidth(30);
            column.setHeaderValue(br - 3);
        }

        while (block_count < 63) {
            bis_count = 0;
            while (bis_count < 3) {
                tblViewAll.setValueAt(sector_count, block_count, 0);
                tblViewAll.setValueAt(bis_count, block_count, 1);
                tblViewAll.setValueAt(block_count, block_count, 2);
                if (first_read) {
                    result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.BlockRead(block_data, block_count, auth_mode, key_index);
                    for (int br = 0; br < 16; br++) {
                        if (chkHex.isSelected()) {
                            tblViewAll.setValueAt("$" + Integer.toHexString(((char) block_data[br] & 0xFF)), block_count, br+3 );
                            
                        } else {
                            tblViewAll.setValueAt((char) ((char) block_data[br] & 0xFF), block_count, br + 3);
                        }
                    }
               }
                bis_count++;
                block_count++;
            }

            for (int br = 0; br < 3; br++) {
                tblViewAll.setValueAt("Sct", block_count, br);
            }
            block_count++;
            sector_count++;
        }
        if (first_read) {
            if (result == DL_OK) {

                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK, RES_SOUND_OK);
                GL.ERRORS_CODE(result, lblFunction_code, lblFunction_explain);
            } else {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT, ERR_SOUND);
                GL.ERRORS_CODE(result, lblFunction_code, lblFunction_explain);
            }
        }

    }
    private void btnReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReadActionPerformed
         frmMain M = new frmMain();
        if (M.functionOn) {
            return;
        }
        new frmMain().ReaderOff();
        if (M.thread_start) {
            M.ReaderOn();
            M.functionOn = false;
            return;
        }
        try{
          ReadValue(true);
          } finally {
            M.ReaderOn();
            M.functionOn = false;
        }

    }//GEN-LAST:event_btnReadActionPerformed

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
          frmMain M = new frmMain();
        if (M.functionOn) {
            return;
        }
        new frmMain().ReaderOff();
        if (M.thread_start) {
            M.ReaderOn();
            M.functionOn = false;
            return;
        }
        try{
          ReadValue(false);
          } finally {
            M.ReaderOn();
            M.functionOn = false;
        }
       
    }//GEN-LAST:event_formInternalFrameActivated

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        
    }//GEN-LAST:event_formMouseDragged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRead;
    private javax.swing.JCheckBox chkHex;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFunctionError;
    private javax.swing.JLabel lblFunction_code;
    private javax.swing.JLabel lblFunction_explain;
    private javax.swing.JPanel stbFunction;
    private javax.swing.JTable tblViewAll;
    // End of variables declaration//GEN-END:variables
}
