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

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

namespace BeWo.Office.TableModels
{
    public class PersonTableModel : AbstractTableModel
    {
        public List<PersonDC> Persons { get; set; }
        private int currentIndex = -1;

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

        public PersonDC GetCurrentPerson()
        {
            if (Persons != null)
                if (currentIndex >= 0 && currentIndex < Persons.Count)
                    return Persons[currentIndex];

            return null;
        }

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

        public override void MoveFirst()
        {
            currentIndex = 0;
        }

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

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

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

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

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

            return fieldnames;
        }

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

            var person = GetCurrentPerson();

            switch (propertyName.ToLower())
            {
                case "abkuerzung":
                    return person.Abbreviation;
                case "anrede":
                    return person.Title;
                case "bankleitzahl":
                    return person.BankCode;
                case "beruf":
                    return person.Function.ToString();
                case "bic":
                    return person.Bic;
                case "familienstand":
                    return (person.FamilyStatus.HasValue ? person.FamilyStatus.Value.ToString() : string.Empty);
                case "fax":
                    return person.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Fax));
                case "geburtsdatum":
                    return (person.DateOfBirth.HasValue ? person.DateOfBirth.Value.ToShortDateString() : string.Empty);
                case "geschlecht":
                    return person.Sex.ToString();
                case "handy":
                    return person.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_MobilePhone));
                case "iban":
                    return person.IBAN;
                case "kommentar":
                    return person.PersonNotice;
                case "kontonummer":
                    return person.AccountNumber;
                case "mail":
                    return person.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Mail));
                case "nachname":
                    return person.LastName;
                case "ort":
                    return person.Town;
                case "plz":
                    return person.PostalCode;
                case "rechnungsadresse_name":
                    return person.InvoiceAddressLine1;
                case "rechnungsadresse_ort":
                    return person.InvoiceAddressTown;
                case "rechnungsadresse_plz":
                    return person.InvoiceAddressPostalCode;
                case "rechnungsadresse_strasse":
                    return person.InvoiceAddressStreet;
                case "strasse":
                    return person.Street;
                case "telefon":
                    return person.ContactInformations.FirstOrDefault(ci => ci.ContactType.Equals(ContactType.business_Phone));
                case "vorname":
                    return person.FirstName;
            }

            return value;
        }
    }
}
