using System;
using System.Collections.Generic;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared;

namespace BeWo.Service.Configuration
{
    public class AppSettings
    {

        public bool IsServiceRecordNoticeMandatory { get; set; }

        public bool IsPasswordSecurityActiv { get; set; }

        public bool IsEndDateVisible { get; set; }

        public int MaxDaysEditServiceRecordsAllowed { get; set; }

        public int HilfeplanAuslaufAuswahl { get; set; }

        public int AnzTageZeiterfassErfolgt { get; set; }

        public bool ShowAdvisedAttendedFlag { get; set; }
		
        public bool ShowAllClients { get; set; }

        public bool ShowExpiredSupportConcepts { get; set; }
		
        public bool ShowOnlyMyClients { get; set; }

        public bool ShowLargeCustomerNotice { get; set; }

        public bool ShowOnlyMySupportConcepts { get; set; }
   
        public string TimeRecordingMenuName { get; set; }

		public bool ShowDistanceFields { get; set; }

		public bool ShowCustomerDistanceFields { get; set; }

		public bool ShowAdditionalService { get; set; }

		public bool ShowRoundedDurationInEmployeeAnalysis { get; set; }

		public bool SortServiceDescriptionAlphabetically { get; set; }

		
		public static AppSettings CreateSettings()
		{
			AppSettings settings = new AppSettings();

			var mandator = GetMandatorEntity();

            bool isServiceRecordNoticeMandatory = true;
            bool isPasswordSecurityActiv = false;
            bool isEndDateVisible = true;
			bool showServiceRecordsDistanceFields = false;
			bool showCustomerDistanceFields = false;
			bool showAdditionalService = false;
			bool showRoundedDurationInEmployeeAnalysis = true;
            int maxDaysEditServiceRecordsAllowed = 0;
            int hilfeplanAuslaufAuswahl = 3;
            int anzTageZeiterfassErfolgt = 0;
			bool sortServiceDescriptionAlphabetically = false;
			settings.ShowAdvisedAttendedFlag = false;


			bool printerSettingsUseLandscape = false;
			bool printerSettingsUseMargins = false;
			bool printerSettingsUsePaperKind = false;



			if (mandator != null)
            {
				string val = GetSettingValue(mandator.Settings, "IsServiceRecordNoticeMandatory");
                if (val != null && val.Equals("0"))
                {
                    isServiceRecordNoticeMandatory = false;
                }

                val = GetSettingValue(mandator.Settings, "IsPasswordSecurityActiv");
                if (val != null && val.Equals("1"))
                {
                    isPasswordSecurityActiv = true;
                }

                val = GetSettingValue(mandator.Settings, "IsEndDateVisible");
                if (val != null && val.Equals("0"))
                {
                    isEndDateVisible = false;
                }

				val = GetSettingValue(mandator.Settings, "MaxDaysEditServiceRecordsAllowed");
                int result;
                if (Int32.TryParse(val, out result))
                {
                    maxDaysEditServiceRecordsAllowed = result;
                }

                val = GetSettingValue(mandator.Settings, "hilfeplanAuslaufAuswahl");
                int resulthilf;
                if (Int32.TryParse(val, out resulthilf))
                {
                    hilfeplanAuslaufAuswahl = resulthilf;
                }


                val = GetSettingValue(mandator.Settings, "AnzTageZeiterfassErfolgt");
                int resultanzeit;
                if (Int32.TryParse(val, out resultanzeit))
                {
                    anzTageZeiterfassErfolgt = resultanzeit;
                }


				val = GetSettingValue(mandator.Settings, "ShowDistanceFields");
				if (val != null && val.Equals("1"))
				{
					showServiceRecordsDistanceFields = true;
				}
				val = GetSettingValue(mandator.Settings, "ShowCustomerDistanceFields");
				if (val != null && val.Equals("1"))
				{
					showCustomerDistanceFields = true;
				}
				val = GetSettingValue(mandator.Settings, "ShowAdditionalService");
				if (val != null && val.Equals("1"))
				{
					showAdditionalService = true;
				}
				val = GetSettingValue(mandator.Settings, "ShowRoundedDurationInEmployeeAnalysis");
				if (val != null && val.Equals("0"))
				{
					showRoundedDurationInEmployeeAnalysis = false;
				}
				val = GetSettingValue(mandator.Settings, "SortServiceDescriptionAlphabetically");
				if (val != null && val.Equals("1"))
				{
					sortServiceDescriptionAlphabetically = true;
				}
				val = GetSettingValue(mandator.Settings, "PrinterSettingsUseLandscape");
				if (val != null && val.Equals("1"))
				{
					printerSettingsUseLandscape = true;
				}
				val = GetSettingValue(mandator.Settings, "PrinterSettingsUseMargins");
				if (val != null && val.Equals("1"))
				{
					printerSettingsUseMargins = true;
				}
				val = GetSettingValue(mandator.Settings, "PrinterSettingsUsePaperKind");
				if (val != null && val.Equals("1"))
				{
					printerSettingsUsePaperKind = true;
				}

            }

			
			settings.IsServiceRecordNoticeMandatory = isServiceRecordNoticeMandatory;
            settings.IsPasswordSecurityActiv = isPasswordSecurityActiv;
            settings.IsEndDateVisible = isEndDateVisible;
			settings.MaxDaysEditServiceRecordsAllowed = maxDaysEditServiceRecordsAllowed;
            settings.HilfeplanAuslaufAuswahl = hilfeplanAuslaufAuswahl;
            settings.AnzTageZeiterfassErfolgt = anzTageZeiterfassErfolgt;
			settings.ShowDistanceFields = showServiceRecordsDistanceFields;
			settings.ShowCustomerDistanceFields = showCustomerDistanceFields;
			settings.ShowAdditionalService = showAdditionalService;
			settings.ShowRoundedDurationInEmployeeAnalysis = showRoundedDurationInEmployeeAnalysis;
			settings.SortServiceDescriptionAlphabetically = sortServiceDescriptionAlphabetically;
			
			return settings;
		}

	    public static Mandator GetMandatorEntity()
		{
			var manList = DAOFactory.GenericDAO.GetAll<Mandator>();
			Mandator man = null;
			if (manList == null || manList.Count == 0)
			{
				man = new Mandator();
				man.Oid = 1;

				DAOFactory.GenericDAO.Insert(man);
			}
			else
			{
				man = manList[0];
			}

			return man;
		}

		private static string GetSettingValue(string settings, string key)
		{
			if (!String.IsNullOrEmpty(settings))
			{
				if (settings.IndexOf(";") == -1 && settings.IndexOf("=") == -1)
				{
					return settings;
				}

				string[] keyValuePairs = settings.Split(';');

				foreach (string pair in keyValuePairs)
				{
					string[] keyValuePair = pair.Split('=');
					if (keyValuePair.Length == 2)
					{
						if (keyValuePair[0] == key)
						{
							return keyValuePair[1];
						}
					}
				}
			}

			return null;
		}

		
	}
}