using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TABS.MailCarrier.WebSDK.Contact;

public partial class Contact_SelectRcpt : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string defLang = WebUser.CurrentUser.UiLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(defLang);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(defLang);
        base.InitializeCulture();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        WebUtil.SetMcCommonCss(Page.Header);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Title = AppSetting.GetTitleInfo(Request).Title;
            LoadAddrBookList();
        }

        string selectedValue = AddrBookList.SelectedValue;
        if (selectedValue == ContactUtil.FavoritesID)
        {
            ContactGridView.DataSourceID = "FavoriteDataSource";
        }
        else
        {
            ContactGridView.DataSourceID = "ContactDataSource";
            ContactDataSource.SelectParameters[0].DefaultValue = selectedValue;
        }
    }

    protected void ContactGridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        if ((gv.ShowHeader == true && gv.Rows.Count > 0) || (gv.ShowHeaderWhenEmpty == true))
        {
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    private void LoadAddrBookList()
    {
        List<ListItem> items = new List<ListItem>();

        // 자주 사용하는 연락처
        ListItem item = new ListItem();
        item.Value = ContactUtil.FavoritesID;
        item.Text = string.Format("[ {0} ]", Resources.Contact.ContactsFavorite);
        items.Add(item);

        // 개인 주소록
        UserAddressBook userAddrBook = new UserAddressBook(WebUser.CurrentUser.DomainUserID);
        foreach (AddressBook addrbook in userAddrBook.MyAddressBooks)
        {
            AddressBookFolder folder = addrbook.RootFolder;
            item = new ListItem();
            item.Value = folder.ID.ToString();
            item.Text = (folder.Name == "개인 주소록") ? string.Format("[ {0} ]", Resources.Contact.ContactsPrivate) : folder.Name;
            items.Add(item);
            TraverseSubFolders(items, folder, 1);
        }

        // 공유 주소록
        item = new ListItem();
        item.Value = ContactUtil.ShareID;
        item.Text = string.Format("[ {0} ]", Resources.Contact.ContactsShare); ;
        items.Add(item);

        string indentStr = HttpUtility.HtmlDecode("&#160;&#160;");  // &nbsp; 코드
        foreach (AddressBook addrbook in userAddrBook.ShareAddressBooks)
        {
            if (addrbook.AccessFlag != AddressBook.RsAccessFlag.None)
            {
                AddressBookFolder folder = addrbook.RootFolder;
                item = new ListItem();
                item.Value = folder.ID.ToString();
                item.Text = indentStr + folder.Name;
                items.Add(item);
                TraverseSubFolders(items, folder, 2);
            }
        }

        AddrBookList.DataSource = items;
        AddrBookList.DataTextField = "Text";
        AddrBookList.DataValueField = "Value";
        AddrBookList.DataBind();
    }

    private void TraverseSubFolders(List<ListItem> items, AddressBookFolder folder, int indent)
    {
        string indentStr = HttpUtility.HtmlDecode("&#160;&#160;");
        foreach (AddressBookFolder subfolder in folder.SubFolders)
        {
            ListItem item = new ListItem();
            item.Value = subfolder.ID.ToString();
            string indentText = "";
            for (int i = 0; i < indent; i++) indentText += indentStr;
            item.Text = indentText + subfolder.Name;
            items.Add(item);
            TraverseSubFolders(items, subfolder, indent + 1);
        }
    }

    protected string GetContact(Contact.ContactType type, string displayName, string email)
    {
        if (type == Contact.ContactType.Contact)
        {
            return string.Format("\"{0}\" ({1})", Server.HtmlEncode(displayName), email);
        }
        else
        {
            return string.Format("@[{0}]", Server.HtmlEncode(displayName));
        }
    }

    protected string GetContactValue(Contact.ContactType type, string email, Guid id)
    {
        return (type == Contact.ContactType.Contact) ? Server.HtmlEncode(email) : id.ToString();
    }
}