// frmSearch.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 System.Collections;
using GUIControlLogic;
using ZANZILib.Protocols.QueryCriteria;
using ZANZILib.Protocols.ResultOne;
using FileScope.Gnutella2;
using FileScope;
using System.Diagnostics;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for frmSearch.
    /// </summary>
    public class frmSearch : Form, ZANZI.ctrl.Ictrl
    {
        private IContainer components;

        #region Variable declaration

        public clsMain m_clsMain;
        private readonly clsSearch clsManager;
        private Button btnSearch;
        internal DataGrid grdResultOne;
        internal long m_CurrentResultRecordGUID;
        internal clsZanziProtResultOne m_ResultOneObj;
        private MenuItem mnuDownloadFile;
        private MenuItem menuItem5;
        private MenuItem mnuChat;
        private MenuItem mnuCompare;
        private ContextMenu mnuContextDataGrid;
        private MenuItem mnuExchangeRates;
        private MenuItem mnuVoice;
        //This is the protocol currently in use
        internal clsZanziProt objSearchCriteria = new clsZanziProt();
        private Panel panel1;
        private Panel panel2;
        internal PropertyGrid propertyGrid1;
        private Splitter splitter1;
        //internal ListView m_lstv;

        //zzoi 29feb 2009
        //public delegate void G2QueryHitDelegate(QueryHitObject qho, QueryHitTable qht, ref string searchName);
        public delegate void addNewItem(clsZanziProtResultOne ResultObj);

        #endregion

        #region Functions

        /// <summary>Main entry for frmSearch</summary>
        /// <returns>Returns nothing</returns>
        /// <param name="frm">Used to pass the MDIParent as reference</param>
        /// <remarks>frmSearch is the constructor in the frmSearch class.  
        /// The <paramref name="frm"/> parameter takes a reference to frmMain.
        /// </remarks>
        public frmSearch(clsMain frm, int count)
        {
            //
            // Required for Windows Form Designer support
            //
            m_clsMain = frm;
            //this.MdiParent =frm;
            InitializeComponent();

            //Load the default protocol structure from ZANZILib
            propertyGrid1.SelectedObject = objSearchCriteria;

            //create a manager class for this frm
            clsManager = new clsSearch(this);

            //set the caption to reflect doc count
            Text += count;
        }

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

        public string SearchString()
        {
            return clsManager.SearchString;
        }

        #endregion

        #region Windows Form 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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmSearch));
            this.panel1 = new System.Windows.Forms.Panel();
            this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
            this.btnSearch = new System.Windows.Forms.Button();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.grdResultOne = new System.Windows.Forms.DataGrid();
            this.mnuContextDataGrid = new System.Windows.Forms.ContextMenu();
            this.mnuChat = new System.Windows.Forms.MenuItem();
            this.mnuVoice = new System.Windows.Forms.MenuItem();
            this.mnuExchangeRates = new System.Windows.Forms.MenuItem();
            this.mnuCompare = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.mnuDownloadFile = new System.Windows.Forms.MenuItem();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdResultOne)).BeginInit();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.AccessibleDescription = null;
            this.panel1.AccessibleName = null;
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.BackgroundImage = null;
            this.panel1.Controls.Add(this.propertyGrid1);
            this.panel1.Controls.Add(this.btnSearch);
            this.panel1.Font = null;
            this.panel1.Name = "panel1";
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.AccessibleDescription = null;
            this.propertyGrid1.AccessibleName = null;
            resources.ApplyResources(this.propertyGrid1, "propertyGrid1");
            this.propertyGrid1.BackgroundImage = null;
            this.propertyGrid1.Font = null;
            this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
            this.propertyGrid1.Name = "propertyGrid1";
            // 
            // btnSearch
            // 
            this.btnSearch.AccessibleDescription = null;
            this.btnSearch.AccessibleName = null;
            resources.ApplyResources(this.btnSearch, "btnSearch");
            this.btnSearch.BackgroundImage = null;
            this.btnSearch.Cursor = System.Windows.Forms.Cursors.Arrow;
            this.btnSearch.Font = null;
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            // 
            // splitter1
            // 
            this.splitter1.AccessibleDescription = null;
            this.splitter1.AccessibleName = null;
            resources.ApplyResources(this.splitter1, "splitter1");
            this.splitter1.BackgroundImage = null;
            this.splitter1.Font = null;
            this.splitter1.Name = "splitter1";
            this.splitter1.TabStop = false;
            // 
            // grdResultOne
            // 
            this.grdResultOne.AccessibleDescription = null;
            this.grdResultOne.AccessibleName = null;
            resources.ApplyResources(this.grdResultOne, "grdResultOne");
            this.grdResultOne.BackgroundImage = null;
            this.grdResultOne.CaptionFont = null;
            this.grdResultOne.ContextMenu = this.mnuContextDataGrid;
            this.grdResultOne.DataMember = "";
            this.grdResultOne.Font = null;
            this.grdResultOne.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.grdResultOne.Name = "grdResultOne";
            this.grdResultOne.ReadOnly = true;
            this.grdResultOne.CurrentCellChanged += new System.EventHandler(this.grdResultOne_CurrentCellChanged);
            this.grdResultOne.Click += new System.EventHandler(this.grdResultOne_Click);
            // 
            // mnuContextDataGrid
            // 
            this.mnuContextDataGrid.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuChat,
            this.mnuVoice,
            this.mnuExchangeRates,
            this.mnuCompare,
            this.mnuDownloadFile});
            resources.ApplyResources(this.mnuContextDataGrid, "mnuContextDataGrid");
            // 
            // mnuChat
            // 
            resources.ApplyResources(this.mnuChat, "mnuChat");
            this.mnuChat.Index = 0;
            // 
            // mnuVoice
            // 
            resources.ApplyResources(this.mnuVoice, "mnuVoice");
            this.mnuVoice.Index = 1;
            // 
            // mnuExchangeRates
            // 
            resources.ApplyResources(this.mnuExchangeRates, "mnuExchangeRates");
            this.mnuExchangeRates.Index = 2;
            // 
            // mnuCompare
            // 
            resources.ApplyResources(this.mnuCompare, "mnuCompare");
            this.mnuCompare.Index = 3;
            this.mnuCompare.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem5});
            // 
            // menuItem5
            // 
            resources.ApplyResources(this.menuItem5, "menuItem5");
            this.menuItem5.Index = 0;
            // 
            // mnuDownloadFile
            // 
            resources.ApplyResources(this.mnuDownloadFile, "mnuDownloadFile");
            this.mnuDownloadFile.Index = 4;
            this.mnuDownloadFile.Click += new System.EventHandler(this.mnuDownloadFile_Click);
            // 
            // panel2
            // 
            this.panel2.AccessibleDescription = null;
            this.panel2.AccessibleName = null;
            resources.ApplyResources(this.panel2, "panel2");
            this.panel2.BackgroundImage = null;
            this.panel2.Font = null;
            this.panel2.Name = "panel2";
            // 
            // frmSearch
            // 
            this.AccessibleDescription = null;
            this.AccessibleName = null;
            resources.ApplyResources(this, "$this");
            this.BackgroundImage = null;
            this.Controls.Add(this.grdResultOne);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel1);
            this.Font = null;
            this.Icon = null;
            this.Name = "frmSearch";
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.grdResultOne)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        #region Events

        private void btnSearch_Click(object sender, EventArgs e)
        {
//			//Pass the query object to the server
//
            //	ZANZI.Test.clsTest cls = new ZANZI.Test.clsTest();
//
            //grdResultOne.DataSource = cls.GetTestData();
//			//grdResultOne
            
            clsManager.DoSearch(objSearchCriteria, grdResultOne);
        }


        private void grdResultOne_CurrentCellChanged(object sender, EventArgs e)
        {
            clsManager.grdResultCurrentCellChanged(grdResultOne);
            //grdResultOne.Select(grdResultOne.CurrentRowIndex);
            //grdResultOne.CurrentCell  = new DataGridCell(0,0);
            //DataGridCell cell = sender;
            //System.EventArgs ev = e;
            //	ev.
            //cell.
        }

        private void grdResultOne_Click(object sender, EventArgs e)
        {
            //clsManager.grdResultClick(this.lstCharacteristics, (ZANZILib.Protocols.QueryCriteria.clsZanziProt ) this.propertyGrid1.SelectedObject );
        }


//		
//		private void btnSearch_Click(object sender, System.EventArgs e)
//		{
//			//Pass the query object to the server
//
//			ZANZI.Test.clsResults cls = new ZANZI.Test.clsResults();
//
//			grdResultOne.DataSource = cls.GetTestData();
//			//grdResultOne
//
//		}
/// <summary>
/// populates the resultgrid with new searchresult object
/// </summary>
/// <param name="zResObj">a Single clsZanziProtResultOne</param>
        public void AddNewItem(clsZanziProtResultOne zResObj)
        {

            try
            {
                if (this.grdResultOne.DataSource != null)
                {
                  //nothing at persent
                }
                else
                {
                    //ArrayList al = new ArrayList();
                    //al.Add(new clsZanziProtResultOne());
                    //this.grdResultOne.DataSource = al;


                    this.grdResultOne.DataSource = new ArrayList();

                }
                //((ArrayList)this.grdResultOne.DataSource).Add(zResObj);
                ArrayList all = ((ArrayList)this.grdResultOne.DataSource);
                all.Add(zResObj);
                this.grdResultOne.DataSource = null;
                this.grdResultOne.DataSource = all;

                //this.grdResultOne.datab
                this.Invalidate();
                this.grdResultOne.Refresh();
                this.Refresh();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("addNewItem frmSearch error" + ex.Message);
            }
        }

        #endregion

        #region Ictrl Members


        public void ResetCulture()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public bool StaticControl
        {
            //get { throw new Exception("The method or operation is not implemented."); }
            get { return false; }
        }

        #endregion

        private void mnuDownloadFile_Click(object sender, EventArgs e)
        {
            try
            {
                //zzoi 13 mrt 2009 open the transfer frm if not open
                m_clsMain.mnuViewTransfers_Click(null, null);
                clsManager.DoDownloadFile();
               
            }
            catch (Exception )
            {
                //new clsExeption(ex);
                //ErrByte = 1;
            }
        }
    }
}
