using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Telerik.Web.UI;
using System.Collections.Generic;
using System.IO;

/// <summary>
/// Summary description for Directories
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Directories : System.Web.Services.WebService
{

    public Directories()
    {
        //
    }

    [WebMethod]
    public NodeData[] GetDirectories(RadTreeNodeData node)
    {
        return BindDirectory(node.Value).ToArray();
    }

    [WebMethod]
    public List<Info> GetFilesAndFolders(string dirPath)
    {
        List<Info> list = new List<Info>();

        string[] dirs = Directory.GetDirectories(dirPath);
        foreach (string path in dirs)
        {
            Info info = new Info();
            info.Name = Path.GetFileName(path);
            list.Add(info);
        }

        DirectoryInfo dir = new DirectoryInfo(dirPath);

        foreach (FileInfo file in dir.GetFiles())
        {
            Info info = new Info();
            info.Name = file.Name;
            info.Size = file.Length;
            list.Add(info);
        }

        return list;
    }

    public List<NodeData> BindDirectory(string dirPath)
    {
        List<NodeData> nodes = new List<NodeData>();

        string[] dirs = Directory.GetDirectories(dirPath);
        foreach (string path in dirs)
        {
            NodeData node = new NodeData();
            node.Text = Path.GetFileName(path);
            node.Value = path;

            if (Directory.GetDirectories(path).Length > 0)
            {
                node.ExpandMode = TreeNodeExpandMode.WebService;
            }

            node.ImageUrl = "Img/mailfolder.gif";

            nodes.Add(node);
        }

        return nodes;
    }

    [Serializable]
    public class NodeData
    {
        private string _text;
        private string _value;
        private string _imageurl;
        private TreeNodeExpandMode _expandMode;
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public string ImageUrl
        {
            get { return _imageurl; }
            set { _imageurl = value; }
        }

        public TreeNodeExpandMode ExpandMode
        {
            get { return _expandMode; }
            set { _expandMode = value; }
        }
    }

    public class Info
    {
        private string _name;
        private long? _size;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public long? Size
        {
            get { return _size; }
            set { _size = value; }
        }
    }
}
