using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

using BeWoPlanerMobil.Service;

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

namespace BeWoPlanerMobil.Models
{
	public class MainModel
	{
        [Display(Name = "Nur meine Hilfepläne anzeigen")]
        public bool ShowOnlyOwnSupportConcepts { get; set; }

		[Display(Name = "Hilfeplan")]
		public long? CostBearer2SupportConceptOid { get; set; }

		[Display(Name = "Kategorie")]
		public long? SelectedServiceCategoryConceptOid { get; set; }

		public long? SelectedServiceDescriptionOid { get; set; }

		public long? SelectedServiceRecordOid { get; set; }

		public SupportConceptDC SelectedSupportConcept { get; set; }

		public int Zeitraum { get; set; }

        public bool Schalter { get; set; }

        //######### neuer wert zur überprüfung 
        public int ErrorWert { get; set; }

        public bool ButtonsSchalter { get; set; }

        public long SrviceRecoOID { get; set; }

		public List<SupportConceptDC> SupportConcepts { get; set; }
		public List<ServiceCategoryModel> ServiceCategories { get; set; }

		private List<ServiceRecordDC> serviceRecords;

		public List<ServiceRecordDC> ServiceRecords
		{
			get
			{
				if(serviceRecords == null)
					return serviceRecords = new List<ServiceRecordDC>();

				return serviceRecords.OrderByDescending(sr => sr.Start).ToList();
			}
			set { serviceRecords = value; }
		}

		public List<ValueListEntryDC> SelectedGoals { get; set; }

        public ValueListEntryDC[] SelectedGoalsforType
        { 
            get{
                var a = new MobileSessionFacade().ValueListService.GetAllValueListEntrysByType(ValueListEntryType.DocumentType);
  
                return a; 
            }
            set { SelectedGoalsforType = value; }
        }

		public ServiceRecordDC NewServiceRecord { get; set; }

		public long? ServiceRecordOidToDelete { get; set; }

		public static bool IsEditBtnVisible
		{
			get { return MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Edit))); }
		}

		public static bool IsDeleteBtnVisible
		{
			get { return MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Delete))); }
		}

		public static bool IsAllowedToCreateOrEditRecord
		{
			get { return MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Create))) ||
						 MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Edit)));
			}
		}

		public static bool IsOnlyAllowedToEdit
		{
			get
			{
				return !MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Create))) &&
						MobileSessionFacade.LoggedInUser.UserGroups.Any(a => a.Rights.Any(f => f.RightType.Equals(UserRightType.ServiceRecordView_Edit)));
			}
		}

		public static bool IsAllowedToSeeCustomers
		{
			get 
			{ 
				return MobileSessionFacade.CheckForUserRight(UserRightType.CustomerView_View) || 
					   MobileSessionFacade.CheckForUserRight(UserRightType.Customer_ViewMyCustomers); 
			}
		}

		public static bool IsEmployeeSelectionVisible
		{
			get
			{
				return MobileSessionFacade.CheckForUserRight(UserRightType.ServiceRecord_AllowCreationForOtherEmployees) ||
				       MobileSessionFacade.CheckForUserRight(UserRightType.ServiceRecord_AllowCreationForOtherTeamMember);
			}
		}

		public MainModel()
		{
			NewServiceRecord           = new ServiceRecordDC();
		    ShowOnlyOwnSupportConcepts = true;
			Zeitraum                   = -1;
            ErrorWert                  = 1;
		}

		public IEnumerable<SelectListItem> SupportConceptListItems
		{
			get
			{
				var allCostBearerRelations = new List<SelectListItem> { new SelectListItem { Selected = true, Value = "-1", Text = String.Empty }, new SelectListItem {Value = "-2", Text = "Ohne Hilfeplan"} };
				foreach (var sc in SupportConcepts.OrderBy(sc => sc.Customer.LastName))
				{
					allCostBearerRelations.AddRange(sc.CostBearerRelations.Select(cb => new SelectListItem
					{
						Value = cb.CostBearer2SupportConceptOid.ToString(), Text = string.Format("{0}, {1} | {2}-{3} {4}", sc.Customer.LastName, sc.Customer.FirstName, (cb.StartDate.HasValue ? cb.StartDate.Value.ToShortDateString().Remove(6, 2) : string.Empty), (cb.EndDate.HasValue ? cb.EndDate.Value.ToShortDateString().Remove(6, 2) : string.Empty), cb.CostBearer.Name)
					}));
				}

				return allCostBearerRelations;
			}
		}

		public IEnumerable<SelectListItem> ServiceCategoryListItems
		{
			get
			{
				var result = new List<SelectListItem>();
				result.AddRange(ServiceCategories.Select(item => new SelectListItem {Value = item.ServiceCategoryOid.Value.ToString(), Text = item.Name}).ToList());

				return result;
			}
		}

		public List<CustomerDC> Customers { get; set; }

		public List<CompactEmployeeDC> Employees { get; set; }

		public EmployeeDC Employee { get; set; }

		[Display(Name = "Klient")]
		public long? SelectedCustomerOid { get; set; }

		public CustomerDC SelectedCustomer { get; set; }

		public IEnumerable<SelectListItem> CustomerListItems
		{
			get
			{
				var result = new List<SelectListItem> {new SelectListItem {Value = "-1", Text = String.Empty}};
				result.AddRange(Customers.Select(item => item.CustomerOid != null ? new SelectListItem { Value = item.CustomerOid.Value.ToString(), Text = String.Format("{1}, {0}", item.FirstName, item.LastName) } : null).ToList());

				return result;
			}
		}

		public static List<CompactEmployeeDC> AllEmployees { get; set; }

		[Display(Name = "Mitarbeiter")]
		public long? SelectedEmployeeOid { get; set; }

		public CompactEmployeeDC SelectedEmployee { get; set; }

		public IEnumerable<SelectListItem> EmployeeListItems
		{
			get
			{
				var result = new List<SelectListItem>();
				result.AddRange(Employees.Select(item => new SelectListItem {Value = item.EmployeeOid.ToString(), Text = String.Format("{0}, {1}", item.LastName, item.FirstName), Selected = item.Equals(MobileSessionFacade.LoggedInCompactEmployee)}).ToList());
				
				return result;
			}
		}
       
	}
}