using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TABS.MailCarrier.WebSDK.Contact;

/// <summary>
/// 개인 주소록과 공유 주소록 폴더 목록을 반환하는 데이터 소스 클래스입니다.
/// </summary>
public class AddrFolderDataSource
{
    public class FolderItem
    {
        private ContactUtil.Type _type;
        private Guid _id;
        private string _folderName;
        ContactUtil.FolderFlag _flag;
        private int _indent;

        public FolderItem(ContactUtil.Type type, Guid id, string folderName, ContactUtil.FolderFlag flag, int indent)
        {
            _type = type;
            _id = id;
            _folderName = folderName;
            _flag = flag;
            _indent = indent;
        }

        public ContactUtil.Type Type
        {
            get { return _type; }
        }

        public Guid ID
        {
            get { return _id; }
        }

        public string FolderName
        {
            get { return _folderName; }
        }

        public ContactUtil.FolderFlag Flag
        {
            get { return _flag; }
        }

        public int Indent
        {
            get { return _indent; }
        }
    }

    public AddrFolderDataSource()
    {
    }

    public List<FolderItem> GetFolders()
    {
        List<FolderItem> folders = new List<FolderItem>();

        // 개인 주소록
        FolderItem item;
        UserAddressBook userAddrbook = new UserAddressBook(WebUser.CurrentUser.DomainUserID);
        foreach (AddressBook addrbook in userAddrbook.MyAddressBooks)
        {
            AddressBookFolder folder = addrbook.RootFolder;
            item = new FolderItem(ContactUtil.Type.Private, folder.ID, Resources.Contact.ContactsPrivate, ContactUtil.FolderFlag.Root, 0);
            folders.Add(item);
            TraverseSubFolders(ContactUtil.Type.Private, folders, folder, 1);
        }

        // 공유 주소록
        item = new FolderItem(ContactUtil.Type.Share, new Guid(ContactUtil.ShareID), Resources.Contact.ContactsShare, ContactUtil.FolderFlag.Root, 0);
        folders.Add(item);

        foreach (AddressBook addrbook in userAddrbook.ShareAddressBooks)
        {
            if (addrbook.AccessFlag != AddressBook.RsAccessFlag.None)
            {
                AddressBookFolder folder = addrbook.RootFolder;
                item = new FolderItem(ContactUtil.Type.Share, folder.ID, folder.Name, ContactUtil.FolderFlag.Root, 1);
                folders.Add(item);
                TraverseSubFolders(ContactUtil.Type.Share, folders, folder, 2);
            }
        }
        
        return folders;;
    }

    public List<FolderItem> GetAllFolders()
    {
        List<FolderItem> adfolders = GetFolders();

        List<FolderItem> folders = new List<FolderItem>();
        FolderItem item = new FolderItem(ContactUtil.Type.Favorite, new Guid(ContactUtil.FavoritesID), Resources.Contact.ContactsFavorite, ContactUtil.FolderFlag.Root, 0);
        folders.Add(item);
        folders.AddRange(adfolders);

        // 조직도 -> 지원하지 않는다.
        //item = new FolderItem(ContactUtil.Type.Organization, new Guid(ContactUtil.OrgzID), "조직도", ContactUtil.FolderFlag.Root, 0);
        //folders.Add(item);
        //UserAddressBook userAddrBook = new UserAddressBook(WebUser.CurrentUser.DomainUserID);
        //foreach (Organization orgz in userAddrBook.Organizations)
        //{
        //    if (orgz.AccessFlag != Organization.RsAccessFlag.None)
        //    {
        //        item = new FolderItem(ContactUtil.Type.Organization, orgz.ID, orgz.Name, ContactUtil.FolderFlag.Root, 1);
        //        folders.Add(item);
        //    }
        //}

        return folders;
    }

    private void TraverseSubFolders(ContactUtil.Type type, List<FolderItem> folders, AddressBookFolder folder, int indent)
    {
        foreach (AddressBookFolder subfolder in folder.SubFolders)
        {
            FolderItem item = new FolderItem(type, subfolder.ID, subfolder.Name, ContactUtil.FolderFlag.Sub, indent);
            folders.Add(item);
            TraverseSubFolders(type, folders, subfolder, indent + 1);
        }
    }
}
