package com.mgatelabs.swftools.exploit.gui;

import com.mgatelabs.swftools.support.swf.io.FlashReader;
import com.mgatelabs.swftools.support.swf.objects.FMovie;
import com.mgatelabs.swftools.support.tools.SwingWorker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;

public class GuiLoadPopup extends JDialog {
    private Frame ownerFrame;
    private FMovie myFMovie;
    private int totalObjects;
    private int currentObject;

    private JProgressBar myJProgressBar;
    //rivate JProgressBar myJProgressBar2;
    private JProgressBar myJProgressBarRam;

    private javax.swing.Timer myTimer;
    private JButton cancelButton;

    private GuiLoadPopup myExploitConvertDialog;

    //private Converter myConverter;

    private ConverterWorker myConverterWorker;

    private int ramCount;

    FMovie myMovie;

    FlashReader myReader;

    // Construct

    public GuiLoadPopup(Frame owner, FlashReader aReader) {
        super(owner, "Flash Exploit: Loading SWF File", true);
        ///myConverter = aConverter;
        myExploitConvertDialog = this;
        ownerFrame = owner;

        myMovie = null;

        myReader = aReader;

        //myFMovie = aMovie;
        //currentObject = 0;
        //totalObjects = myFMovie.getObjects().size();

        myConverterWorker = null;

        this.getContentPane().add(buildGui());

        myConverterWorker = new ConverterWorker();

        myExploitConvertDialog.addWindowListener(new GuiLoadListenClass());

        display();
    }

    public FMovie getMovie() {
        return myMovie;
    }

    // Build panel

    public JPanel buildGui() {
        JPanel master = new JPanel();
        master.setBackground(Color.WHITE);
        master.setLayout(new BoxLayout(master, BoxLayout.Y_AXIS));

        JPanel slideMaster = new JPanel();
        slideMaster.setBackground(Color.WHITE);
        slideMaster.setLayout(new BoxLayout(slideMaster, BoxLayout.Y_AXIS));

        JPanel splitMaster = new JPanel();
        splitMaster.setBackground(Color.WHITE);
        splitMaster.setLayout(new BoxLayout(splitMaster, BoxLayout.X_AXIS));

        myJProgressBar = new JProgressBar(0, 100);
        myJProgressBar.setStringPainted(true);
        myJProgressBar.setString("Complete %");

        //myJProgressBar2 = new JProgressBar(0, 100);
        //myJProgressBar2.setStringPainted(true);
        //myJProgressBar2.setString("Status");

        Runtime r = Runtime.getRuntime();
        myJProgressBarRam = new JProgressBar(0, (int) (r.maxMemory() / 1024));
        myJProgressBarRam.setStringPainted(true);
        myJProgressBarRam.setString("Ram Usage");

        ExploitActionListener eal = new ExploitActionListener();

        myTimer = new javax.swing.Timer(1000, eal);

        //cancelButton = new JButton("Cancel");
        //cancelButton.addActionListener(eal);

        //master.add( new JLabel("Converting " + totalObjects + " Shapes") );
        //master.add( new JLabel( myConverter.getName()) );

        slideMaster.add(myJProgressBar);
        //slideMaster.add(myJProgressBar2);

        slideMaster.add(Box.createVerticalGlue());

        JPanel tempLabelPanel = new JPanel();
        tempLabelPanel.setBackground(Color.WHITE);
        tempLabelPanel.setLayout(new BoxLayout(tempLabelPanel, BoxLayout.X_AXIS));

        tempLabelPanel.add(Box.createHorizontalGlue());
        tempLabelPanel.add(new JLabel(new ImageIcon(this.getClass().getResource("/com/mgatelabs/swftools/images/icon.png"))));
        tempLabelPanel.add(Box.createHorizontalGlue());

        slideMaster.add(tempLabelPanel);

        slideMaster.add(Box.createVerticalGlue());

        slideMaster.add(myJProgressBarRam);

        splitMaster.add(slideMaster);
        //splitMaster.add(cancelButton);

        Random rand = new Random();

        int imageIndex = (rand.nextInt() % 4);

        if (imageIndex < 0) {
            imageIndex = 1;
        }

        imageIndex++;

        String imageUrl = "/com/mgatelabs/swftools/images/lolcat_0" + imageIndex + ".jpg";

        //System.out.println(imageUrl);

        splitMaster.add(new JLabel(new ImageIcon(this.getClass().getResource(imageUrl))));

        master.add(splitMaster);

        return master;
    }

    // Show to screen

    public void display() {
        this.setResizable(false);
        this.pack();
        setPos();
        this.show();
    }

    // Set window position

    public void setPos() {
        int w = this.getWidth();
        int h = this.getHeight();

        int pw = w;
        int ph = h;

        int px = 0;
        int py = 0;

        pw = ownerFrame.getWidth();
        ph = ownerFrame.getHeight();
        px = ownerFrame.getX();

        this.setLocation(px + (pw / 2) - (w / 2), py + (ph / 2) - (h / 2));
    }

    // Updated Display

    private class ExploitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == myTimer) {

                myJProgressBar.setValue(myReader.getComplete());
                /*
               int aValue = myConverter.getValue();
               int aTotal = myConverter.getTotal();
               String a = myConverter.getStatus();
					
					if (a != null)
						{
						myJProgressBar2.setString(a);
						}
						
               if (aValue < aTotal && aTotal > 0)
               {
                  myJProgressBar2.setValue( (int) (((float)aValue/(float)aTotal) * 100) );
               }
               
					
					*/

                ramCount++;
                if (ramCount == 10) {
                    ramCount = 0;
                    Runtime r = Runtime.getRuntime();
                    myJProgressBarRam.setValue((int) (r.freeMemory() / 1024));
                }

            } else if (e.getSource() == cancelButton) {
                cancelButton.setEnabled(false);
                //myConverter.cancel();
            }
        }
    }

    // Threaded Worker

    private class ConverterWorker extends SwingWorker {

        private boolean cancel;

        public ConverterWorker() {
            super();
            //myConverter = aConverter;
            //myMovie = aMovie;
            cancel = false;
        }

        public void clean() {
            //myConverter = null;
            //myMovie = null;
        }

        public void cancel() {
            cancel = true;
            //myMovie = null;
        }

        public Object construct() {
            try {
                myMovie = myReader.parse();
            } catch (Exception ex) {
                myMovie = null;
            }
            myTimer.stop();
            return null;
        }

        public void finished() {
            //myTimer.stop();
            myExploitConvertDialog.dispose();
        }
    }

    private class GuiLoadListenClass implements WindowListener {
        public void windowActivated(WindowEvent e) {
        }

        //Invoked when the Window is set to be the active Window.
        public void windowClosed(WindowEvent e) {
            myTimer = null;
            myExploitConvertDialog = null;
            //myConverter = null;
            //myFMovie = null;
            if (myConverterWorker != null) {
                myConverterWorker.clean();
            }
            myConverterWorker = null;
        }

        //Invoked when a window has been closed as the result of calling dispose on the window.
        public void windowClosing(WindowEvent e) {
        }

        //Invoked when the user attempts to close the window from the window's system menu.
        public void windowDeactivated(WindowEvent e) {
        }

        //Invoked when a Window is no longer the active Window.
        public void windowDeiconified(WindowEvent e) {
        }

        //Invoked when a window is changed from a minimized to a normal state.
        public void windowIconified(WindowEvent e) {
        }

        //Invoked when a window is changed from a normal to a minimized state.
        public void windowOpened(WindowEvent e) {
            //myTimer.start();
            myTimer.start();
            myConverterWorker.start();
        }
        //Invoked the first time a window is made visible.
    }

}
@@ -4219,32 +4219,34 @@
 eUrl);%0A%0A        
+//
 splitMaster.add(
@@ -8352,8 +8352,9 @@
     %7D%0A%0A%7D
+%0A

Fix for opening files