/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResourceMonitor.java
 *
 * Created on 15-Apr-2012, 1:04:46 AM
 */
package webcamstudio.components;

import java.awt.Component;
import java.util.Timer;
import java.util.TimerTask;
import webcamstudio.mixers.MasterMixer;

/**
 *
 * @author patrick
 */
public class ResourceMonitor extends javax.swing.JPanel {

    Timer timer = new Timer();
    private static ResourceMonitor instance = null;
    /** Creates new form ResourceMonitor */
    private ResourceMonitor() {
        initComponents();
        timer.scheduleAtFixedRate(new ResourceMonitorThread(this), 0, 1000);
    }
    
    public static ResourceMonitor getInstance(){
        if (instance==null){
            instance = new ResourceMonitor();
        }
        return instance;
    }
    public void addMessage(ResourceMonitorLabel label){
        label.setBorder(lblFPS.getBorder());
        this.add(label);
        if (label.getEndTime() !=0){
            System.err.println(label.getText());
        }
        this.revalidate();
    }
    public void removeMessage(ResourceMonitorLabel label){
        this.remove(label);
        this.revalidate();
        this.repaint();
    }
    public void updateInfo(){
        long maxMem = Runtime.getRuntime().maxMemory() / 1024 / 1024;
        long usedMem = ((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()) / 1024 / 1024);
        memLevel.setMaximum((int)maxMem);
        memLevel.setValue((int)usedMem);
        memLevel.setString(usedMem + "MB/" + maxMem + "MB");
        lblFPS.setText(Math.round(MasterMixer.getInstance().getFPS()) + " fps");
        lblMixerSize.setText(MasterMixer.getInstance().getWidth()+ "X" + MasterMixer.getInstance().getHeight());
        for (Component c: this.getComponents()){
            if (c instanceof ResourceMonitorLabel){
                ResourceMonitorLabel rml = (ResourceMonitorLabel)c;
                if (rml.getEndTime() != 0 && rml.getEndTime()< System.currentTimeMillis()){
                    removeMessage(rml);
                }
            }
        }
    }
    /** 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        memLevel = new javax.swing.JProgressBar();
        lblFPS = new javax.swing.JLabel();
        lblMixerSize = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        memLevel.setName("memLevel"); // NOI18N
        memLevel.setStringPainted(true);
        add(memLevel);

        lblFPS.setText("jLabel1");
        lblFPS.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        lblFPS.setName("lblFPS"); // NOI18N
        add(lblFPS);

        lblMixerSize.setText("jLabel1");
        lblMixerSize.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        lblMixerSize.setName("lblMixerSize"); // NOI18N
        add(lblMixerSize);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblFPS;
    private javax.swing.JLabel lblMixerSize;
    private javax.swing.JProgressBar memLevel;
    // End of variables declaration//GEN-END:variables
}
class ResourceMonitorThread extends TimerTask{
    ResourceMonitor monitor;
    ResourceMonitorThread(ResourceMonitor m){
        monitor=m;
    }
    @Override
    public void run() {
        monitor.updateInfo();
    }
}