using BeWo.Validation;

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

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

        public static string PropertyName_Person = "Person";

        public static string PropertyName_PersonString = "PersonString";

        public static string PropertyName_AddressSummaryInfo = "AddressSummaryInfo";

        public static string PropertyName_ContactSummaryInfo = "ContactSummaryInfo";

        public static string PropertyName_Role = "Role";

        private string _Notice;

        private CompactPersonDC _Person;

        private ValueListEntryDC _Role;

        public CustomerPersonRelationVM(CustomerPersonRelationDC pDC) : base(pDC, pDC.Customer2PersonOid == null) { }

        public string Notice
        {
            get { return _Notice; }

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

        public CompactPersonDC Person
        {
            get { return _Person; }

            set
            {
                if (AreDifferent(_Person, value))
                {
                    _Person = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Person, value), PropertyName_Person);
                    FirePropertyChanged(PropertyName_Person);
                    FirePropertyChanged(PropertyName_PersonString);
                    FirePropertyChanged(PropertyName_AddressSummaryInfo);
                    FirePropertyChanged(PropertyName_ContactSummaryInfo);
                }
            }
        }

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

        public string AddressSummaryInfo
        {
            get
            {
                var address = _Person.Street;
                if (!string.IsNullOrEmpty(address))
                {
                    address += "\n";
                }

                if (address == null)
                {
                    address = string.Empty;
                }

                address += _Person.PostalCode + " " + _Person.Town;
                
                return address;
            } 

            set { }
        }

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

                if (!string.IsNullOrEmpty(_Person.Communication1))
                {
                    info = "Tel.: " + _Person.Communication1;
                }

                if (!string.IsNullOrEmpty(_Person.Communication2))
                {
                    if (info.Length > 0)
                    {
                        info += "\n";
                    }

                    info += "Mobil: " + _Person.Communication2;
                }

                if (!string.IsNullOrEmpty(_Person.Communication3))
                {
                    if (info.Length > 0)
                    {
                        info += "\n";
                    }

                    info += "E-Mail: " + _Person.Communication3;
                }

                if (!string.IsNullOrEmpty(_Person.Communication4))
                {
                    if (info.Length > 0)
                    {
                        info += "\n";
                    }

                    info += "Fax: " + _Person.Communication4;
                }

                return info;
            }

            set { }
        }

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

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

        protected override void InitByDataContract(CustomerPersonRelationDC pDataContract)
        {
            if (!IsNew)
            {
                _Notice = pDataContract.Customer2PersonNotice;
                _Person = pDataContract.Person;
                _Role   = pDataContract.RelationRole;
            }
        }

        protected override CustomerPersonRelationDC MapToDataContract(CustomerPersonRelationDC pDataContract, bool doCommit)
        {
            pDataContract.Customer2PersonNotice = _Notice;
            pDataContract.Person                = _Person;
            pDataContract.RelationRole          = _Role;

            return pDataContract;
        }
    }
}