using System.Collections.Generic;
using System.Linq;

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

namespace BeWo.Office.TableModels
{
    public class CustomerTableModel : AbstractTableModel
    {
        public List<CustomerDC> Customers { get; set; }
        private int currentIndex = -1;

        public override int Count
        {
            get { return Customers.Count; }
        }

        public CustomerDC GetCurrentCustomer()
        {
            if (Customers != null)
                if (currentIndex >= 0 && currentIndex < Customers.Count)
                    return Customers[currentIndex];

            return null;
        }

        private static List<string> CreateDefaultFieldNames()
        {
            var fieldnames = new List<string>
                {
                    "Anrede",
                    "Beruf",
                    "Eigenbeteiligung",
                    "Familienstand",
                    "Fax",
                    "Geburtsdatum",
                    "Geschlecht",
                    "Handy",
                    "Kinder",
                    "Kommentar",
                    "Mail",
                    "Nachname",
                    "Ort",
                    "PLZ",
                    "Rechnungsadresse_Name",
                    "Rechnungsadresse_Ort",
                    "Rechnungsadresse_PLZ",
                    "Rechnungsadresse_Strasse",
                    "Strasse",
                    "Telefon",
                    "Vorname"
                };

            return fieldnames;
        }

        public override bool DataAvailable
        {
            get { return Customers != null && currentIndex >= 0 && currentIndex < Customers.Count; }
        }

        public override object GetPropertyValue(string propertyName)
        {
            var value = string.Empty;

            var customer = GetCurrentCustomer();

            switch (propertyName.ToLower())
            {
                case "anrede":
                    return customer.Title;
                case "beruf":
                    return customer.Profession;
                case "eigenbeteiligung":
                    return customer.CostCenter;
                case "familienstand":
                    return (customer.FamilyStatus.HasValue ? customer.FamilyStatus.Value.ToString() : string.Empty);
                case "fax":
                    return customer.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Fax));
                case "geburtsdatum":
                    return (customer.DateOfBirth.HasValue ? customer.DateOfBirth.Value.ToShortDateString() : string.Empty);
                case "geschlecht":
                    return customer.Sex.ToString();
                case "handy":
                    return customer.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_MobilePhone));
                case "kinder":
                    return customer.Childs;
                case "kommentar":
                    return customer.Notice;
                case "mail":
                    return customer.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Mail));
                case "nachname":
                    return customer.LastName;
                case "ort":
                    return customer.Town;
                case "plz":
                    return customer.PostalCode;
                case "rechnungsadresse_name":
                    return customer.InvoiceAddressLine1;
                case "rechnungsadresse_ort":
                    return customer.InvoiceAddressTown;
                case "rechnungsadresse_plz":
                    return customer.InvoiceAddressPostalCode;
                case "rechnungsadresse_strasse":
                    return customer.InvoiceAddressStreet;
                case "strasse":
                    return customer.Street;
                case "telefon":
                    return customer.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Phone));
                case "vorname":
                    return customer.FirstName;
            }
            
            return value;
        }
        
        public override void MoveFirst()
        {
            currentIndex = 0;
        }

        public override void MoveLast()
        {
            currentIndex = Customers != null ? Customers.Count - 1 : -1;
        }

        public override void MoveNext()
        {
            currentIndex++;
        }

        public override void MovePrevious()
        {
            currentIndex--;
        }

        protected override string[] CreateFieldNames()
        {
            return CreateDefaultFieldNames().ToArray();
        }
    }
}
