using System;
using System.Collections;
using System.Windows.Forms;

using DigitalPlatform.Xml;

namespace DigitalPlatform.DTLP
{
    /*
	// Í¨Ñ¶ÐÝÏÐÊ±¿Ì
	// return:
	//		0	¼ÌÐø
	//		1	Í£Ö¹
	public delegate int Delegate_Idle(HostEntry entry);
    */

    /*
	// »ñµÃÈ±Ê¡ÕÊ»§ÐÅÏ¢
	// return:
	//		2	already login succeed
	//		1	dialog return OK
	//		0	dialog return Cancel
	//		-1	other error
	public delegate int Delegate_AskAccountInfo(DtlpChannel channel, 
	string strPath,
	out IWin32Window owner,	// Èç¹ûÐèÒª³öÏÖ¶Ô»°¿ò£¬ÕâÀï·µ»Ø¶Ô»°¿òµÄËÞÖ÷Form
	out string strUserName,
	out string strPassword);
     * */

	/// <summary>
	/// Summary description for ChannelArray.
	/// </summary>
	public class DtlpChannelArray : ArrayList
	{
		public ApplicationInfo appInfo = null;

		// »ñµÃÈ±Ê¡ÕË»§ÃûµÈÐÅÏ¢µÄ»Øµ÷º¯ÊýµØÖ·
		// public Delegate_AskAccountInfo procAskAccountInfo = null;

        public event AskDtlpAccountInfoEventHandle AskAccountInfo = null;


		// Í¨Ñ¶ÐÝÏÐ×´Ì¬»Øµ÷º¯Êý
		// public Delegate_Idle procIdle = null;
        public event DtlpIdleEventHandler Idle = null;

        public bool GUI = true;

		public DtlpChannelArray()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        public bool DoIdle(object sender)
        {
            System.Threading.Thread.Sleep(1);	// ±ÜÃâCPU×ÊÔ´¹ý¶ÈºÄ·Ñ

            bool bDoEvents = true;
            if (this.Idle != null)
            {
                DtlpIdleEventArgs e = new DtlpIdleEventArgs();
                this.Idle(sender, e);
                if (e.Stop == true)
                    return true;
                bDoEvents = e.bDoEvents;
            }

            if (bDoEvents == true)
            {
                try
                {
                    Application.DoEvents();	// ³öÈÃ½çÃæ¿ØÖÆÈ¨
                }
                catch
                {
                }
            }

            System.Threading.Thread.Sleep(1);	// ±ÜÃâCPU×ÊÔ´¹ý¶ÈºÄ·Ñ

            return false;
        }

		// ´´½¨Ò»¸öÐÂÍ¨µÀ
		public DtlpChannel CreateChannel(int usrid)
		{
			DtlpChannel channel = new DtlpChannel();

			channel.Container = this;
			channel.m_lUsrID = usrid;
			channel.InitialHostArray();
	
			this.Add(channel);

			return channel;
		}

		public bool DestroyChannel(DtlpChannel channel)
		{
			channel.Cancel();
			this.Remove(channel);
			return true;
		}

        // ÊÇ·ñÒÑ¾­¹Ò½ÓÁËÊÂ¼þ£¿
        public bool HasAskAccountInfoEventHandler
        {
            get
            {
                return (this.AskAccountInfo != null);
            }
        }

        // µ÷ÓÃÊÂ¼þ
        public void CallAskAccountInfo(object obj,
            AskDtlpAccountInfoEventArgs e)
        {
            if (this.AskAccountInfo == null)
                return;

            this.AskAccountInfo(obj, e);
        }

	}

    // ÊÂ¼þ: Ñ¯ÎÊÕÊ»§ÐÅÏ¢
    public delegate void AskDtlpAccountInfoEventHandle(object sender,
    AskDtlpAccountInfoEventArgs e);

    public class AskDtlpAccountInfoEventArgs : EventArgs
    {
        // ÊäÈë²ÎÊý
        public DtlpChannel Channel = null;
        public string Path = "";

        // Êä³ö²ÎÊý
        public IWin32Window Owner = null;	// Èç¹ûÐèÒª³öÏÖ¶Ô»°¿ò£¬ÕâÀï·µ»Ø¶Ô»°¿òµÄËÞÖ÷Form
        public string UserName = "";
        public string Password = "";

        public int Result = 0;
        // Result:
        //		2	already login succeed
        //		1	dialog return OK
        //		0	dialog return Cancel
        //		-1	other error
        public string ErrorInfo = "";
    }

    public delegate void DtlpIdleEventHandler(object sender,
DtlpIdleEventArgs e);


    public class DtlpIdleEventArgs : EventArgs
    {
        public bool bDoEvents = true;
        public bool Stop = false; 
    }
}
