using BeWo.Core.Service;
using BeWo.ServiceProxy;
using BeWo.Validation;

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.ViewModel
{
    public class ValueListEntryVM : AbstractDCMapperVM<ValueListEntryDC>
    {
        public static string PropertyName_Description = "Description";

        public static string PropertyName_IsDeleteable = "IsDeleteable";

        public static string PropertyName_ParentOid = "ParentOid";

        public static string PropertyName_Type = "Type";

        private string _Description;

        private bool _IsDeleteable = true;

        private ValueListEntryDC _ParentEntry;

        private long? _ParentOid;

        private ValueListEntryType _Type;

        public ValueListEntryVM(ValueListEntryDC pDC) : base(pDC, pDC.ValueListEntryOid == null)
        {
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string Description
        {
            get { return _Description; }

            set
            {
                if (AreDifferent(_Description, value))
                {
                    _Description = value;
                    StoreDirtyInformation(AreDifferent(DataContract.TypeDescription, value), PropertyName_Description);
                    FirePropertyChanged(PropertyName_Description);
                }
            }
        }

        public bool IsDeleteable
        {
            get { return _IsDeleteable; }
        }

        public ValueListEntryDC ParentEntry
        {
            get
            {
                if (_ParentEntry == null && _ParentOid != null)
                {
                    _ParentEntry = Cache.GetInstance().GetValueListEntry(_ParentOid.Value);
					if (_ParentEntry == null)
					{
						_ParentEntry = ServiceFacade.DoValueListServiceSync(s => s.GetValueListEntryByOid(_ParentOid.Value));
					}
                }

                return _ParentEntry;
            }

            set
            {
                if (AreDifferent(_ParentEntry, value))
                {
                    _ParentEntry = value;
                    long? oid = null;
                    if (_ParentEntry != null)
                    {
                        oid = _ParentEntry.ValueListEntryOid;
                    }

                    _ParentOid = oid;
                    StoreDirtyInformation(AreDifferent(DataContract.ParentOid, oid), PropertyName_ParentOid);
                    FirePropertyChanged(PropertyName_ParentOid);
                }
            }
        }

        public new ValueListEntryType Type
        {
            get { return _Type; }

            set
            {
                if (AreDifferent(_Type, value))
                {
                    _Type = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Type, value), PropertyName_Type);
                    FirePropertyChanged(PropertyName_Type);
                }
            }
        }

        protected override void InitByDataContract(ValueListEntryDC pDataContract)
        {
            if (!IsNew)
            {
                _Description  = pDataContract.TypeDescription;
                _Type         = pDataContract.Type;
                _IsDeleteable = !pDataContract.IsSystemEntry;
                _ParentOid    = pDataContract.ParentOid;
                var tmp       = ParentEntry;
            }
        }

        protected override ValueListEntryDC MapToDataContract(ValueListEntryDC pDataContract, bool doCommit)
        {
            pDataContract.TypeDescription = _Description;
            pDataContract.Type            = _Type;
            pDataContract.ParentOid       = _ParentOid;
            
            return pDataContract;
        }

    }
}