using System;
using System.Collections.Generic;
using System.Linq;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
using BeWo.Core;

namespace BeWo.ViewModel
{
    public class MandatorVM : AbstractDCMapperVM<MandatorDC>
    {
        public static string PropertyName_BeWoClientType = "BeWoClientType";

        public static string PropertyName_ClientId = "ClientId";

        public static string PropertyName_Country = "Country";

        public static string PropertyName_Name = "Name";

        public static string PropertyName_PostalCode = "PostalCode";

        public static string PropertyName_Settings = "Settings";

        public static string PropertyName_State = "State";

        public static string PropertyName_Street = "Street";

        public static string PropertyName_Town = "Town";

        public static string PropertyName_TimeUntilUILock = "TimeUntilUILock";

        public static string PropertyName_HilfeplanAuslaufAuswahl = "HilfeplanAuslaufAuswahl";

        public static string PropertyName_AnzTageZeiterfassErfolgt = "AnzTageZeiterfassErfolgt";

       
        private long _BeWoClientType;

        private string _ClientId;

        private string _Country;

        private string _Name;

        private string _PostalCode;

        private string _Settings;

        private string _State;

        private string _Street;

        private string _Town;

        private int _TimeUntilUILock;

        private int _HilfeplanAuslaufAuswahl;

        private int _AnzTageZeiterfassErfolgt;

        private InvoiceNumberVM _InvoiceNumber;

        public MandatorVM(InvoiceNumberVM invoiceNumberVM, MandatorDC pDC) : base(pDC, true)
        {
            _InvoiceNumber = invoiceNumberVM;
        }

        public InvoiceNumberVM InvoiceNumber 
        {
            get
            {
                return _InvoiceNumber;
            }
        }

        public long BeWoClientType
        {
            get { return this._BeWoClientType; }

            set
            {
                if (this.AreDifferent(this._BeWoClientType, value))
                {
                    this._BeWoClientType = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.BeWoClientType, value), PropertyName_BeWoClientType);
                    this.FirePropertyChanged(PropertyName_BeWoClientType);
                }
            }
        }

        public string ClientId
        {
            get { return this._ClientId; }

            set
            {
                if (this.AreDifferent(this._ClientId, value))
                {
                    this._ClientId = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ClientId, value), PropertyName_ClientId);
                    this.FirePropertyChanged(PropertyName_ClientId);
                }
            }
        }

        public string Country
        {
            get { return this._Country; }

            set
            {
                if (this.AreDifferent(this._Country, value))
                {
                    this._Country = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Country, value), PropertyName_Country);
                    this.FirePropertyChanged(PropertyName_Country);
                }
            }
        }

        public bool IsServiceRecordNoticeMandatory
        {
            get { return BeWoApp.AppSettings.IsServiceRecordNoticeMandatory; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.IsServiceRecordNoticeMandatory, value))
                {
                    BeWoApp.AppSettings.IsServiceRecordNoticeMandatory = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "IsServiceRecordNoticeMandatory", value ? "1" : "0");
                }
            }
        }

        public bool IsPasswordSecurityActiv
        {
            get { return BeWoApp.AppSettings.IsPasswordSecurityActiv; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.IsPasswordSecurityActiv, value))
                {
                    BeWoApp.AppSettings.IsPasswordSecurityActiv = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "IsPasswordSecurityActiv", value ? "1" : "0");
                }
            }
        }

        public bool IsEndDateVisible
        {
            get { return BeWoApp.AppSettings.IsEndDateVisible; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.IsEndDateVisible, value))
                {
                    BeWoApp.AppSettings.IsEndDateVisible = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "IsEndDateVisible", value ? "1" : "0");
                }
            }
        }

        public int MaxDaysEditServiceRecordsAllowed
        {
            get { return BeWoApp.AppSettings.MaxDaysEditServiceRecordsAllowed; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.MaxDaysEditServiceRecordsAllowed, value))
                {
                    BeWoApp.AppSettings.MaxDaysEditServiceRecordsAllowed = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "MaxDaysEditServiceRecordsAllowed", value.ToString());
                }
            }
        }

        public int TimeUntilUILock
        {
            get { return _TimeUntilUILock; }

            set
            {
                if (!AreDifferent(_TimeUntilUILock, value)) return;

                _TimeUntilUILock = value;
                Settings = BeWoApp.CreateNewSettingValue(_Settings, "TimeUntilUILock", value.ToString());

                AutoLockUI.LockUITime = value;
                AutoLockUI.StopTimer();
                AutoLockUI.StartAutoLockUIOption();
            }
        }


        public int HilfeplanAuslaufAuswahl
        {
            get { return BeWoApp.AppSettings.HilfeplanAuslaufAuswahl; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.HilfeplanAuslaufAuswahl, value))
                {
                    BeWoApp.AppSettings.HilfeplanAuslaufAuswahl = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "HilfeplanAuslaufAuswahl", value.ToString());
                }
            }
        }

        public int AnzTageZeiterfassErfolgt
        {
            get { return BeWoApp.AppSettings.AnzTageZeiterfassErfolgt; }

            set
            {
                if (this.AreDifferent(BeWoApp.AppSettings.AnzTageZeiterfassErfolgt, value))
                {
                    BeWoApp.AppSettings.AnzTageZeiterfassErfolgt = value;

                    this.Settings = BeWoApp.CreateNewSettingValue(this._Settings, "AnzTageZeiterfassErfolgt", value.ToString());
                }
            }
        }

        public string Name
        {
            get { return this._Name; }

            set
            {
                if (this.AreDifferent(this._Name, value))
                {
                    this._Name = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Name, value), PropertyName_Name);
                    this.FirePropertyChanged(PropertyName_Name);
                }
            }
        }

        public string PostalCode
        {
            get { return this._PostalCode; }

            set
            {
                if (this.AreDifferent(this._PostalCode, value))
                {
                    this._PostalCode = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.PostalCode, value), PropertyName_PostalCode);
                    this.FirePropertyChanged(PropertyName_PostalCode);
                }
            }
        }

        public string Settings
        {
            get { return _Settings; }

            set
            {
                if (AreDifferent(_Settings, value))
                {
                    _Settings = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Settings, value), PropertyName_Settings);
                    FirePropertyChanged(PropertyName_Settings);
                }
            }
        }

        public string State
        {
            get { return this._State; }

            set
            {
                if (this.AreDifferent(this._State, value))
                {
                    this._State = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.State, value), PropertyName_State);
                    this.FirePropertyChanged(PropertyName_State);
                }
            }
        }

        public string Street
        {
            get { return this._Street; }

            set
            {
                if (this.AreDifferent(this._Street, value))
                {
                    this._Street = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Street, value), PropertyName_Street);
                    this.FirePropertyChanged(PropertyName_Street);
                }
            }
        }

        public string Town
        {
            get { return this._Town; }

            set
            {
                if (this.AreDifferent(this._Town, value))
                {
                    this._Town = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Town, value), PropertyName_Town);
                    this.FirePropertyChanged(PropertyName_Town);
                }
            }
        }

        public override bool IsDirty
        {
            get
            {
                return (base.IsDirty || InvoiceNumber.IsDirty);
            }
        }        

        protected override void InitByDataContract(MandatorDC pDataContract)
        {
            this._Name = pDataContract.Name;
            this._ClientId = pDataContract.ClientId;
            this._BeWoClientType = pDataContract.BeWoClientType;
            this._Country = pDataContract.Country;
            this._State = pDataContract.State;
            this._Town = pDataContract.Town;
            this._PostalCode = pDataContract.PostalCode;
            this._Street = pDataContract.Street;
            this._Settings = pDataContract.Settings;

            if (_Settings == null)
            {
                _TimeUntilUILock = 30;
            }
            if (_Settings != null && _Settings.Contains(PropertyName_TimeUntilUILock))
            {
                var keyValuePairs = _Settings.SplitToKeyValuePairList(";", "=");
                _TimeUntilUILock = Convert.ToInt32(keyValuePairs.First(f => f.Key.Equals(PropertyName_TimeUntilUILock)).Value);
            }

            //HilfeplanAuslaufAuswahl
            if (_Settings == null)
            {
                _HilfeplanAuslaufAuswahl = 3;
            }
            if (_Settings != null && _Settings.Contains(PropertyName_HilfeplanAuslaufAuswahl))
            {
                var keyValuePairs = _Settings.SplitToKeyValuePairList(";", "=");
                _HilfeplanAuslaufAuswahl = Convert.ToInt32(keyValuePairs.First(f => f.Key.Equals(PropertyName_HilfeplanAuslaufAuswahl)).Value);
            }

            if (_Settings == null)
            {
                _AnzTageZeiterfassErfolgt = 0;
            }
            if (_Settings != null && _Settings.Contains(PropertyName_AnzTageZeiterfassErfolgt))
            {
                var keyValuePairs = _Settings.SplitToKeyValuePairList(";", "=");
                _AnzTageZeiterfassErfolgt = Convert.ToInt32(keyValuePairs.First(f => f.Key.Equals(PropertyName_AnzTageZeiterfassErfolgt)).Value);
            }

        }

        protected override MandatorDC MapToDataContract(MandatorDC pDataContract, bool doCommit)
        {
            pDataContract.Name = this._Name;
            pDataContract.ClientId = this._ClientId;
            pDataContract.BeWoClientType = this._BeWoClientType;
            pDataContract.Country = this._Country;
            pDataContract.State = this._State;
            pDataContract.Town = this._Town;
            pDataContract.PostalCode = this._PostalCode;
            pDataContract.Street = this._Street;
            pDataContract.Settings = this._Settings;

            return pDataContract;
        }
    }
}