using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using BeWo.Service.ServiceImplementations;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using DevExpress.XtraReports.UI;
using System.Data;

namespace BeWo.GpzBiberachReports 
{
	public partial class KlientenBetreuungsart : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
    {
		public KlientenBetreuungsart()
        {
            InitializeComponent();

        }

        public void SetReportDataSource(QueryRO pRO)
        {
			this.bindingSource1.DataSource = CreateTable(pRO); ;
        }

		private QueryRO CreateTable(QueryRO ro)
		{
			OperationsServiceImp opService = new OperationsServiceImp();

		    QueryRO newQueryRo = new QueryRO();
		    int year = 0;
		
			if (ro.Rows != null)
		    {
			    DateTime dt;
			    if (DateTime.TryParse(ro.Rows[0].Field1.ToString(), out dt))
			    {
				    year = dt.Year;

				    
			    }
				lblHeader.Text = String.Format("Klienten und zugeordnete Betreuungsarten {0:yyyy}", dt);
		}

			IList<Customer> list = DAOFactory.GenericDAO.GetAllActiveAndArchived<Customer>();

		    foreach (Customer c in list)
		    {
				QueryRO.Row row = new QueryRO.Row();
				newQueryRo.Rows.Add(row);

				row.Field1 = String.Format("{0}, {1}", c.Person.LastName, c.Person.FirstName);


				String betreuungsarten = "";
				foreach (var v2o in c.ValueList)
				{

					ValueListEntry vle = v2o.Entry;
					if (vle.Type == ValueListEntryType.CustomerCareType)
					{

						if (String.IsNullOrEmpty(betreuungsarten))
							betreuungsarten = vle.Value;
						else
						{
							betreuungsarten += ", " + vle.Value;
						}
					}

				}
				row.Field2 = betreuungsarten;


			    var scList = c.SupportConcepts;

				StringBuilder sb = new StringBuilder();

			    foreach (var sc in scList)
			    {
				    if (sc.CostBearer2SupportConceptList != null && sc.CostBearer2SupportConceptList.Count > 0)
				    {
					    foreach (CostBearer2SupportConcept cb2sc in sc.CostBearer2SupportConceptList)
					    {
						    foreach (var scap in cb2sc.ApprovalPeriodList)
						    {
							    if (scap.StartDate.HasValue &&
							        (scap.StartDate.Value.Year <= year && (!scap.EndDate.HasValue || scap.EndDate.Value.Year >= year)))
							    {
								    DateTime start = scap.StartDate.Value;
								    DateTime? end = scap.EndDate;


								    if (sb.Length > 0)
									    sb.AppendLine("");
								    sb.Append(start.ToShortDateString());
								    sb.Append(" - ");

								    if (end != null)
								    {
									    sb.Append(end.Value.ToShortDateString());
								    }

							    }
						    }
					    }
				    }

			    }

			    //HP Zeitraum
				row.Field3 = String.Format("{0}", sb);
		    }


		    newQueryRo.Rows.Sort((a, b) => a.Field1.ToString().CompareTo(b.Field1));

		    return newQueryRo;
	    }

		private String GetVarFieldValue(Customer customer, Dictionary<long, VarFieldDef> oid2VarfieldDef, string varFieldName)
		{
			
			if (customer.VarFieldValueList != null)
			{
				foreach (var varField in customer.VarFieldValueList)
				{
					if (oid2VarfieldDef.ContainsKey(varField.VarFieldDefOid.Value))
					{
						var def = oid2VarfieldDef[varField.VarFieldDefOid.Value];

						if (def.Label == varFieldName)
						{
							var varFieldValue = Utils.XMLDeserializeFromString(varField.SerializedValue, Type.GetType(varField.TypeName));
							if (varFieldValue != null)
							{
								return varFieldValue.ToString();
								
							}
							
						}
					}
				}
			}
			return String.Empty;
		}
    }
}
