using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OBJ_FileBus
{
    /// <summary>
    /// 客户端高级版， 没有fileinfo概念，不用想更新问题。
    /// 功能：
    /// 1.读取文件内容
    /// 2.写入文件内容
    /// 3.删除文件
    /// 4.打开文件夹，回调函数通知文件夹内容更新成功
    /// 5.关闭文件夹
    /// </summary>
    public class ClientAdv
    {
        #region 成员
        public Client mClient;

        public FileElement Root {
            get { return mClient.Root; }
        }
        public string RootPath {
            get { return mClient.RootPath; }
            set { mClient.RootPath = value; }
        }

        private List<string> onOpenDirectory = new List<string>();
        #endregion

        public ClientAdv(string rootPath)
        {
            mClient = new Client(rootPath);
        }
        
        //打开文件夹，回调函数通知文件夹内容更新成功
        public void OpenDirectory(string server_path, object sponsor, EventHandler onFresh) 
        {
            //1.读取文件夹内容
            mClient.Read(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (((OBJ_FileBus.Client.ReadEventArgs)e).mErrno == ERRNO.OK)//已经被写入到mClient.Root
                    {
                        onFresh(this, e);

                        //2.读完，注册文件夹推送
                        mClient.PushRegist(((OBJ_FileBus.Client.ReadEventArgs)e).mPath, ((OBJ_FileBus.Client.ReadEventArgs)e).sponsor, true, new EventHandler(delegate(object sender2, EventArgs e2)
                            {
                                if (e2 is OBJ_FileBus.Client.PushRegistEventArgs)
                                {
                                    if (((OBJ_FileBus.Client.PushRegistEventArgs)e2).mErrno == ERRNO.OK) //注册成功 
                                    {
                                        //if(!onOpenDirectory.Contains(server_path))
                                        //    onOpenDirectory.Add(server_path);
                                    }
                                    //onFresh(this, e2);
                                }
                                else//if (fbea is OBJ_FileBus.Client.PushEventArgs)
                                {
                                    //3.被更新，检测时间
                                    if (mClient.Root.SelectSingleNode(((OBJ_FileBus.Client.PushEventArgs)e2).mPath).Update == true)
                                    {
                                        mClient.Read(((OBJ_FileBus.Client.PushEventArgs)e2).mPath, ((OBJ_FileBus.Client.PushEventArgs)e2).sponsor, new EventHandler(delegate(object sender3, EventArgs e3)
                                            {
                                                //4.读取成功与否，都通知
                                                onFresh(this, e3);
                                            }));
                                    }
                                }
                        }));
                }
                else 
                {
                    onFresh(this, e);
                }
            }));
        }
        public void OpenDirectoryWithoutPush(string server_path, object sponsor, EventHandler onFresh) 
        {
            mClient.Read(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
            {
                onFresh(this, e);
            }));
        }
        //关闭文件夹，停止回调
        public void CloseDirectory(string server_path) 
        {
            mClient.PushRegist(server_path, null, false, null);
            //if (!onOpenDirectory.Contains(server_path))
            //    onOpenDirectory.Remove(server_path);
        }

        public void PushRegist(string path, object sponsor, bool isAdd, EventHandler handler) 
        {
            mClient.PushRegist(path, sponsor, isAdd, handler );
        }
        public string GetAbsPath(string path) 
        {
            return mClient.GetAbsPath(path);
        }

        public void FreeComm(object sponsor) 
        {
            mClient.FreeComm(sponsor);
        }
        /// <summary>
        /// 读取文件内容 EventArgs is ReadInfoEventArgs or ReadEventArgs
        /// </summary>
        /// <param name="server_path"></param>
        /// <param name="onFresh"></param>
        public void ReadFile(string server_path, object sponsor, EventHandler onFresh) 
        {
            //1.判断本地文件信息，判断是否需要更新
            FileElement fe = mClient.Root.SelectSingleNode(server_path);
            if (fe == null || //没有此信息
                fe.Update == true || //必须更新
                (!File.Exists(mClient.GetAbsPath(server_path))))//没有已经下载的文件
            {
                // 2.本地文件信息缺失，或需要更新，直接读取远端文件
                mClient.Read(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
                    {
                        //文件读取成功
                        onFresh(this, e);//更新成功,通知
                        
                    }));
            }
            else
            {
                //3. 检测远端文件信息，判断是否需要更新
                mClient.ReadInfo(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
                    {
                        if ((e as OBJ_FileBus.Client.ReadInfoEventArgs).mErrno == ERRNO.OK)
                        {

                            fe = mClient.Root.SelectSingleNode((e as OBJ_FileBus.Client.ReadInfoEventArgs).mPath);
                            if (fe == null || //没有此信息
                                fe.Update == true || //必须更新
                                (!File.Exists(mClient.GetAbsPath((e as OBJ_FileBus.Client.ReadInfoEventArgs).mPath))))//没有已经下载的文件
                            {
                                // 2.本地文件信息缺失，或需要更新，直接读取远端文件
                                mClient.Read((e as OBJ_FileBus.Client.ReadInfoEventArgs).mPath, (e as OBJ_FileBus.Client.ReadInfoEventArgs).sponsor, new EventHandler(delegate(object sender2, EventArgs e2)
                                    {
                                        //文件读取成功
                                        onFresh(this, e2);//更新成功,通知
                                    }));
                            }
                            else
                            {
                                //文件已经是最新了
                                onFresh(this, e);//更新成功,通知
                            }
                        }
                        else
                        {
                            //文件不存在
                            onFresh(this, e);//更新成功,通知
                        }
                    }));
            }
        }
        /// <summary>
        /// 写文件,本地文件 上传到服务器
        /// onFresh 中
        /// EventArgs的类型为OBJ_FileBus.Client.WriteEventArgs
        /// </summary>
        /// <param name="server_path"></param>
        /// <param name="onFresh"></param>
        public void WriteFile(string server_path, object sponsor, EventHandler onFresh) 
        {
            mClient.Write(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
                {
                    onFresh(this, e);
                }));
        }
        public void Del(string server_path, object sponsor, EventHandler onFresh) 
        {
            mClient.Del(server_path, sponsor, new EventHandler(delegate(object sender, EventArgs e)
            {
                onFresh(this, e);
            }));            
        }
    }
}
