// ctrlDiscovery.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.ComponentModel;
using System.Windows.Forms;
using FileScope;
using FileScope.Gnutella2;
using GUIControlLogic.Error;
using ZANZI.ctrl;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for ctrlDiscovery.
    /// </summary>
    public class ctrlDiscovery : UserControl, Ictrl
    {
        private readonly clsDiscovery m_cls;
        private ContextMenu cmnuDiscovery;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private ColumnHeader columnHeader5;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private Container components;

        private string InputText;
        public ListView lstDiscovery;
        private MenuItem menuServerRemove;

        private MenuItem mnuServerAdd;
        private MenuItem mnuServerQuery;
        //zzoi localized the hardcoded tabheader string
        //private string tabHeader;

        private MenuItem mnuServerUpdate;

        private ComponentResourceManager resources;
        // = new System.ComponentModel.ComponentResourceManager(typeof(ctrlDiscovery));

        public ctrlDiscovery(clsDiscovery cls)
        {
            m_cls = cls;


            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitializeComponent call
        }

        #region Ictrl Members

        public override string Text
        {
            //zzoi localized the hardcoded tabheader string
            get { return resources.GetString("tabHeader.Text"); }
            //get{ return  "Discovery";}
            //set{ m_CharValue = value;}
        }

        public  bool StaticControl
        {

            get { return true; }

        }

        /// <summary>
        /// Function for restCulture event raised when language is changed
        /// </summary>
        public void ResetCulture()
        {
            try
            {
                //zzoi 6 June 2008 impliment string replacements for text
                //zzoi 6 June 2008 impliment string replacements for text
                //zzoi 6 June 2008 added mnuitems for reset to other language
                Text = resources.GetString("tabHeader.Text");


                lstDiscovery.Columns[0].Text = resources.GetString("columnHeader1.Text");
                lstDiscovery.Columns[1].Text = resources.GetString("columnHeader2.Text");
                lstDiscovery.Columns[2].Text = resources.GetString("columnHeader3.Text");
                lstDiscovery.Columns[3].Text = resources.GetString("columnHeader4.Text");
                lstDiscovery.Columns[4].Text = resources.GetString("columnHeader5.Text");


                lstDiscovery.Refresh();
            }
            catch (Exception )
            {
                 //new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        #endregion

        //private void MakeTabVisable()
        //{

        //    if (!tabControl1.Visible)
        //    {
        //        tabControl1.Visible = true;
        //    }
        //}


        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void mnuServerAdd_Click(object sender, EventArgs e)
        {
            //int index;

            try
            {
                //show the input box
                frmInput frm = new frmInput();

                //prepare for a server URL
                frm.lblInputText.Text = "Please enter the URL of the server to add.";
                frm.txtInputText.Text = "http://";
                frm.ShowDialog();

                // Determine if the OK button was clicked on the dialog box.
                if (frm.DialogResult == DialogResult.OK)
                {
                    InputText = frm.txtInputText.Text;

                    //add to the engen web cache list
                    HubInfo hi = new HubInfo();
                    MoreHubInfo mhi = new MoreHubInfo();
                    hi.mhi = mhi;
                    lock (Stats.gnutella2WebCache)
                    {
                        Stats.gnutella2WebCache.Add(InputText, hi);
                    }
                    frm.Dispose();

                    //update the gui
                    m_cls.AddWebCacheItem(Stats.gnutella2WebCache.IndexOfKey(InputText));
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuServerQuery_Click(object sender, EventArgs e)
        {
            try
            {
                //query the server for hosts
                ListViewItem lvi = lstDiscovery.FocusedItem;
                Sck.OutgoingWebCache(lvi.SubItems[0].Text);
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        private void mnuServerUpdate_Click(object sender, EventArgs e)
        {
            //update the server with own info
            //for now there are rules preventing the GUI to do a manual update
            //see ResetWebCache()
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
             resources = new System.ComponentModel.ComponentResourceManager(typeof(ctrlDiscovery));
            this.lstDiscovery = new System.Windows.Forms.ListView();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
            this.cmnuDiscovery = new System.Windows.Forms.ContextMenu();
            this.mnuServerAdd = new System.Windows.Forms.MenuItem();
            this.mnuServerQuery = new System.Windows.Forms.MenuItem();
            this.mnuServerUpdate = new System.Windows.Forms.MenuItem();
            this.menuServerRemove = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // lstDiscovery
            // 
            this.lstDiscovery.AllowColumnReorder = true;
            this.lstDiscovery.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader5});
            this.lstDiscovery.ContextMenu = this.cmnuDiscovery;
            resources.ApplyResources(this.lstDiscovery, "lstDiscovery");
            this.lstDiscovery.FullRowSelect = true;
            this.lstDiscovery.HideSelection = false;
            this.lstDiscovery.MultiSelect = false;
            this.lstDiscovery.Name = "lstDiscovery";
            this.lstDiscovery.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstDiscovery.UseCompatibleStateImageBehavior = false;
            this.lstDiscovery.View = System.Windows.Forms.View.Details;
            //this.lstDiscovery.SelectedIndexChanged += new System.EventHandler(this.lstDiscovery_SelectedIndexChanged);
            // 
            // columnHeader1
            // 
            resources.ApplyResources(this.columnHeader1, "columnHeader1");
            // 
            // columnHeader2
            // 
            resources.ApplyResources(this.columnHeader2, "columnHeader2");
            // 
            // columnHeader3
            // 
            resources.ApplyResources(this.columnHeader3, "columnHeader3");
            // 
            // columnHeader4
            // 
            resources.ApplyResources(this.columnHeader4, "columnHeader4");
            // 
            // columnHeader5
            // 
            resources.ApplyResources(this.columnHeader5, "columnHeader5");
            // 
            // cmnuDiscovery
            // 
            this.cmnuDiscovery.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuServerAdd,
            this.mnuServerQuery,
            this.mnuServerUpdate,
            this.menuServerRemove});
            // 
            // mnuServerAdd
            // 
            this.mnuServerAdd.Index = 0;
            resources.ApplyResources(this.mnuServerAdd, "mnuServerAdd");
            this.mnuServerAdd.Click += new System.EventHandler(this.mnuServerAdd_Click);
            // 
            // mnuServerQuery
            // 
            this.mnuServerQuery.Index = 1;
            resources.ApplyResources(this.mnuServerQuery, "mnuServerQuery");
            this.mnuServerQuery.Click += new System.EventHandler(this.mnuServerQuery_Click);
            // 
            // mnuServerUpdate
            // 
            this.mnuServerUpdate.Index = 2;
            resources.ApplyResources(this.mnuServerUpdate, "mnuServerUpdate");
            this.mnuServerUpdate.Click += new System.EventHandler(this.mnuServerUpdate_Click);
            // 
            // menuServerRemove
            // 
            this.menuServerRemove.Index = 3;
            resources.ApplyResources(this.menuServerRemove, "menuServerRemove");
            this.menuServerRemove.Click += new System.EventHandler(this.menuServerRemove_Click);
            // 
            // ctrlDiscovery
            // 
            this.Controls.Add(this.lstDiscovery);
            this.Name = "ctrlDiscovery";
            resources.ApplyResources(this, "$this");
            this.ResumeLayout(false);

        }

        #endregion


        private void menuServerRemove_Click(object sender, EventArgs e)
        {
            //int index;

            try
            {
                    //get the current list itm
                    ListViewItem lvi = lstDiscovery.FocusedItem;

                    //Stats.gnutella2WebCache.Add(InputText, hi);
                    Stats.gnutella2WebCache.Remove(lvi.SubItems[0].Text);

                    //update the gui remove itm from list
                    m_cls.DeleteWebCacheItem(lvi.SubItems[0].Text); 

            }
            catch (Exception ex)
            {
                new clsExeption(ex);
                //ErrByte = 1;
            }
        }





    }
}