//This file has been modified by Nimish Jain on March 03, 2006 at 1930 Hrs IST

/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2005 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  Created on 11.08.2005
 *  --- CVS Information ---
 *  $Id: ConnectionsZone.java,v 1.2 2005/10/03 00:18:26 gregork Exp $
 */
package phex.gui.statusbar;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import phex.common.HorizonTracker;

import phex.connection.NetworkManager;
import phex.gui.actions.FWAction;
import phex.gui.common.GUIRegistry;
import phex.gui.common.IconFactory;
import phex.gui.common.LinkLabel;
import phex.host.HostManager;
import phex.host.NetworkHostsContainer;
import phex.utils.Localizer;

public class ConnectionsZone extends JPanel
{
    private NetworkHostsContainer hostsContainer;
    private NetworkManager networkMgr;
    
    private JProgressBar jp;
    private JLabel connectionLabel;
    private Icon connectedIcon;
    private Icon firewalledIcon;
    private Icon disconnectedIcon;
    private JLabel systemPerformance;
    private LinkLabel gameLabel;

    public ConnectionsZone()
    {
        super(  );
        SpringLayout layout = new SpringLayout();
        setLayout( new BorderLayout() );
        
        networkMgr = NetworkManager.getInstance();
        HostManager hostMgr = HostManager.getInstance();
        hostsContainer = hostMgr.getNetworkHostsContainer();
        
        IconFactory factory = GUIRegistry.getInstance().getIconFactory();
        connectedIcon = factory.getIcon( "Network" );
        firewalledIcon = factory.getIcon( "Firewalled" );
        disconnectedIcon = factory.getIcon( "Disconnect" );
        
        //Nimish
        jp=new JProgressBar();
        jp.setMinimum(0);
        jp.setMaximum(50);
        jp.setValue(0);
        jp.setStringPainted(true);
        jp.setString("Connecting...");
        add(jp,BorderLayout.WEST);
        
        connectionLabel = new JLabel();
        connectionLabel.setIcon( connectedIcon );
        

        //Nimish
        gameLabel = new LinkLabel(new gameAction());
        gameLabel.setText("  Play VideoToons Game");
        gameLabel.setToolTipText("Don't be bored while downloading.Play VideoToons Game.");
        add(gameLabel,BorderLayout.CENTER);
        systemPerformance = new JLabel();
        add(systemPerformance,BorderLayout.EAST);
        
        updateZone();
        
        layout.putConstraint(SpringLayout.NORTH, jp, 2, SpringLayout.NORTH, this );
        layout.putConstraint(SpringLayout.WEST, jp, 5, SpringLayout.WEST, this );
        layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, jp );
        layout.putConstraint(SpringLayout.SOUTH, this, 2, SpringLayout.SOUTH, jp );
    }

    public void updateZone()
    {
        //Nimish

        int hostCount = HorizonTracker.getInstance().getTotalHostCount();
        //int hostCount = hostsContainer.getTotalConnectionCount();
        if ( hostCount > 0 )
        {
            if ( networkMgr.hasConnectedIncoming() )
            {
                connectionLabel.setIcon( connectedIcon );
            }
            else
            {
                //Nimish
                connectionLabel.setIcon( firewalledIcon );
                jp.setString("You are Firewalled...");
            }
        }
        else
        {
            //Nimish
            connectionLabel.setIcon( disconnectedIcon );
            jp.setString("You are Disconnected...");
            
        }

        Object[] args = new Object[]
        {
            new Integer( hostCount )
        };
        //Nimish
        try {
        systemPerformance.setText("System Performance :"+ GUIRegistry.getInstance().getMProfiler().getStatus());
        String text = Localizer.getFormatedString( "StatusBar_Connections", args );
            connectionLabel.setText("Connections :"+ String.valueOf(hostCount));
        
        if(hostCount<=50)
        {
        jp.setString( "Connecting..."+String.valueOf(hostCount*2)+"%");
        }
        else if(hostCount>50 && hostCount<80)
        {
            jp.setString("Connected...100%");
            
        }
        else if(hostCount>=80 && hostCount<200)
        {
                jp.setMinimum(80);
                jp.setMaximum(200);
                jp.setString("Extending Connections...");
        }
        else
        {
                jp.setString("Extended Connections...made");
        }
        jp.setValue(hostCount);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        validate();
    }
    
    //Nimish - Created this Action class to handle gameLabel action
    public class gameAction extends FWAction
    {
        public void actionPerformed(ActionEvent ae)
        {
            gameRunnable gr=new gameRunnable();
            gr.start();
        }
        
        public void refreshActionState()
        {
            
        }
    }
    
    //Nimish - Thread class to run the Game
    public class gameRunnable extends Thread
    {
        public void run()
                        {
                            try
                            {
                                gameLabel.setEnabled(false);
                                Process p;
                                p=Runtime.getRuntime().exec("java -jar videotoons.jar");
                                p.waitFor();
                            }
                            catch(Exception e)
                            {
                                e.printStackTrace();
                            }
                            finally
                            {
                                gameLabel.setEnabled(true);
                            }
                        }
    }
    
    
    
    
}
