using BeWo.Validation;

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

namespace BeWo.ViewModel
{
    public class CustomerOrganisationRelationVM : AbstractDCMapperVM<CustomerOrganisationRelationDC>
    {
        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_Info1 = "Info1";

        public static string PropertyName_Info2 = "Info2";

        public static string PropertyName_Info3 = "Info3";

        public static string PropertyName_Info4 = "Info4";

        public static string PropertyName_Info5 = "Info5";

        public static string PropertyName_Organisation = "Organisation";

        public static string PropertyName_OrganisationString = "OrganisationString";

        public static string PropertyName_Role = "Role";

        public static string PropertyName_AddressSummaryInfo = "AddressSummaryInfo";

        public static string PropertyName_ContactSummaryInfo = "ContactSummaryInfo";

        private string _Notice;

        private string _Info1;

        private string _Info2;

        private string _Info3;

        private string _Info4;

        private string _Info5;

        private CompactOrganisationDC _Organisation;

        private ValueListEntryDC _Role;

        public CustomerOrganisationRelationVM(CustomerOrganisationRelationDC pDC)
            : base(pDC, pDC.Customer2OrganisationOid == null)
        {
        }

        public string Notice
        {
            get { return this._Notice; }

            set
            {
                if (this.AreDifferent(this._Notice, value))
                {
                    this._Notice = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Customer2OrganisationNotice, value), PropertyName_Notice);
                    this.FirePropertyChanged(PropertyName_Notice);
                }
            }
        }

        public string Info1
        {
            get { return this._Info1; }

            set
            {
                if (this.AreDifferent(this._Info1, value))
                {
                    this._Info1 = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Info1, value), PropertyName_Info1);
                    this.FirePropertyChanged(PropertyName_Info1);
                }
            }
        }

        public string Info2
        {
            get { return this._Info2; }

            set
            {
                if (this.AreDifferent(this._Info2, value))
                {
                    this._Info2 = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Info2, value), PropertyName_Info2);
                    this.FirePropertyChanged(PropertyName_Info2);
                }
            }
        }

        public string Info3
        {
            get { return this._Info3; }

            set
            {
                if (this.AreDifferent(this._Info3, value))
                {
                    this._Info3 = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Info3, value), PropertyName_Info3);
                    this.FirePropertyChanged(PropertyName_Info3);
                }
            }
        }

        public string Info4
        {
            get { return this._Info4; }

            set
            {
                if (this.AreDifferent(this._Info4, value))
                {
                    this._Info4 = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Info4, value), PropertyName_Info4);
                    this.FirePropertyChanged(PropertyName_Info4);
                }
            }
        }

        public string Info5
        {
            get { return this._Info5; }

            set
            {
                if (this.AreDifferent(this._Info5, value))
                {
                    this._Info5 = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Info5, value), PropertyName_Info5);
                    this.FirePropertyChanged(PropertyName_Info5);
                }
            }
        }


        public CompactOrganisationDC Organisation
        {
            get { return this._Organisation; }

            set
            {
                if (this.AreDifferent(this._Organisation, value))
                {
                    this._Organisation = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Organisation, value), PropertyName_Organisation);
                    this.FirePropertyChanged(PropertyName_Organisation);
                    this.FirePropertyChanged(PropertyName_OrganisationString);
                    this.FirePropertyChanged(PropertyName_AddressSummaryInfo);
                    this.FirePropertyChanged(PropertyName_ContactSummaryInfo);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string OrganisationString
        {
            get { return this._Organisation.ToStringNullCheck(); } // Validation Workaround
            set { }
        }

        [ValidationAttribute(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public ValueListEntryDC Role
        {
            get { return this._Role; }

            set
            {
                if (this.AreDifferent(this._Role, value))
                {
                    this._Role = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.RelationRole, value), PropertyName_Role);
                    this.FirePropertyChanged(PropertyName_Role);
                }
            }
        }

        public string AddressSummaryInfo
        {
            get
            {
                string address = _Organisation != null ? _Organisation.Street : string.Empty;
                if (!string.IsNullOrEmpty(address))
                {
                    address += "\n";
                }
                if (address == null)
                {
                    address = string.Empty;
                }
                address += _Organisation != null ? _Organisation.PostalCode + " " + this._Organisation.Town : string.Empty;

                return address;
            }
            set { }
        }

        public string ContactSummaryInfo
        {
            get
            {
                string info = string.Empty;

                if (!string.IsNullOrEmpty(this._Organisation.Communication1))
                    info = "Tel.: " + this._Organisation.Communication1;
                if (!string.IsNullOrEmpty(this._Organisation.Communication2))
                {
                    if (info.Length > 0)
                        info += "\n";
                    info += "Fax: " + this._Organisation.Communication2;
                }
                if (!string.IsNullOrEmpty(this._Organisation.Communication3))
                {
                    if (info.Length > 0)
                        info += "\n";
                    info += "E-Mail: " + this._Organisation.Communication3;
                }
                if (!string.IsNullOrEmpty(this._Organisation.Communication4))
                {
                    if (info.Length > 0)
                        info += "\n";
                    info += "Internet: " + this._Organisation.Communication4;
                }
                return info;
            }
            set { }
        }

        protected override void InitByDataContract(CustomerOrganisationRelationDC pDataContract)
        {
            if (this.IsNew) 
                return;

            this._Notice = pDataContract.Customer2OrganisationNotice;
            this._Info1 = pDataContract.Info1;
            this._Info2 = pDataContract.Info2;
            this._Info3 = pDataContract.Info3;
            this._Info4 = pDataContract.Info4;
            this._Info5 = pDataContract.Info5;

            this._Organisation = pDataContract.Organisation;
            this._Role = pDataContract.RelationRole;
        }

        protected override CustomerOrganisationRelationDC MapToDataContract(CustomerOrganisationRelationDC pDataContract, bool doCommit)
        {
            pDataContract.Customer2OrganisationNotice = this._Notice;
            pDataContract.Organisation = this._Organisation;
            pDataContract.RelationRole = this._Role;
            pDataContract.Info1 = this._Info1;
            pDataContract.Info2 = this._Info2;
            pDataContract.Info3 = this._Info3;
            pDataContract.Info4 = this._Info4;
            pDataContract.Info5 = this._Info5;

            return pDataContract;
        }
    }
}