using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BS.Shared.Core;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

namespace BeWo.HPHBersenbrueck
{
    public partial class Stundenzettel : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<ServicesOverviewRO>
    {
        public Stundenzettel()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
			SetVarFieldValues(pRO);
			if (pRO.Services != null)
			{
				List<ServicesOverviewRO.ServiceDetail> servicesBillable = new List<ServicesOverviewRO.ServiceDetail>();

				foreach (ServicesOverviewRO.ServiceDetail s in pRO.Services)
				{
					if (s.IsBillable)
					{
						if (!s.ServiceDescription.ToLower().Contains("intern"))
						{
							DateTime dt = DateTime.Now.Date.AddMinutes(s.Minutes);
							TimeSpan tspan = dt.Subtract(DateTime.Now.Date);

							double d = tspan.TotalHours;
							d = Math.Floor(d);

							s.TimeRangeString = String.Format("{0}:{1:00}", d, tspan.Minutes);

							//if (s.StartDate.Second == 0)
							//{
							//    s.Notice = String.Format("{0:HH:mm}h: {1}", s.StartDate, s.Notice);
							//}
							servicesBillable.Add(s);
						}
					}
				}

				pRO.Services = servicesBillable;
			}

			
			DateTime dt2 = DateTime.Now.Date.AddMinutes(pRO.TotalFLMBillable);
			TimeSpan tspan2 = dt2.Subtract(DateTime.Now.Date);

			double d2 = tspan2.TotalHours;
			d2 = Math.Floor(d2);

			lblMinutesTotal.Text = String.Format("{0}:{1:00}", d2, tspan2.Minutes);

	        if (pRO.RateFactor != 0)
	        {
		        
	        }
			lblFLSGesamt.Text = String.Format("entspricht {0:0.##} FLS", pRO.TotalFLMBillable / 60);
			
            this.bindingSource1.DataSource = pRO;
        }

		private void SetVarFieldValues(ServicesOverviewRO pRO)
		{
			String allLeben = ""; // Oid = 16
			String indBasis = ""; // Oid = 17
			String gestaltung = ""; // Oid = 18
			String teilnahme = ""; // Oid = 19
			String kommunikation = ""; // Oid = 20
			String emotion = ""; // Oid = 21
			String gesund = ""; // Oid = 22
			String globalZiel = ""; // Oid = 23
			String zieleDerHilfe = ""; // Oid = 24


			if (pRO.CostBearer2SupportConceptList != null && pRO.CostBearer2SupportConceptList.Count > 0)
			{
				var c2s = pRO.CostBearer2SupportConceptList[0];

				Customer c = c2s.SupportConcept.Customer;

				foreach (var vv in c.VarFieldValueList)
				{
					var varFieldValue = Utils.XMLDeserializeFromString(vv.SerializedValue, Type.GetType(vv.TypeName));
					if (varFieldValue != null)
					{
						if (vv.VarFieldDefOid.Value == 16)
						{
							allLeben = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 17)
						{
							indBasis = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 18)
						{
							gestaltung = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 19)
						{
							teilnahme = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 20)
						{
							kommunikation = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 21)
						{
							emotion = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 22)
						{
							gesund = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 23)
						{
							globalZiel = varFieldValue.ToString();
						}
						else if (vv.VarFieldDefOid.Value == 24)
						{
							zieleDerHilfe = varFieldValue.ToString();
						}
					}
				}
				
			}

			lblAlltaeglicheLebensfuehrung.Text = allLeben;
			lblIndividuelleBasisversorgung.Text = indBasis;
			lblGestaltungSozialerBeziehungen.Text = gestaltung;
			lblTeilnahme.Text = teilnahme;
			lblKommUndOr.Text = kommunikation;
			lblEmoUndPsy.Text = emotion;
			lblGesund.Text = gesund;
			lblGlobalziel.Text = globalZiel;
			lblZieleDerHilfe.Text = zieleDerHilfe;


			decimal gesamt = 0;

			decimal test = 0;

			if (Decimal.TryParse(allLeben, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(indBasis, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(gestaltung, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(teilnahme, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(kommunikation, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(emotion, out test))
			{
				gesamt += test;
			}
			test = 0;
			if (Decimal.TryParse(gesund, out test))
			{
				gesamt += test;
			}
			lblHilfeschwerpunkteGesamt.Text = String.Format("{0:0.##}", gesamt);
		}
    }
}
