using System.Windows.Media;

using BeWo.Validation;

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

namespace BeWo.ViewModel
{
    public class ResourceVM : AbstractDCMapperVM<ResourceDC>
    {
        public static string PropertyName_Brush = "Brush";

        public static string PropertyName_Category = "Category";

        public static string PropertyName_Description = "Description";

        public static string PropertyName_IsDeleteable = "IsDeleteable";

        public static string PropertyName_Name = "Name";

        private SolidColorBrush _Brush;

        private string _BrushString;

        private ValueListEntryDC _Category;

        private string _Description;

        private bool _IsDeleteable = true;

        private string _Name;

        public ResourceVM(ResourceDC pDataContract) : base(pDataContract, pDataContract.ResourceOid == null)
        {
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public SolidColorBrush Brush
        {
            get { return this._Brush; }

            set
            {
                if (this.AreDifferent(this._Brush, value))
                {
                    this._Brush = value;
                    this._BrushString = value.ToStringNullCheck();
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Color, value.ToString()), PropertyName_Brush);
                    this.FirePropertyChanged(PropertyName_Brush);
                }
            }
        }

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

            set
            {
                if (this.AreDifferent(this._Category, value))
                {
                    this._Category = value;
                    this.StoreDirtyInformation(!this.AreEqual(value, this.DataContract.ResourceCategory), PropertyName_Category);
                    this.FirePropertyChanged(PropertyName_Category);
                }
            }
        }

        public string Description
        {
            get { return this._Description; }

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

        public bool IsDeleteable
        {
            get { return this._IsDeleteable; }
        }

        [ValidationAttribute(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string Name
        {
            get { return this._Name; }

            set
            {
                if (this.AreDifferent(this._Name, value))
                {
                    this._Name = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Name, value), PropertyName_Name);
                    this.FirePropertyChanged(PropertyName_Name);
                }
            }
        }

        protected override void InitByDataContract(ResourceDC pDataContract)
        {
            if (!this.IsNew)
            {
                this._Name = pDataContract.Name;
                this._Description = pDataContract.Description;
                this._Category = pDataContract.ResourceCategory;
                this._IsDeleteable = !pDataContract.IsSystemEntry;
                if (!string.IsNullOrEmpty(pDataContract.Color))
                {
                    this.Brush = (SolidColorBrush)new BrushConverter().ConvertFrom(pDataContract.Color);
                }
            }
        }

        protected override ResourceDC MapToDataContract(ResourceDC pDataContract, bool doCommit)
        {
            pDataContract.Name = this._Name;
            pDataContract.Description = this._Description;
            pDataContract.ResourceCategory = this._Category;
            pDataContract.Color = this._BrushString;

            return pDataContract;
        }
    }
}