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_SelectOrgz : 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;
            LoadOrgzBookList();
            if (OrgzList.Items.Count > 0)
                LoadTopParts(new Guid(OrgzList.SelectedValue));
        }
    }

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

        // 조직도
        UserAddressBook userAddrBook = new UserAddressBook(WebUser.CurrentUser.DomainUserID);
        foreach (Organization orgz in userAddrBook.Organizations)
        {
            if (orgz.AccessFlag != Organization.RsAccessFlag.None)
            {
                ListItem item = new ListItem();
                item.Value = orgz.ID.ToString();
                item.Text = orgz.Name;
                items.Add(item);
            }
        }

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

    /// <summary>
    /// 최상위 파트 정보를 읽어 트리를 구성한다.
    /// </summary>
    /// <param name="orgzId">조직도 ID</param>
    private void LoadTopParts(Guid orgzId)
    {
        Organization orgz = new Organization(orgzId);
        orgz.Load(false);

        OrgzTree.Nodes.Clear();
        foreach (OrganizationPart part in orgz.Parts)
        {
            TreeNode partNode = new TreeNode(part.Name, part.ID.ToString(), "~/Images/Contact/part_16.gif");
            partNode.PopulateOnDemand = true;
            partNode.SelectAction = TreeNodeSelectAction.SelectExpand;
            OrgzTree.Nodes.Add(partNode);
        }
    }

    protected void OrgzTree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        Guid partId = new Guid(e.Node.Value);
        OrganizationPart part = new OrganizationPart(partId);
        part.Load();

        if (AppSetting.OrgzListPartFirst)
        {
            TraversePart(e, part);
            TraverseMember(e, part);
        }
        else
        {
            TraverseMember(e, part);
            TraversePart(e, part);
        }
    }

    private static void TraversePart(TreeNodeEventArgs e, OrganizationPart part)
    {
        foreach (OrganizationPart subPart in part.Parts)
        {
            TreeNode subPartNode = new TreeNode(subPart.Name, subPart.ID.ToString(), "~/Images/Contact/part_16.gif");
            subPartNode.PopulateOnDemand = true;
            subPartNode.SelectAction = TreeNodeSelectAction.SelectExpand;
            e.Node.ChildNodes.Add(subPartNode);
        }
    }

    private static void TraverseMember(TreeNodeEventArgs e, OrganizationPart part)
    {
        foreach (OrganizationMember member in part.Members)
        {
            TreeNode memberNode = new TreeNode(string.Format("{0} {1}", member.Title, member.Name), member.ID.ToString(), "~/Images/Contact/member_16.gif");
            memberNode.SelectAction = TreeNodeSelectAction.Select;
            e.Node.ChildNodes.Add(memberNode);
        }
    }

    protected void OrgzTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        //OrgzItemName.Value = OrgzTree.SelectedNode.Text.Split(' ')[1];
        OrgzItemName.Value = OrgzTree.SelectedNode.Text;
        OrgzItemID.Value = OrgzTree.SelectedNode.Value;
    }

    protected void OrgzList_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadTopParts(new Guid(OrgzList.SelectedValue));
    }

    
}