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

public partial class Contact_View : System.Web.UI.Page
{
    protected string _folderId;
    protected string _page;
    protected Guid _contactId;

    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)
    {
        _folderId = WebParam.GetStrParamValue("folder");
        _page = WebParam.GetStrParamValue("page");
        _contactId = WebParam.GetGuidParamValue("contact");
        if (!IsPostBack)
        {
            Page.Title = AppSetting.GetTitleInfo(Request).Title;

            Contact contact = new Contact(_contactId);

            // 권한 검사
            Guid folderId, addrbookId;
            contact.GetAddressBookAndFolderID(out folderId, out addrbookId);
            AddressBook.RsAccessFlag accessFlag = AddressBook.GetAccessFlag(WebUser.CurrentUser.DomainUserID, addrbookId);
            if ((accessFlag & AddressBook.RsAccessFlag.Read) == 0)
            {
                Response.Redirect("~/Error.aspx?code=3", true);
                return;
            }

            // 권한에 따른 UI 조정
            if ((accessFlag & AddressBook.RsAccessFlag.Write | AddressBook.RsAccessFlag.Admin) == 0)
            {
                BootstrapUtil.EnableButton(modifyToolButton, false);
                BootstrapUtil.EnableButton(deleteToolButton, false);
            }

            // 연락처 데이터 로딩
            contact.Load();
            if (contact.ExistImage)
                IDPhotoImage.ImageUrl = "UserImage.ashx?contact=" + _contactId.ToString();
            DisplayNameLabel.Text = contact.DisplayName;
            CompanyLabel.Text = contact.JobCompany;
            DeptLabel.Text = contact.JobDepartment;
            JobTitleLabel.Text = contact.Title;
            MemoLabel.Text = WebUtil.GetFormattedPlainText(contact.Memo, true);
            if (MemoLabel.Text.Length == 0) BootstrapUtil.CollapseControl(MemoPanel);

            JobPhoneLabel.Text = GetPhoneLink(contact.JobPhone);
            JobFaxLabel.Text = GetPhoneLink(contact.JobFax);
            MobileLabel.Text = GetPhoneLink(contact.HomeMobile);

            EmailLink1.Text = Server.HtmlEncode(contact.Email);
            if (EmailLink1.Text.IndexOf('@') > 0) EmailLink1.Attributes["onclick"] = string.Format("OpenNewMail('{0}', 0);return false;", _contactId);
            EmailLink2.Text = Server.HtmlEncode(contact.Email2);
            if (EmailLink2.Text.IndexOf('@') > 0) EmailLink2.Attributes["onclick"] = string.Format("OpenNewMail('{0}', 1);return false;", _contactId);
            EmailLink3.Text = Server.HtmlEncode(contact.Email3);
            if (EmailLink3.Text.IndexOf('@') > 0) EmailLink3.Attributes["onclick"] = string.Format("OpenNewMail('{0}', 2);return false;", _contactId);
            WebPageLink.Text = Server.HtmlEncode(contact.JobWebpage);
            WebPageLink.NavigateUrl = (WebPageLink.Text.IndexOf("http") == 0) ? WebPageLink.Text : "http://" + WebPageLink.Text;
            if (EmailLink1.Text.Length > 0 || EmailLink2.Text.Length > 0 || EmailLink1.Text.Length > 0) BootstrapUtil.CollapseControl(EmailPanel, false);

            JobZipcodeLabel.Text = contact.JobZipcode;
            JobCityLabel.Text = contact.JobCity;
            JobStateLabel.Text = contact.JobState;
            JobStreetLabel.Text = contact.JobStreet;
            JobCountryLabel.Text = contact.JobCountry;

            HomeZipcodeLabel.Text = contact.HomeZipcode;
            HomeCityLabel.Text = contact.HomeCity;
            HomeStateLabel.Text = contact.HomeState;
            HomeStreetLabel.Text = contact.HomeStreet;
            HomeCountryLabel.Text = contact.HomeCountry;

            FavoriteCheckBox.Checked = Favorites.Contains(WebUser.CurrentUser.DomainUserID, contact);
        }
    }

    protected void FavoriteCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        Contact contact = new Contact(_contactId);
        Favorites favorites = new Favorites(WebUser.CurrentUser.DomainUserID);

        if (FavoriteCheckBox.Checked)
        {
            favorites.AddContact(contact);
        }
        else
        {
            favorites.RemoveContact(contact);
        }
    }

    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        // 권한 검사
        Guid folderId, addrbookId;
        Contact contact = new Contact(_contactId);
        contact.GetAddressBookAndFolderID(out folderId, out addrbookId);
        AddressBook.RsAccessFlag accessFlag = AddressBook.GetAccessFlag(WebUser.CurrentUser.DomainUserID, addrbookId);
        if ((accessFlag & (AddressBook.RsAccessFlag.Write | AddressBook.RsAccessFlag.Admin)) == 0)
        {
            Response.Redirect("~/Error.aspx?code=0", true);
            return;
        }

        contact.Remove();
        WebOperationLog.Write(WebUser.CurrentUser.MailAddress, Request.UserHostAddress, "M/Contact/View.aspx", "Remove", string.Format("FID:{0} CID:{1}", _folderId.ToString(), _contactId.ToString()));
        Response.Redirect(string.Format("List.aspx?folder={0}&page={1}", _folderId, _page), true);
    }

    private string GetPhoneLink(string number)
    {
        string html = "";
        string phoneNumber = number.Trim();
        if (phoneNumber.Length > 0)
        {
            html = string.Format("<a href=\"#\" onclick=\"CallPhone('{0}');return false;\">{0}</a>", Server.HtmlEncode(phoneNumber));
        }
        return html;
    }
}