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;
using TABS.MailCarrier.WebSDK.Contact;
using TABS.MailCarrier.WebSDK.User;

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

    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.GetGuidParamValue("folder");
        _page = WebParam.GetStrParamValue("page");
        _contactId = WebParam.GetGuidParamValue("contact");
        _mode = (_contactId != Guid.Empty) ? "update" : (_folderId != Guid.Empty) ? "create" : "";

        if (!IsPostBack)
        {
            Page.Title = AppSetting.GetTitleInfo(Request).Title;

            JobPhoneTextBox.Attributes["type"] = "tel";
            HomeMobileTextBox.Attributes["type"] = "tel";
            JobFaxTextBox.Attributes["type"] = "tel";
            EmailTextBox1.Attributes["type"] = "email";
            EmailTextBox2.Attributes["type"] = "email";
            EmailTextBox3.Attributes["type"] = "email";

            if (_contactId != Guid.Empty)
            {
                // 연락처 수정
                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.Write) == 0)
                {
                    Response.Redirect("~/Error.aspx?code=5", true);
                }

                LoadContact(contact);
                SaveButton.Attributes["onclick"] = "UpdateContact()";
            }
            else if (_folderId != Guid.Empty)
            {
                // 새 연락처
                // 권한 검사
                AddressBookFolder folder = new AddressBookFolder(_folderId);
                AddressBook.RsAccessFlag accessFlag = AddressBook.GetAccessFlag(WebUser.CurrentUser.DomainUserID, folder.AddressBookID);
                if ((accessFlag & AddressBook.RsAccessFlag.Write) == 0)
                {
                    Response.Redirect("~/Error.aspx?code=5", true);
                }
                SaveButton.Attributes["onclick"] = "SaveContact()";
            }
        }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        if (_mode == "update")
        {
            // 권한 검사
            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.Write) == 0)
            {
                Response.Redirect("~/Error.aspx?code=0", true);
            }

            UpdateContact();
        }
        else if (_mode == "create")
        {
            // 권한 검사
            AddressBookFolder folder = new AddressBookFolder(_folderId);
            AddressBook.RsAccessFlag accessFlag = AddressBook.GetAccessFlag(WebUser.CurrentUser.DomainUserID, folder.AddressBookID);
            if ((accessFlag & AddressBook.RsAccessFlag.Write) == 0)
            {
                Response.Redirect("~/Error.aspx?code=0", true);
            }

            CreateContact();
        }
        Response.Redirect(string.Format("List.aspx?folder={0}", _folderId), true);
    }

    private void LoadContact(Contact contact)
    {
        contact.Load();

        LastNameTextBox.Value = contact.LastName;
        FirstNameTextBox.Value = contact.FirstName;
        DispNameTextBox.Value = contact.DisplayName;

        JobCompanyTextBox.Value = contact.JobCompany;
        JobDepartmentTextBox.Value = contact.JobDepartment;
        JobTitleTextBox.Value = contact.Title;
        EmailTextBox1.Value = contact.Email;
        EmailTextBox2.Value = contact.Email2;
        EmailTextBox3.Value = contact.Email3;
        JobWebpageTextBox.Value = contact.JobWebpage;
        FavoriteCheckBox.Checked = Favorites.Contains(WebUser.CurrentUser.DomainUserID, contact);
        MemoTextBox.Value = contact.Memo;

        JobPhoneTextBox.Value = contact.JobPhone;
        JobFaxTextBox.Value = contact.JobFax;
        HomeMobileTextBox.Value = contact.HomeMobile;

        JobZipcodeTextBox.Value = contact.JobZipcode;
        JobCityTextBox.Value = contact.JobCity;
        JobStateTextBox.Value = contact.JobState;
        JobStreetTextBox.Value = contact.JobStreet;
        JobCountryTextBox.Value = contact.JobCountry;

        HomeZipcodeTextBox.Value = contact.HomeZipcode;
        HomeCityTextBox.Value = contact.HomeCity;
        HomeStateTextBox.Value = contact.HomeState;
        HomeStreetTextBox.Value = contact.HomeStreet;
        HomeCountryTextBox.Value = contact.HomeCountry;
    }

    private void CreateContact()
    {
        Contact contact = new Contact();

        contact.LastName = LastNameTextBox.Value;
        contact.FirstName = FirstNameTextBox.Value;
        contact.DisplayName = DispNameTextBox.Value;
        contact.JobCompany = JobCompanyTextBox.Value;
        contact.JobDepartment = JobDepartmentTextBox.Value;

        contact.Title = JobTitleTextBox.Value;
        contact.Email = EmailTextBox1.Value;
        contact.Email2 = EmailTextBox2.Value;
        contact.Email3 = EmailTextBox3.Value;
        contact.JobWebpage = JobWebpageTextBox.Value;
        contact.Memo = MemoTextBox.Value;

        contact.JobPhone = JobPhoneTextBox.Value;
        contact.JobFax = JobFaxTextBox.Value;
        contact.HomeMobile = HomeMobileTextBox.Value;        

        contact.JobZipcode = JobZipcodeTextBox.Value;
        contact.JobCity = JobCityTextBox.Value;
        contact.JobState = JobStateTextBox.Value;
        contact.JobStreet = JobStreetTextBox.Value;
        contact.JobCountry = JobCountryTextBox.Value;

        contact.HomeZipcode = HomeZipcodeTextBox.Value;
        contact.HomeCity = HomeCityTextBox.Value;
        contact.HomeState = HomeStateTextBox.Value;
        contact.HomeStreet = HomeStreetTextBox.Value;
        contact.HomeCountry = HomeCountryTextBox.Value;

        AddressBookFolder folder = new AddressBookFolder(_folderId);
        Guid contactId = folder.AddContact(contact, "");
        WebOperationLog.Write(WebUser.CurrentUser.MailAddress, Request.UserHostAddress, "M/Contact/Edit.aspx", "Add", string.Format("FID:{0} S:{1}", _folderId.ToString(), contact.DisplayName));

        if (FavoriteCheckBox.Checked)
        {
            Favorites favorites = new Favorites(WebUser.CurrentUser.DomainUserID);
            Contact favContact = new Contact(contactId);
            favorites.AddContact(favContact);
        }
    }

    private void UpdateContact()
    {
        Contact contact = new Contact(_contactId);
        contact.Load();

        contact.LastName = LastNameTextBox.Value;
        contact.FirstName = FirstNameTextBox.Value;
        contact.DisplayName = DispNameTextBox.Value;
        contact.JobCompany = JobCompanyTextBox.Value;
        contact.JobDepartment = JobDepartmentTextBox.Value;

        contact.Title = JobTitleTextBox.Value;
        contact.Email = EmailTextBox1.Value;
        contact.Email2 = EmailTextBox2.Value;
        contact.Email3 = EmailTextBox3.Value;
        contact.JobWebpage = JobWebpageTextBox.Value;
        contact.Memo = MemoTextBox.Value;

        contact.JobPhone = JobPhoneTextBox.Value;
        contact.JobFax = JobFaxTextBox.Value;
        contact.HomeMobile = HomeMobileTextBox.Value;

        contact.JobZipcode = JobZipcodeTextBox.Value;
        contact.JobCity = JobCityTextBox.Value;
        contact.JobState = JobStateTextBox.Value;
        contact.JobStreet = JobStreetTextBox.Value;
        contact.JobCountry = JobCountryTextBox.Value;

        contact.HomeZipcode = HomeZipcodeTextBox.Value;
        contact.HomeCity = HomeCityTextBox.Value;
        contact.HomeState = HomeStateTextBox.Value;
        contact.HomeStreet = HomeStreetTextBox.Value;
        contact.HomeCountry = HomeCountryTextBox.Value;

        contact.Update("");
        WebOperationLog.Write(WebUser.CurrentUser.MailAddress, Request.UserHostAddress, "M/Contact/Edit.aspx", "Modify", string.Format("CID:{0} S:{1}", _contactId.ToString(), contact.DisplayName));

        Favorites favorites = new Favorites(WebUser.CurrentUser.DomainUserID);
        if (FavoriteCheckBox.Checked)
            favorites.AddContact(contact);
        else
            favorites.RemoveContact(contact);
    }
}