using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Core;
using BeWo.Validation;
using BeWo.ViewModel.ListViewModel;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using System.Windows.Forms;

namespace BeWo.ViewModel
{
    public class WohnheimVM : AbstractDCMapperVM<WohnheimDC>
    {
        #region Property Names

        public static string PropertyName_WohnheimEmployeeRelations = "WohnheimEmployeeRelations";
        public static string PropertyName_WohnheimCustomerRelations = "WohnheimCustomerRelations";
        public static string PropertyName_IsArchived = "IsArchived";
        

        //##################### Erstellte Variablen #######################
        public static string PropertyName_WohnheimName = "WohnheimName";
        public static string PropertyName_PLZ = "PLZ";
        public static string PropertyName_Ort = "Ort";
        public static string PropertyName_Bemerkung = "Bemerkung";
        public static string PropertyName_Street = "Strasse";
        //##################################################################
        #endregion

        //##################### Erstellte Variablen #######################
        private string _WohnheimName;
        private string _Strasse;
        private string _PLZ;
        private string _Ort;
        private string _Bemerkung;
        private bool _IsArchived;

        //##################################################################


        private WohnheimEmployeeRelationListVM _WohnheimEmployeeRelations;
        private WohnheimCustomerRelationListVM _WohnheimCustomerRelations;

        public WohnheimVM(WohnheimDC pWohnheimDC)
            : base(pWohnheimDC, pWohnheimDC.WohnheimOid == null)
        {

        }

        public override bool IsDirty
        {
            get
            {
                return base.IsDirty || WohnheimEmployeeRelations.IsDirty || WohnheimCustomerRelations.IsDirty;
            }
        }

        public bool IsArchived
        {
            get { return _IsArchived; }

            set
            {
                if (AreDifferent(_IsArchived, value))
                {
                    _IsArchived = value;
                    StoreDirtyInformation(
                        (value && DataContract.ActivationType == ActivationTypeId.Active) ||
                        (!value && DataContract.ActivationType == ActivationTypeId.Archived), PropertyName_IsArchived);
                    FirePropertyChanged(PropertyName_IsArchived);
                }
            }
        }

        //Setter und Getter
        public String WohnheimName
        {

            get
            {
                return _WohnheimName;
            }
            set
            {
                _WohnheimName = value;
                StoreDirtyInformation(AreDifferent(DataContract.WohnheimName, value), PropertyName_WohnheimName);
                FirePropertyChanged(PropertyName_WohnheimName);
            }
        }

        public String Strasse
        {

            get
            {
                return _Strasse;
            }
            set
            {
                _Strasse = value;
                StoreDirtyInformation(AreDifferent(DataContract.Strasse, value), PropertyName_Street);
                FirePropertyChanged(PropertyName_Street);
            }
        }

        public String PLZ
        {
            get
            {
                return _PLZ;
            }
            set
            {
                _PLZ = value;
                StoreDirtyInformation(AreDifferent(DataContract.PLZ, value), PropertyName_PLZ);
                FirePropertyChanged(PropertyName_PLZ);
            }
        }

        public String Ort
        {
            get
            {
                return _Ort;
            }
            set
            {
                _Ort = value;
                StoreDirtyInformation(AreDifferent(DataContract.Ort, value), PropertyName_Ort);
                FirePropertyChanged(PropertyName_Ort);
            }
        }

        public String Bemerkung
        {

            get
            {
                return _Bemerkung;
            }
            set
            {
                _Bemerkung = value;
                StoreDirtyInformation(AreDifferent(DataContract.Bemerkung, value), PropertyName_Bemerkung);

                FirePropertyChanged(PropertyName_Bemerkung);
            }
        }


        public WohnheimEmployeeRelationListVM WohnheimEmployeeRelations
        {
            get { return _WohnheimEmployeeRelations; }

            set
            {
                _WohnheimEmployeeRelations = value;
                FirePropertyChanged(PropertyName_WohnheimEmployeeRelations);
            }
        }

        public WohnheimCustomerRelationListVM WohnheimCustomerRelations
        {
            get { return _WohnheimCustomerRelations; }

            set
            {
                _WohnheimCustomerRelations = value;
                FirePropertyChanged(PropertyName_WohnheimCustomerRelations);
            }
        }

        // HACK: Bug bei den RadioButtons, geben false nicht weiter
        // TODO nochmal angucken wenn mehr als zwei Optionen, dann in listbox und an selected item binden
        // http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2146568&SiteID=1


        protected override void InitByDataContract(WohnheimDC pDataContract)
        {
           
            _WohnheimName = pDataContract.WohnheimName;
            _Strasse = pDataContract.Strasse;
            _PLZ = pDataContract.PLZ;
            _Ort = pDataContract.Ort;
            _Bemerkung = pDataContract.Bemerkung;


            _IsArchived = pDataContract.ActivationType == ActivationTypeId.Archived;

            WohnheimEmployeeRelations = VMFactory.CreateWohnheimEmployeeRelationListVM(pDataContract.RelatedEmployees ?? (pDataContract.RelatedEmployees = new List<WohnheimEmployeeRelationDC>()));
            WohnheimCustomerRelations = VMFactory.CreateWohnheimCustomerRelationListVM(pDataContract.RelatedCustomers ?? (pDataContract.RelatedCustomers = new List<WohnheimCustomerRelationDC>()));

        }

        protected override WohnheimDC MapToDataContract(WohnheimDC pDataContract, bool doCommit)
        {

            pDataContract.WohnheimName = _WohnheimName;
            pDataContract.Strasse = _Strasse;
            pDataContract.PLZ = _PLZ;
            pDataContract.Ort = _Ort;
            pDataContract.Bemerkung = _Bemerkung;


            pDataContract.ActivationType = _IsArchived ? ActivationTypeId.Archived : ActivationTypeId.Active;
           
            pDataContract.RelatedEmployees = _WohnheimEmployeeRelations.CopyToDCList(doCommit);
            pDataContract.RelatedCustomers = _WohnheimCustomerRelations.CopyToDCList(doCommit);

            return pDataContract;
        }


        private void CommitContact(string pValue, ContactType pContactType)
        {
            List<ContactDC> lOldContactDCs;
            if (DataContract.ContactInformations != null)
            {
                lOldContactDCs = DataContract.ContactInformations.ToList();
            }
            else
            {
                lOldContactDCs = new List<ContactDC>();
            }

            ContactDC lContactDC = null;
            try
            {
                lContactDC = lOldContactDCs.SingleOrDefault(con => con.ContactType == pContactType);
            }
            catch (Exception)
            {
                // donothing
            }

            if (string.IsNullOrEmpty(pValue) && lContactDC != null)
            {
                lOldContactDCs.Remove(lContactDC);
            }
            else if (!string.IsNullOrEmpty(pValue))
            {
                if (lContactDC == null)
                {
                    lContactDC = new ContactDC
                    {
                        ContactType = pContactType
                    };
                    lOldContactDCs.Add(lContactDC);
                }

                lContactDC.ContactValue = pValue;
            }

            DataContract.ContactInformations = lOldContactDCs;
        }

    }
}
