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

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for ctrlDiscovery.
    /// </summary>
    public class ctrlHosts : UserControl, Ictrl
    {
        private readonly clsHosts m_cls;
        private ContextMenu cmnuHosts;
        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 lstHost;
        private MenuItem mnuHostRemove;
        private MenuItem mnuHostAdd;
        private MenuItem mnuHostConnect;

        //zzoi localized the hardcoded tabheader string
        //private string tabHeader;
        private ComponentResourceManager resources;
        // = new System.ComponentModel.ComponentResourceManager(typeof(ctrlDiscovery));


        public ctrlHosts(clsHosts 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
            //Bug -  the ide sometimes declare the resource variable local to the initializecomponent
            //function and a error occurs here
            //to fix this remove the new variable declaration on the first line of that function
            get { return resources.GetString("tabHeader.Text"); }
            //get{ return  "Host cache";}
            //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");


                //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");

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


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

        #endregion

        public void mnuHostAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //show the input box
                frmInput frm = new frmInput();

                //prepare for a Host URL
                frm.lblInputText.Text = "Please enter the IP:Port of the Host 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;

                    //break the adderss apart
                    string address = InputText.Substring(0, InputText.IndexOf(":"));
                    IPAddress ipa = IPAddress.Parse(address);
                    ushort port = Convert.ToUInt16(InputText.Substring(InputText.IndexOf(":") + 1));

                    //add to the engen host cache list
                    HubInfo hi = new HubInfo();
                    MoreHubInfo mhi = new MoreHubInfo();
                    hi.mhi = mhi;
                    hi.port = port;

                    lock (HostCache.hubCache)
                    {
                        if (ipa != null) HostCache.hubCache.Add(ipa, hi);
                    }
                    frm.Dispose();

                    //update the gui
                    if (ipa != null) m_cls.AddHostCacheItem(HostCache.hubCache.IndexOfKey(ipa));
                }
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        public void mnuHostConnect_Click(object sender, EventArgs e)
        {
            try
            {
                ListViewItem lvi = lstHost.FocusedItem;
                Sck.Outgoing(lvi.SubItems[0].Text + ":" + lvi.SubItems[1].Text);
            }
            catch (Exception  ex)
            {
                 new clsExeption(ex);
                //ErrByte = 1;
            }
        }

        /// <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 cmnuHosts_Popup(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Removes the selected host from the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuHostRemove_Click(object sender, EventArgs e)
        {
            try
            {
                //get the current list itm
                ListViewItem lvi = lstHost.FocusedItem;

                //remove from cache in lib
                //IPAddress key = (IPAddress)lvi.Tag ;
                IPAddress key = IPAddress.Parse(lvi.SubItems[0].Text);
                
                HostCache.hubCache.Remove(key); //the key value

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

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

        #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(ctrlHosts));
            this.lstHost = new System.Windows.Forms.ListView();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
            this.cmnuHosts = new System.Windows.Forms.ContextMenu();
            this.mnuHostAdd = new System.Windows.Forms.MenuItem();
            this.mnuHostConnect = new System.Windows.Forms.MenuItem();
            this.mnuHostRemove = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // lstHost
            // 
            this.lstHost.AccessibleDescription = null;
            this.lstHost.AccessibleName = null;
            resources.ApplyResources(this.lstHost, "lstHost");
            this.lstHost.AllowColumnReorder = true;
            this.lstHost.BackgroundImage = null;
            this.lstHost.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader2,
            this.columnHeader5});
            this.lstHost.FullRowSelect = true;
            this.lstHost.HideSelection = false;
            this.lstHost.MultiSelect = false;
            this.lstHost.Name = "lstHost";
            this.lstHost.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstHost.UseCompatibleStateImageBehavior = false;
            this.lstHost.View = System.Windows.Forms.View.Details;
            // 
            // columnHeader1
            // 
            resources.ApplyResources(this.columnHeader1, "columnHeader1");
            // 
            // columnHeader3
            // 
            resources.ApplyResources(this.columnHeader3, "columnHeader3");
            // 
            // columnHeader4
            // 
            resources.ApplyResources(this.columnHeader4, "columnHeader4");
            // 
            // columnHeader2
            // 
            resources.ApplyResources(this.columnHeader2, "columnHeader2");
            // 
            // columnHeader5
            // 
            resources.ApplyResources(this.columnHeader5, "columnHeader5");
            // 
            // cmnuHosts
            // 
            this.cmnuHosts.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuHostAdd,
            this.mnuHostConnect,
            this.mnuHostRemove});
            resources.ApplyResources(this.cmnuHosts, "cmnuHosts");
            this.cmnuHosts.Popup += new System.EventHandler(this.cmnuHosts_Popup);
            // 
            // mnuHostAdd
            // 
            resources.ApplyResources(this.mnuHostAdd, "mnuHostAdd");
            this.mnuHostAdd.Index = 0;
            this.mnuHostAdd.Click += new System.EventHandler(this.mnuHostAdd_Click);
            // 
            // mnuHostConnect
            // 
            resources.ApplyResources(this.mnuHostConnect, "mnuHostConnect");
            this.mnuHostConnect.Index = 1;
            this.mnuHostConnect.Click += new System.EventHandler(this.mnuHostConnect_Click);
            // 
            // mnuHostRemove
            // 
            resources.ApplyResources(this.mnuHostRemove, "mnuHostRemove");
            this.mnuHostRemove.Index = 2;
            this.mnuHostRemove.Click += new System.EventHandler(this.mnuHostRemove_Click);
            // 
            // ctrlHosts
            // 
            this.AccessibleDescription = null;
            this.AccessibleName = null;
            resources.ApplyResources(this, "$this");
            this.BackgroundImage = null;
            this.ContextMenu = this.cmnuHosts;
            this.Controls.Add(this.lstHost);
            this.Font = null;
            this.Name = "ctrlHosts";
            this.ResumeLayout(false);

        }

        #endregion
    }
}