using System;
using System.Collections.Generic;

using BeWo.ServiceProxy;
using BeWo.Validation;

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

namespace BeWo.View.Detail
{
	public partial class ServiceRecordRebookingView
	{
		[ValidationAttribute(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
		public DateTime StartDatum { get; set; }

		[Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
		public DateTime EndDatum   { get; set; }

		public CompactSupportConceptDC Zielhilfeplan { get; set; }
		public SupportConceptCostBearerRelDC ZielhilfeplanKostentraegerRelDC { get; set; }

		public SupportConceptCostBearerRelDC SupportConceptCostBearerRelDC { get; set; }

		public ServiceRecordRebookingView(SupportConceptCostBearerRelDC supportConceptCostBearerRelDC)
		{
			InitializeComponent();

			StartDatum                    = DateTime.Now;
			EndDatum                      = DateTime.Now.AddDays(1);
			SupportConceptCostBearerRelDC = supportConceptCostBearerRelDC;

			DataContext = this;
		}

		public void SetSearchText(String text)
		{
			supportConceptSelectionControl.SearchText = text;
		}

		private void supportConceptSelectionControl_ItemSelected(object sender, EventArgs<FlatSupportConceptTreeNodeDC> e)
		{
			Zielhilfeplan                   = e.Data.SupportConceptTreeNodeDC.SupportConcept;
			ZielhilfeplanKostentraegerRelDC = e.Data.SupportConceptTreeNodeDC.SupportConceptCostBearerRelDC;
		}

		public void Save(Action<List<ServiceRecordDC>> pCallBack)
		{
			var x = new DateTimeSpan
			{
				StartDate    = StartDatum, 
				StartHours   = 0, 
				StartMinutes = 0, 
				EndDate      = EndDatum, 
				EndHours     = 23, 
				EndMinutes   = 59
			};

			ServiceFacade.DoOperationsServiceAsync(s => s.FindServiceRecordsInSpan(SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid.Value, x),
				records2Rebook =>
				{
					foreach (var record in records2Rebook)
					{
						record.SupportConcept               = Zielhilfeplan;
						record.CostBearer                   = ZielhilfeplanKostentraegerRelDC.CostBearer;
						record.CostBearer2SupportConceptOid = ZielhilfeplanKostentraegerRelDC.CostBearer2SupportConceptOid;
						record.Customer                     = Zielhilfeplan.Customer;
					}

					pCallBack(records2Rebook);
				});
		}
	}
}
