using System;
using System.Windows.Media;

using BeWo.Validation;

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

namespace BeWo.ViewModel
{
    public class AccountingTransactionVM : AbstractDCMapperVM<AccountingTransactionDC>
    {
        public static string PropertyName_Amount = "Amount";

        public static string PropertyName_BookingDate = "BookingDate";

        public static string PropertyName_ValidityDate = "ValidityDate";

        public static string PropertyName_Category = "Category";

        public static string PropertyName_CostBearerRel = "CostBearerRel";

        public static string PropertyName_Notice = "Notice";

        private decimal? _Amount;

        private DateTime? _BookingDate;

        private DateTime? _ValidityDate;

        private ValueListEntryDC _Category;

        private SupportConceptCostBearerRelDC _CostBearerRel;

        private string _InsUser;

        private DateTime? _InsertedOn;

        private string _Notice;

        private FlatSupportConceptTreeNodeDC _SupportConceptTreeNode;

        public AccountingTransactionVM(AccountingTransactionDC pDC) : base(pDC, pDC.AccountingTransactionOid == null)
        {
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public decimal? Amount
        {
            get { return this._Amount; }

            set
            {
                if (this.AreDifferent(this._Amount, value))
                {
                    this._Amount = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Amount, value), PropertyName_Amount);
                    this.FirePropertyChanged(PropertyName_Amount);
                }
            }
        }

        public Brush AmountForegroundBrush
        {
            get
            {
                if (this.Amount > 0)
                {
                    return Brushes.DarkGreen;
                }
                else if (this.Amount < 0)
                {
                    return Brushes.DarkRed;
                }
                else
                {
                    return Brushes.Black;
                }
            }
        }

	    public String DatumsText
	    {
		    get
		    {
			    if (!ValidityDate.HasValue)
			    {
				    return String.Format("Für Monat {0:MMMM yyyy}, Buchungsdatum: {0:dd.MM.yyyy}", BookingDate);
			    }
				return String.Format("Für Monat {0:MMMM yyyy}, Buchungsdatum: {1:dd.MM.yyyy}", ValidityDate, BookingDate);
		    }
	    }
        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public DateTime? BookingDate
        {
            get { return this._BookingDate; }

            set
            {
                if (this.AreDifferent(this._BookingDate, value))
                {
                    this._BookingDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.BookingDate, value), PropertyName_BookingDate);
                    this.FirePropertyChanged(PropertyName_BookingDate);
                }
            }
        }

        public DateTime? ValidityDate
        {
            get { return this._ValidityDate; }

            set
            {
                if (this.AreDifferent(this._ValidityDate, value))
                {
                    this._ValidityDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ValidityDate, value), PropertyName_ValidityDate);
                    this.FirePropertyChanged(PropertyName_ValidityDate);
                }
            }
        }

        public ValueListEntryDC Category
        {
            get { return this._Category; }

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

        public CompactOrganisationDC CostBearer
        {
            get
            {
                if (this.CostBearerRel == null)
                {
                    return null;
                }

                return this.CostBearerRel.CostBearer;
            }
        }

        public SupportConceptCostBearerRelDC CostBearerRel
        {
            get { return this._CostBearerRel; }

            set
            {
                if (this.AreDifferent(this._CostBearerRel, value))
                {
                    this._CostBearerRel = value;
                    this.StoreDirtyInformation(!this.AreEqual(this.DataContract.SupportConceptCostBearerRelDC, value), PropertyName_CostBearerRel);
                    this.FirePropertyChanged(PropertyName_CostBearerRel);
                }
            }
        }

        public bool EditAllowed
        {
            get
            {
                if (BeWoApp.LoggedOnUser.HasRight(UserRightType.AccountingTransactionView_Edit))
                {
                    return true;
                }

                return false;
            }
        }

        public string InsUser
        {
            get { return this._InsUser; }

            set { this._InsUser = value; }
        }

        public string InsertedOn
        {
            get
            {
                if (this._InsertedOn != null)
                {
                    return this._InsertedOn.Value.ToShortDateString() + " " + this._InsertedOn.Value.ToShortTimeString();
                }

                return null;
            }
        }

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

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

        // Workaround for Devexpress grid: doesn´t (yet) support property chanes
        public CompactSupportConceptDC SupportConcept
        {
            get
            {
                if (this.CostBearerRel == null)
                {
                    return null;
                }

                return this.CostBearerRel.SupportConcept;
            }
        }

        public string SupportConceptName
        {
            get
            {
                CompactSupportConceptDC dc = this.SupportConcept;
                if (dc != null)
                {
                    return dc.Customer + " " + dc;
                }

                return null;
            }
        }

        // Workaround for Devexpress grid: doesn´t (yet) support property chanes
        public FlatSupportConceptTreeNodeDC SupportConceptTreeNode
        {
            get { return this._SupportConceptTreeNode; }

            set
            {
                this._SupportConceptTreeNode = value;
                if (value != null)
                {
                    this.CostBearerRel = value.SupportConceptTreeNodeDC.SupportConceptCostBearerRelDC;
                }
                else
                {
                    this.CostBearerRel = null;
                }
            }
        }

        public AccountingTransactionVM Clone()
        {
            AccountingTransactionDC dc = new AccountingTransactionDC();

            return new AccountingTransactionVM(this.CopyToDataContract(dc));
        }

        public void CopyTo(AccountingTransactionVM copy)
        {
            copy.Notice = this.Notice;
            copy.BookingDate = this.BookingDate;
			copy.ValidityDate = this.ValidityDate;
            copy.Amount = this.Amount;

            copy.CostBearerRel = this.CostBearerRel;
            copy.Category = this.Category;
        }

        public void SetInsertedOn(DateTime dt)
        {
            this._InsertedOn = dt;
        }

        protected override void InitByDataContract(AccountingTransactionDC pDataContract)
        {
            this._CostBearerRel = pDataContract.SupportConceptCostBearerRelDC;
            this._Category = pDataContract.Category;
            this._Notice = pDataContract.Notice;
            this._BookingDate = pDataContract.BookingDate;
            this._ValidityDate = pDataContract.ValidityDate;
            this._Amount = pDataContract.Amount;
            this._InsertedOn = pDataContract.InsertedOn;
            this._InsUser = pDataContract.InsUser;

            // if (!IsNew)
            // {
            // _CostBearerRel = pDataContract.SupportConceptCostBearerRelDC;
            // _Category = pDataContract.Category;
            // _Notice = pDataContract.Notice;
            // _BookingDate = pDataContract.BookingDate;
            // _Amount = pDataContract.Amount;
            // _InsertedOn = pDataContract.InsertedOn;
            // _InsUser = pDataContract.InsUser;
            // }
            // else
            // BookingDate = DateTime.Now;
        }

        protected override AccountingTransactionDC MapToDataContract(AccountingTransactionDC pDataContract, bool doCommit)
        {
            pDataContract.SupportConceptCostBearerRelDC = this._CostBearerRel;
            pDataContract.Category = this._Category;
            pDataContract.BookingDate = this._BookingDate;
            pDataContract.ValidityDate = this._ValidityDate;
            pDataContract.Amount = this._Amount.Value;
            pDataContract.Notice = this._Notice;

            return pDataContract;
        }

        private AccountingTransactionDC CopyToDataContract(AccountingTransactionDC pDataContract)
        {
            //decimal amount = 0;
            //if (this._Amount.HasValue)
            //{
            //    amount = this._Amount.Value;
            //}

            pDataContract.Amount = this._Amount.Value;
            pDataContract.BookingDate = this._BookingDate;
            pDataContract.ValidityDate = this._ValidityDate;
            pDataContract.Category = this._Category;
            pDataContract.Notice = this._Notice;
            pDataContract.SupportConceptCostBearerRelDC = this._CostBearerRel;

            return pDataContract;
        }
    }
}