using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Office.TableModels
{
    public class SupportConceptTableModel : AbstractTableModel
    {
        public List<SupportConceptDC> SupportConcepts { get; set; }
        private int currentIndex = -1;

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

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

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

        public override void MoveLast()
        {
            currentIndex = SupportConcepts != null ? SupportConcepts.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>
                {
                    "Klient_Eigenbeteiligung",
                    "Klient_Geburtsdatum",
                    "Klient_Geschlecht",
                    "Klient_Nachname",
                    "Klient_Ort",
                    "Klient_PLZ",
                    "Klient_Strasse",
                    "Klient_Vorname",
                    "Teilname an Hilfeplankonferenz",
                    "Hilfeplan beantragt am",
                    "Hilfeplan eingereicht am",
                    "Hilfeplankonferenz",
                    "Ort des Hilfeplangespraechs",
                    "Ohne Beratungsbedarf",
                    "Bewilligung eingetroffen am",
                    "Verlaengerung beantragt am",
                    "Archiviert",
                    "Ersteller"
                };

            return fieldnames;
        }

        public SupportConceptDC GetCurrentSupportConcept()
        {
            if (SupportConcepts != null)
                if (currentIndex >= 0 && currentIndex < SupportConcepts.Count)
                    return SupportConcepts[currentIndex];

            return null;
        }

        public override object GetPropertyValue(string propertyName)
        {
            var value = string.Empty;
            var supportConcept = GetCurrentSupportConcept();
            var customer = supportConcept.Customer;

            switch (propertyName.ToLower())
            {
                case "klient_eigenbeteiligung":
                    return customer.CostCenter;
                case "klient_geburtsdatum":
                    return (customer.DateOfBirth.HasValue ? customer.DateOfBirth.Value.ToShortDateString() : string.Empty);
                case "klient_geschlecht":
                    return customer.Sex.ToString();
                case "klient_nachname":
                    return customer.LastName;
                case "klient_ort":
                    return customer.Town;
                case "klient_plz":
                    return customer.PostalCode;
                case "klient_strasse":
                    return customer.Street;
                case "klient_vorname":
                    return customer.FirstName;
                case "teilname an hilfeplankonferenz":
		            if (supportConcept.Conference.HasValue)
		            {
			            return supportConcept.Conference.Value ? "Ja" : "Nein";
		            }
                    return string.Empty;
                case "hilfeplan beantragt am":
                    return supportConcept.RequisitionSendDate.HasValue ? supportConcept.RequisitionSendDate.Value.ToShortDateString() : string.Empty;
                case "hilfeplan eingereicht am":
                    return supportConcept.SupportConceptSendDate.HasValue ? supportConcept.SupportConceptSendDate.Value.ToShortDateString() : string.Empty;
                case "hilfeplankonferenz":
                    return supportConcept.ConferenceDate.HasValue ? supportConcept.ConferenceDate.Value.ToShortDateString() : string.Empty;
                case "ort des hilfeplangespraechs":
                    return supportConcept.ConferenceVenue ?? string.Empty;
                case "ohne beratungsbedarf":
                    return supportConcept.ConsultingNeeded.HasValue ? supportConcept.ConsultingNeeded.Value ? "Ja" : "Nein" : string.Empty;
                case "bewilligung eingetroffen am":
                    return supportConcept.ApprovalDate.HasValue ? supportConcept.ApprovalDate.Value.ToShortDateString() : string.Empty;
                case "verlaengerung beantragt am":
                    return supportConcept.RenewalSendDate.HasValue ? supportConcept.RenewalSendDate.Value.ToShortDateString() : string.Empty;
                case "archiviert":
                    return supportConcept.ActivationType.Equals(ActivationTypeId.Archived) ? "Ja" : "Nein";
                case "ersteller":
                    return supportConcept.Originator.FirstName + " " + supportConcept.Originator.LastName;
            }

            return value;
        }
    }
}