// clsHosts.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// 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

using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;
using FileScope;
using FileScope.Gnutella2;
using GUIControlLogic.Error;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for clsHosts.
    /// </summary>
    public class clsHosts
    {
        private ctrlHosts m_ctrl;

        public clsHosts()

        {
            try
            {
                //
                // TODO: Add constructor logic here
                //

                //register for events
                GUIBridge.DeleteHostCacheRecord += DeleteHostCacheItem;
                GUIBridge.UpdateHostCacheRecord += UpdateHostCacheItem;
                GUIBridge.NewHostCacheRecord += AddHostCacheItem;
            }
            catch (Exception ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }


        public ctrlHosts GetHostsCtrl()
        {
            try
            {
                ctrlHosts ctrl = new ctrlHosts(this);
                m_ctrl = ctrl;
                LoadHostCache(ctrl);
                return ctrl;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
                return null;
            }
        }

        /// <summary>
        /// load the listview with info from the engen
        /// </summary>
        private void LoadHostCache(ctrlHosts ctrl)
        {
            try
            {
                lock (HostCache.hubCache)
                {
                    foreach (IPAddress  key in HostCache.hubCache.Keys)
                    {
                        string[] subitems = new string[5];
                        HubInfo hi = (HubInfo) HostCache.hubCache[key];

                        subitems[0] = key.ToString(); //address
                        subitems[1] = hi.port.ToString(); //port
                        //subitems[2] = hi.mhi.Hosts.ToString() ; //url
                        subitems[3] = hi.mhi.LastSeen.ToString(); //last seen
                        //subitems[4] = hi.mhi.Updates.ToString() ; //name


                        ListViewItem lvi = new ListViewItem(subitems);
                        //lvi.ForeColor = Stats.settings.clHighlight3;
                        ctrl.lstHost.Items.Add(lvi);
                    }
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        //update the cache with new stats
        public void UpdateHostCacheItem(int index)
        {
            try
            {
                lock (HostCache.hubCache)
                {
                    string key = (string) HostCache.hubCache.GetKey(index); //the key value
                    HubInfo hi = (HubInfo) HostCache.hubCache[key];

                    //get the record from the list by key with enumerator
                    ListViewItem lvi;
                    for (int x = 0; x < m_ctrl.lstHost.Items.Count; x++)
                    {
                        lvi = m_ctrl.lstHost.Items[x];
                        if (lvi.SubItems[0].Text == key)
                        {
                            //update
                            lvi.SubItems[1].Text = hi.port.ToString(); //port
                            //lvi.SubItems[2].Text  = hi.mhi.Hosts.ToString() ; //url
                            lvi.SubItems[3].Text = hi.mhi.LastSeen.ToString(); //last seen
                            //lvi.SubItems[4].Text  = hi.mhi.Updates.ToString() ; //name
                        }
                    }
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        //add a record
        public void AddHostCacheItem(int index)
        {
            try
            {
                IPAddress key = (IPAddress) HostCache.hubCache.GetKey(index); //the key value
                string[] subitems = new string[5];
                HubInfo hi = (HubInfo) HostCache.hubCache[key];

                subitems[0] = key.ToString(); //address
                subitems[1] = hi.port.ToString(); //port
                //subitems[2] = hi.mhi.Hosts.ToString() ; //url
                subitems[3] = hi.mhi.LastSeen.ToString(); //lastseen
                //subitems[4] = hi.mhi.Updates.ToString() ; //name

                ListViewItem lvi = new ListViewItem(subitems);

                //lvi.Tag = key;

                m_ctrl.lstHost.Items.Add(lvi);

                //set focus to the item added
                m_ctrl.lstHost.Items[lvi.Index].Focused = true;
                //m_ctrl.lstHost.Items[m_ctrl.lstHost.Items.].Focused = true;
                //lvi.Selected = true;
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        //deletea record
        public void DeleteHostCacheItem(string key)
        {
            try
            {
                //string key  = (string)FileScope.Gnutella2.HostCache.hubCache.GetKey(index); //the key value
                ListViewItem lvi;
                for (int x = 0; x < m_ctrl.lstHost.Items.Count; x++)
                {
                    lvi = m_ctrl.lstHost.Items[x];
                    if (lvi.SubItems[0].Text == key)
                    {
                        m_ctrl.lstHost.Items.RemoveAt(x);
                        return;
                    }
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                Debug.WriteLine("Hostcach DeleteHostCacheItem");
            }
        }


        public void ConnectHost(string key)
        {
            try
            {
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                Debug.WriteLine("Hostcach DeleteHostCacheItem");
            }
        }
    }
}