//zzoi 10mrt 2009

using System;
using System.Collections.Generic;
using System.Text;
using FileScope.Gnutella2;
using FileScope;
using GUIControlLogic.Error;
using ZANZI.Test;
using ZANZIClient;
using ZANZILib.Protocols.QueryCriteria;
using ZANZILib.Protocols.ResultOne;
using ZANZILib.Protocols;
using ZANZILib;
using System.Windows.Forms;

namespace GUIControlLogic
{
    class clsTransfer
    {

        #region Variable declaration

        /// <summary>
        /// Reference to the search frm in the client
        /// </summary>
        /// <remarks>None</remarks>
        private readonly ctrlTransfer m_cls;

        private System.Windows.Forms.ListView downList = new System.Windows.Forms.ListView();


        private System.Windows.Forms.ListView upList = new System.Windows.Forms.ListView();






        #endregion

        public delegate void newDownload(string[] values);
        public delegate void updateDownload(string [] values);
        public delegate void removeDownload(int dlNum);
        public delegate void newUpload(int upNum);
        public delegate void updateUpload(string filename, uint filesize, string status, string percent, string offsets, string speed, int upNum);
        public delegate void removeUpload(int upNum, int type);


        public clsTransfer(ctrlTransfer ctrl)
        {
            m_cls = ctrl;

            //register for events
            GUIBridge.NewDownloadEvent += NewDownload;
            GUIBridge.UpdateDownloadEvent += UpdateDownload ;

            //create the lists


        }

        /// <summary>
        /// Process the new download event from the lib
        /// </summary>
        /// <param name="qht"></param>
        /// <param name="dlNum"></param>
        public void NewDownload(QueryHitTable qht, int dlNum)
        {
            //create the item to add
            string[] items = new string[6];
            QueryHitObject qho = (QueryHitObject)qht.queryHitObjects[0];
            items[0] = qho.fileName;
            items[1] = Utils.Assemble(Convert.ToUInt32(Math.Round((double)qho.fileSize / 1024)), " KB");
            items[2] = "Connecting to " + qht.queryHitObjects.Count.ToString() + " hosts";
            items[3] = "0%";
            items[4] = "0 KB/s";
            items[5] = dlNum.ToString();

            //StartApp.main.transfers.Invoke(new Transfers.newDownload(StartApp.main.transfers.NewDownload), new object[] { qht, dlNum });
            //to run on the GUI thread
            m_cls.BeginInvoke(new clsTransfer.newDownload(this.AddNewDownItem), new object[] { items });
        }

        /// <summary>
        /// add the new downloaditem to the GUI transfer list
        /// </summary>
        /// <param name="qht"></param>
        /// <param name="dlNum"></param>
        public void AddNewDownItem(string[] values)
        {
            //m_cls.listViewDowns.



            ListViewItem lvi = new ListViewItem(values);

            //add the item
            //m_cls.listViewDowns.ForeColor = Stats.settings.clHighlight4;
            m_cls.listViewDowns.Items.Add(lvi);

            //downList.Items.Add(lvi);
            //m_cls.listViewDowns.Refresh();

        }

        /// <summary>
        /// Update the status of transfer, percentage of file done, and speed of transfer.
        /// </summary>
        public void UpdateDownload(ref string status, ref string percent, ref string speed, int dlNum)
        {

            //create the item to add
            string[] items = new string[6];
            items[2] = status;
            items[3] = percent;
            items[4] = speed;
            items[5] = dlNum.ToString();

            //StartApp.main.transfers.Invoke(new Transfers.updateDownload(StartApp.main.transfers.UpdateDownload), new object[] { status, percent, speed, dlNum });
            m_cls.BeginInvoke(new clsTransfer.updateDownload(this.UpdateDownItem), new object[] { items });

            return;
            
        }

        /// <summary>
        /// Updates the download item values
        /// </summary>
        /// <param name="values"></param>
        public void UpdateDownItem(string[] values)
        {
            foreach (ListViewItem lvi in m_cls.listViewDowns.Items)
                if (lvi.SubItems[5].Text == values[5])
                {
                    lvi.SubItems[2].Text = values[2];
                    lvi.SubItems[3].Text = values[3];
                    lvi.SubItems[4].Text = values[4];

                }
                else
                {
                    //the item is not there
                }
            m_cls.listViewDowns.Invalidate();


            //foreach (ListViewItem lvi in this.upList.Items)
            //    if (lvi.SubItems[5].Text == values[5])
            //    {
            //        lvi.SubItems[2].Text = values[2];
            //        lvi.SubItems[3].Text = values[3];
            //        lvi.SubItems[4].Text = values[4];

            //    }
            //    else
            //    {
            //        //the item is not there
            //    }


        }


        public void RemoveDownload(int dlNum)
        {
            //do any other logic processing here

            m_cls.BeginInvoke(new clsTransfer.removeDownload(this.RemoveDownItem), new object[] { dlNum });

            return;
        }
        /// <summary>
        /// Removes the download item from the list
        /// </summary>
        /// <param name="dlNum">The downloaditem number to remove</param>
        public void RemoveDownItem(int dlNum)
        {


            foreach (ListViewItem lvi in m_cls.listViewDowns.Items)
                if (lvi.SubItems[5].Text == dlNum.ToString())
                {
                    if (Stats.settings.clearDl)
                        m_cls.listViewDowns.Items.Remove(lvi);
                    else
                    {
                        lvi.SubItems[2].Text = "Completed";
                        lvi.SubItems[3].Text = "100%";
                        lvi.SubItems[4].Text = "";
                        lvi.SubItems[5].Text = "";
                    }
                    
                    return;
                }


            m_cls.listViewDowns.Invalidate();




        }


        /// <summary>
        /// Cancel all selected downloads.
        /// </summary>
        void DownCancel(bool delete)
        {
            //if (listViewDowns.SelectedItems.Count == 0)
            //    return;

            ////show alert if enabled
            //if (Stats.settings.cancelDLAlert)
            //{
            //    DialogResult dr = MessageBox.Show("Are you sure you want to cancel the download(s)?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //    if (dr.ToString().ToLower() == "no")
            //        return;
            //}

            //foreach (ListViewItem lvi in listViewDowns.SelectedItems)
            //    if (lvi.SubItems[5].Text != "")
            //    {
            //        if (Stats.settings.clearDl)
            //        {
            //            int dlNum = Convert.ToInt32(lvi.SubItems[5].Text);
            //            DownloadManager.dms[dlNum].CancelDownload(delete);
            //            listViewDowns.Items.Remove(lvi);
            //        }
            //        else
            //        {
            //            int dlNum = Convert.ToInt32(lvi.SubItems[5].Text);
            //            DownloadManager.dms[dlNum].CancelDownload(delete);
            //            lvi.SubItems[2].Text = "Cancelled";
            //            lvi.SubItems[4].Text = "";
            //            lvi.SubItems[5].Text = "";
            //        }
            //    }
            //SetupToolbarDowns();
        }


        #region Properties

        public ListView UpList
        {
            get { return upList; }
            set { upList = value; }
        }

        public ListView DownList
        {
            get { return downList; }
            set { downList = value; }
        }

        #endregion


    }
}
