using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace BS.Shared.DataContracts.Compact
{
    public partial class CompactSupportConceptDC : IFilterableDC
    {
        private List<string> _CostBearerDetailStrings;

        public string AddressString
        {
            get
            {
                return this.Customer.AddressString;
            }
        }

        public string AllCostBearerNames
        {
            get
            {
                return string.Join(", ", this.CostBearerList.Select(cb => cb.Organisation != null ? cb.Organisation.Name : string.Empty).ToArray());
            }
        }

        public string ConferenceDateString
        {
            get
            {
                if (this.ConferenceDate != null)
                {
                    return this.ConferenceDate.Value.ToShortDateString();
                }

                return null;
            }
        }

        public string CostBearer
        {
            get
            {
                List<string> cbs = this.CostBearerDetailStrings;

                if (cbs != null && cbs.Count > 0)
                {
                    return cbs[0];
                }

                return string.Empty;
            }
        }

        public List<string> CostBearerDetailStrings
        {
            get
            {
                return this._CostBearerDetailStrings ?? this.CreateCostBearerDetailStrings();
            }
        }

        public string CustomerFullName
        {
            get
            {
                return this.Customer != null ? this.Customer.FullName : null;
            }
        }

        public string CustomerName
        {
            get
            {
                return this.Customer != null ? this.Customer.ToString() : null;
            }
        }

        public string DetailDescription
        {
            get
            {
                return this.Customer + " " + this.Customer.DateOfBirthString;
            }
        }

        public DateTime? EndDate
        {
            get
            {
                DateTime? maxDate = null;

                foreach (CompactCostBearerDC cb in this.CostBearerList)
                {
                    DateTime? end = null;
                    end = cb.ApprovedEndDate;

                    if (end == null)
                    {
                        end = cb.RequestedEndDate;
                    }

                    if (maxDate == null || (end != null && end.Value > maxDate.Value))
                    {
                        maxDate = end;
                    }
                }

                return maxDate;
            }
        }

        public bool ExpiresIn3MonthOrLess
        {
            get
            {
                bool expires = false;

                DateTime? date = this.EndDate;

                if (date != null)
                {
                    expires = date < DateTime.Now.AddMonths(BS.Shared.Settings.ApplicationSettings.SupportConceptExpirationLimitInMonths);
                }

                return expires;
            }
        }

        public string FilterRelevants
        {
            get
            {
                // return string.Join(" ", CostBearerDetailStrings.ToArray())
                // + " " + string.Join(" ", CustomerReferenceNumbers.ToArray())
                // + " " + Customer.ToString();
                string fr = string.Join(" ", this.CustomerReferenceNumbers.ToArray()) + " " + this.Customer;

                fr += " " + string.Join(" ", this.CostBearerDetailStrings.ToArray());
                return fr;
            }
        }

        public string FirstCostBearerDetailString
        {
            get
            {

                if (this.CostBearerDetailStrings != null && this.CostBearerDetailStrings.Count > 0)
                {
                    return this.CostBearerDetailStrings[0];
                }

                return null;
            }
        }

        public string FirstName
        {
            get
            {
                return this.Customer != null ? this.Customer.FirstName : null;
            }
        }

        public Brush ForegroundBrush
        {
            get
            {
                if (this.IsAboutToExpire)
                {
                    return Brushes.Red;
                }
                else if (this.ExpiresIn3MonthOrLess)
                {
                    return Brushes.Yellow;
                }
                else
                {
                    return new SolidColorBrush(Color.FromRgb(160, 160, 160));
                }
            }
        }

        public string IconPath
        {
            get
            {
                return @"..\..\Ressources\Icons\IDBadgeDisabled.png";
            }
        }

        public List<IInvoiceRecipient> InvoiceRecipients
        {
            get
            {
                var result = new List<IInvoiceRecipient> { this.Customer };
                result.AddRange(this.CostBearerList.Cast<IInvoiceRecipient>());
                return result;
            }
        }

        public bool IsAboutToExpire
        {
            get
            {
                bool expires = false;

                DateTime? date = this.EndDate;

                if (date != null)
                {
                    expires = date < DateTime.Now.AddDays(7); // TODO anz Tage einstellbar machen
                }

                return expires;
            }
        }

        public bool IsArchived
        {
            get
            {
                return this.ActivationType == ActivationTypeId.Archived;
            }
        }

		public bool IsDeleted
		{
			get
			{
				return this.ActivationType == ActivationTypeId.Deleted;
			}
		}

        public string LastName
        {
            get
            {
                return this.Customer != null ? this.Customer.LastName : null;
            }
        }

        public string SimpleDescription
        {
            get
            {
                return this.ToString();
            }
        }

        public DateTime? StartDate
        {
            get
            {
                DateTime? minDate = null;

                foreach (CompactCostBearerDC cb in this.CostBearerList)
                {
                    DateTime? start = null;
                    start = cb.ApprovedStartDate;

                    if (start == null)
                    {
                        start = cb.RequestedStartDate;
                    }

                    if (minDate == null || (start != null && start.Value < minDate.Value))
                    {
                        minDate = start;
                    }
                }

                return minDate;
            }
        }

        public bool SupportsActivationType
        {
            get
            {
                return true;
            }
        }

        public long Version
        {
            get
            {
                return this.SupportConceptVersion;
            }

            set
            {
                this.SupportConceptVersion = value;
            }
        }

        public SolidColorBrush FilterableBrush { get { return new SolidColorBrush(Colors.Transparent); } }

        public override bool Equals(object obj)
        {
            if (obj is CompactSupportConceptDC)
            {
                CompactSupportConceptDC dc = obj as CompactSupportConceptDC;

                if (this.CostBearerList != null && this.CostBearerList.Count == 1 && dc.CostBearerList != null && dc.CostBearerList.Count == 1)
                {
                    return this.CostBearerList[0].CostBearer2SupportConceptOid == dc.CostBearerList[0].CostBearer2SupportConceptOid;
                }

                return this.SupportConceptOid == ((CompactSupportConceptDC)obj).SupportConceptOid;
            }

            return false;
        }

        public override int GetHashCode()
        {
            if (this.CostBearerList != null && this.CostBearerList.Count == 1)
            {
                return this.GetType().Name.GetHashCode() ^ this.CostBearerList[0].CostBearer2SupportConceptOid.GetHashCode();
            }

            return this.GetType().Name.GetHashCode() ^ this.SupportConceptOid.GetHashCode();
        }

        public override string ToString()
        {
            DateTime? start = this.StartDate;
            DateTime? end = this.EndDate;

            StringBuilder sb = new StringBuilder();

            if (start != null)
            {
                sb.Append(start.Value.ToShortDateString());
            }

            if (start != null || end != null)
            {
                sb.Append(" - ");
            }

            if (end != null)
            {
                sb.Append(end.Value.ToShortDateString());
            }

            return sb.ToString();
        }

        private List<string> CreateCostBearerDetailStrings()
        {
            this._CostBearerDetailStrings = new List<string>();
            foreach (CompactCostBearerDC cb in this.CostBearerList)
            {
                DateTime? from = null;
                DateTime? to = null;

                from = cb.ApprovedStartDate;
                to = cb.ApprovedEndDate;

                if (from == null)
                {
                    from = cb.RequestedStartDate;
                }

                if (to == null)
                {
                    to = cb.RequestedEndDate;
                }

                StringBuilder cbstr = new StringBuilder();

                if (from != null && to != null)
                {
                    cbstr.Append(from.Value.ToShortDateString());
                    cbstr.Append(" - ");
                    cbstr.Append(to.Value.ToShortDateString());
                }

                if (cbstr.Length > 0)
                {
                    cbstr.Append("   ");
                }

                if (cb.Organisation != null)
                {
                    cbstr.Append(cb.Organisation.Name);
                }

                if (cb.ApprovedStartDate == null || cb.ApprovedEndDate == null)
                {
                    cbstr.Append(" nicht bewilligt!");
                }

                this._CostBearerDetailStrings.Add(cbstr.ToString());
            }

            return this._CostBearerDetailStrings;
        }

        public String CustomerAndFirstDetailString
        {
            get { return String.Format("{0}\n{1}", CustomerName, FirstCostBearerDetailString); }
        }
    }
}