// clsDiscovery.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.Diagnostics;
using System.Windows.Forms;
using FileScope;
using FileScope.Gnutella2;

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

        /// <summary>
        /// Initializes a new instance of the <see cref="clsDiscovery"/> class.
        /// </summary>
        public clsDiscovery()
        {
            //
            // TODO: Add constructor logic here
            //

            //register for events
            //zzoi 6 June 2008  commented to compile
            //GUIBridge.DeleteWebCacheRecord += new GUIBridge.NewWebCacheDeleteDelegate(DeleteWebCacheItem);
            GUIBridge.UpdateWebCacheRecord += UpdateWebCacheItem;
            GUIBridge.NewWebCacheRecord += AddWebCacheItem;
        }

        public ctrlDiscovery GetDiscoveryCtrl()
        {
            ctrlDiscovery ctrl = new ctrlDiscovery(this);
            m_ctrl = ctrl;
            LoadWebCache(ctrl);
            return ctrl;
        }

        /// <summary>
        /// load the listview with info from the engen
        /// </summary>
        private void LoadWebCache(ctrlDiscovery ctrl)
        {
            lock (Stats.gnutella2WebCache)
            {
                foreach (string key in Stats.gnutella2WebCache.Keys)
                {
                    string[] subitems = new string[5];
                    HubInfo hi = (HubInfo) Stats.gnutella2WebCache[key];

                    subitems[0] = key; //address
                    subitems[1] = hi.timeKnown.ToString(); //last accessed
                    subitems[2] = hi.mhi.Hosts.ToString(); //host count
                    subitems[3] = hi.mhi.Acesses.ToString(); //access count
                    subitems[4] = hi.mhi.Updates.ToString(); //update count

                    ListViewItem lvi = new ListViewItem(subitems);
                    //lvi.ForeColor = Stats.settings.clHighlight3;
                    ctrl.lstDiscovery.Items.Add(lvi);
                }
            }
        }

        //update the cache with new stats
        public void UpdateWebCacheItem(int index)
        {
            lock (Stats.gnutella2WebCache)
            {
                string key = (string) Stats.gnutella2WebCache.GetKey(index); //the key value
                HubInfo hi = (HubInfo) Stats.gnutella2WebCache[key];

                //get the record from the list by key with enumerator
                ListViewItem lvi;
                for (int x = 0; x < m_ctrl.lstDiscovery.Items.Count; x++)
                {
                    lvi = m_ctrl.lstDiscovery.Items[x];
                    if (lvi.SubItems[0].Text == key)
                    {
                        //update
                        lvi.SubItems[1].Text = hi.timeKnown.ToString(); //last accessed
                        lvi.SubItems[2].Text = hi.mhi.Hosts.ToString(); //host count
                        lvi.SubItems[3].Text = hi.mhi.Acesses.ToString(); //access count
                        lvi.SubItems[4].Text = hi.mhi.Updates.ToString(); //update count
                    }
                }
            }
        }

        //add a record
        public void AddWebCacheItem(int index)
        {
            string key = (string) Stats.gnutella2WebCache.GetKey(index); //the key value
            string[] subitems = new string[5];
            HubInfo hi = (HubInfo) Stats.gnutella2WebCache[key];

            subitems[0] = key; //address
            subitems[1] = hi.timeKnown.ToString(); //last accessed
            subitems[2] = hi.mhi.Hosts.ToString(); //host count
            subitems[3] = hi.mhi.Acesses.ToString(); //access count
            subitems[4] = hi.mhi.Updates.ToString(); //update count

            ListViewItem lvi = new ListViewItem(subitems);
            m_ctrl.lstDiscovery.Items.Add(lvi);
        }

        //deletea record
        public void DeleteWebCacheItem(string key)
        {
            try
            {
                //string key  = (string)FileScope.Stats.gnutella2WebCache.GetKey(index); //the key value
                ListViewItem lvi;
                for (int x = 0; x < m_ctrl.lstDiscovery.Items.Count; x++)
                {
                    lvi = m_ctrl.lstDiscovery.Items[x];
                    if (lvi.SubItems[0].Text == key)
                    {
                        m_ctrl.lstDiscovery.Items.RemoveAt(x);
                        return;
                    }
                }
            }
            catch
            {
                Debug.WriteLine("Webcach DeleteWebCacheItem");
            }
        }
    }
}