/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package de.nplusc.izc.InstallPak;

import de.nplusc.izc.tools.baseTools.HidableTableColumnModel;
import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LH
 */
public class PackagerGUIReloaded extends JFrame
{
    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    private InstallFilesManagerAPI p;
    /**
     * Creates new form PackagerGUI
     * @param mdl Object containing the real install file;
     */
    public PackagerGUIReloaded(InstallFilesManagerAPI mdl)
    {
        p=mdl;
        initComponents();
        updatePosition();
        lblVID.setText(Definitions.VERSION_GUI_TXT);
        packStatus.setVisible(false);
        ((HidableTableColumnModel)(tblSelector.getColumnModel())).setColumnVisible(0, false);
        //^auskommentieren um id-spalte sichtbar zu bekommen (Tempcode fürs Testen)
        int rows=1;
        String[] tablefill = p.getPacksInPackage();
        for (int i = 0; i < tablefill.length; i++)
        {
            String GRP = tablefill[i];;
            if(i>=rows)
                ((DefaultTableModel)tblSelector.getModel()).addRow(new String[3]);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(i, i, 0);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(GRP, i, 1);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt("Wird installiert", i, 2);
            if(p.getPackInstalled(i))
            {
                ((DefaultTableModel)tblSelector.getModel()).setValueAt("Bereits installiert", i, 2);
            }
        }
        tblSelectorMouseClicked(null);
    }
    private void updatePosition()
    {
        Point p = new Point(Main.topleft_coords_x, Main.topleft_coords_y);
        setLocation(p);
    }
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblSelector = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        packDesc = new javax.swing.JTextArea();
        packTitle = new javax.swing.JLabel();
        btnInstall = new javax.swing.JButton();
        packStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblVID = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentMoved(java.awt.event.ComponentEvent evt)
            {
                formComponentMoved(evt);
            }
        });

        tblSelector.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {
                {null, null, null}
            },
            new String []
            {
                "meta:id", "Name", "Status"
            }
        )
        {
            boolean[] canEdit = new boolean []
            {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex)
            {
                return canEdit [columnIndex];
            }
        });
        tblSelector.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                tblSelectorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSelector);
        tblSelector.setColumnModel(new HidableTableColumnModel(tblSelector.getColumnModel()));
        if (tblSelector.getColumnModel().getColumnCount() > 0)
        {
            tblSelector.getColumnModel().getColumn(0).setResizable(false);
            tblSelector.getColumnModel().getColumn(0).setPreferredWidth(30);
            tblSelector.getColumnModel().getColumn(1).setMinWidth(80);
            tblSelector.getColumnModel().getColumn(1).setPreferredWidth(80);
            tblSelector.getColumnModel().getColumn(1).setMaxWidth(80);
            tblSelector.getColumnModel().getColumn(2).setPreferredWidth(80);
        }

        packDesc.setColumns(20);
        packDesc.setRows(5);
        packDesc.addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentMoved(java.awt.event.ComponentEvent evt)
            {
                packDescComponentMoved(evt);
            }
        });
        jScrollPane2.setViewportView(packDesc);

        packTitle.setText("{PackTitle}");

        btnInstall.setText("Installation starten");
        btnInstall.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnInstallActionPerformed(evt);
            }
        });

        packStatus.setText("{DepDetails}");

        jLabel1.setText("Doppelklicken um Status zu ändern");

        lblVID.setText("izSetup 1.0.0 (c) 2012 iZc");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(packStatus)
                                    .addComponent(packTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 12, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnInstall, javax.swing.GroupLayout.PREFERRED_SIZE, 597, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 584, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(8, 8, 8))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblVID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 642, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(packStatus))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(packTitle)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnInstall, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVID, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void tblSelectorMouseClicked(java.awt.event.MouseEvent evt)                                         
    {                                             
        int x = tblSelector.getSelectedRow();
        x=x!=-1?x:0;
        System.out.println("Row selected="+x);
        boolean sel = p.getIsScheduled(x);
        if(evt !=null&&evt.getClickCount()==2)
        {
            if(sel==false&&p.getPackInstallable(x))
            {
                sel=true;
            }
            else
            {
                sel=false;
            }
            p.setPackageState(x, sel);
        }
        
        rebuildTable();
        String _packTitle = p.getPacksInPackage()[x];
        String packDescr = p.getPackageDescription(x);
        packTitle.setText(_packTitle);
        packDesc.setText(packDescr);
    }                                        

    private String[][] handoverqueue;
    private void btnInstallActionPerformed(java.awt.event.ActionEvent evt)                                           
    {                                               
        handoverqueue = p.packagesQueue();
        new Thread(() ->
        {
            new ExtendedInstallProgressGui(handoverqueue).setVisible(true);
        }).start();
        this.setVisible(false);
    }                                          

    private void packDescComponentMoved(java.awt.event.ComponentEvent evt)                                        
    {                                            
        //NOP
    }                                       

    private void formComponentMoved(java.awt.event.ComponentEvent evt)                                    
    {                                        
        Main.topleft_coords_x = getLocation().x;
        Main.topleft_coords_y = getLocation().x;
    }                                   

    private void rebuildTable()
    {
        String[] tablefill = p.getPacksInPackage();
        for (int i = 0; i < tablefill.length; i++)
        {
            String GRP = tablefill[i];
            int packid = i;
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(i, i, 0);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(GRP, i, 1);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt("0xFA11", i, 2);
            
            if(p.getPackInstalled(packid))
            {
                Tools.DebugHelperPrint(packid+"|exis", true, "iZsetup.enableFinerDebug");
                ((DefaultTableModel)tblSelector.getModel()).setValueAt("Bereits installiert", i, 2);
            }
            else
            {
                if(p.getIsScheduled(packid))
                {
                    Tools.DebugHelperPrint(packid+"|sched", true, "iZsetup.enableFinerDebug");
                    ((DefaultTableModel)tblSelector.getModel()).setValueAt("Wird installiert", i, 2);
                }
                else
                {
                    if(!p.getPackInstallable(packid))
                    {
                        String depName = ""; 
                        int depID = p.getDependencyParent(i);
                        try
                        {
                        depName = tablefill[depID];
                        }
                        catch(Exception e)
                        {
                        }
                        Tools.DebugHelperPrint(packid+"|reqdep:"+depID, true, "iZsetup.enableFinerDebug");
                        ((DefaultTableModel)tblSelector.getModel()).setValueAt(depName+" benötigt", i, 2);
                    }
                    else
                    {
                        ((DefaultTableModel)tblSelector.getModel()).setValueAt("Abgewählt", i, 2);
                    }
                }
            }
        }
    }
            
    
    /**
     * @param args the command line arguments
     */    
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex)
        {

        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                //new PackagerGUIReloaded().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnInstall;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel lblVID;
    private javax.swing.JTextArea packDesc;
    private javax.swing.JLabel packStatus;
    private javax.swing.JLabel packTitle;
    private javax.swing.JTable tblSelector;
    // End of variables declaration                   
}

