using System;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BS.Shared;
using BS.Shared.Core;
using DevExpress.Data.Linq;
using DevExpress.XtraReports.UI;

namespace BeWo.LebenshilfeDuisburgReports
{
	[IDSpecificClass(Identifier = "LH-IFF")]
    public partial class ServiceInvoiceReportIFF : XtraReport, IBeWoReport<ServiceInvoiceRO>
    {
		public ServiceInvoiceReportIFF()
        {
            this.InitializeComponent();
        }

        public void SetReportDataSource(ServiceInvoiceRO pRO)
        {
			pRO.Notice = "";

			
			if (pRO.InvoiceDateTime.HasValue)
				pRO.Notice = String.Format("{0:dd.MM.yyyy}", pRO.InvoiceDateTime.Value.AddDays(30));

	        if (pRO.ServiceInvoicePeriods != null)
	        {
		        foreach (var sip in pRO.ServiceInvoicePeriods)
		        {
			        if (sip.ServiceInvoiceItems != null)
			        {
				        foreach (var ii in sip.ServiceInvoiceItems)
				        {
					        if (!String.IsNullOrEmpty(ii.Notice))
					        {
						        ii.ServiceDescription = String.Format("{0}\n{1}", ii.ServiceDescription, ii.Notice);
					        }

					        ii.CustomerFirstname = String.Format("{0:c}", ii.AmountPerUnit);
					        if (!String.IsNullOrEmpty(ii.RateFactor))
					        {
								
								var rfStr = ii.RateFactor.Replace("%", "").Replace(",00", "").Trim();
								int rfInt = 0;
								Int32.TryParse(rfStr, out rfInt);

								decimal rateFactor = (100m + rfInt) / 100m;
						        if (rateFactor > 1)
						        {
							        ii.CustomerFirstname = String.Format("{0} x {1:0.##}", ii.CustomerFirstname, rateFactor);
						        }
						        else
						        {
									ii.CustomerFirstname = String.Format("{0}", ii.CustomerFirstname);
						        }
					        }
						        
				        }
			        }
		        }

		        
	        }

			SetCustomerInfos(pRO);
			
            this.bindingSource1.DataSource = pRO;
        }

		private void SetCustomerInfos(ServiceInvoiceRO pRO)
		{
			Customer c = null;
			if (pRO.CustomerOid.HasValue)
			{
				c = DAOFactory.GenericDAO.LoadByID<Customer>(pRO.CustomerOid.Value);
			}
			if (c == null)
			{
				if (pRO.ServiceInvoicePeriods.Count > 0)
				{
					var c2s = pRO.ServiceInvoicePeriods[0].CostBearer2SupportConcept;
					c = c2s.SupportConcept.Customer;
				}
			}

			StringBuilder sb = new StringBuilder();
			if (c != null)
			{

				if (c.Person.InvoiceAddress != null)
				{
					sb.AppendLine(c.Person.InvoiceAddress.AddressLine1);
				}
				else
				{
					sb.AppendLine(c.Person.FirstNameLastName);
				}
				if (c.Person.InvoiceAddress != null && !String.IsNullOrEmpty(c.Person.InvoiceAddress.Street))
				{
					sb.AppendLine(c.Person.InvoiceAddress.Street);
				}
				else if (c.Person.Address != null && !String.IsNullOrEmpty(c.Person.Address.Street))
				{
					sb.AppendLine(c.Person.Address.Street);
				}
				if (c.Person.InvoiceAddress != null && (!String.IsNullOrEmpty(c.Person.InvoiceAddress.PostalCode) || !String.IsNullOrEmpty(c.Person.InvoiceAddress.Town)))
				{
					sb.AppendLine(String.Format("{0} {1}", c.Person.InvoiceAddress.PostalCode, c.Person.InvoiceAddress.Town));
				}
				else if (c.Person.Address != null && (!String.IsNullOrEmpty(c.Person.Address.PostalCode) || !String.IsNullOrEmpty(c.Person.Address.Town)))
				{
					sb.AppendLine(String.Format("{0} {1}", c.Person.Address.PostalCode, c.Person.Address.Town));
				}
			}

			sb.AppendLine("über");
			sb.AppendLine(pRO.RecipientOrganisation);
			sb.AppendLine(pRO.RecipientStreet);
			sb.Append(pRO.RecipientPostCodeAndTown);
			lblAnschrift.Text = sb.ToString();


			String leistungkategorie = "";

			if (pRO.ServiceInvoicePeriods != null)
			{
				foreach (var sp in pRO.ServiceInvoicePeriods)
				{
					if (sp.ServiceInvoiceItems != null)
					{
						foreach (var ii in sp.ServiceInvoiceItems)
						{
							if (String.IsNullOrEmpty(leistungkategorie))
							{
								leistungkategorie = ii.ServiceDescription;
							}
						}
					}
				}
			}

			sb = new StringBuilder();
			sb.AppendLine(String.Format("{0} für", leistungkategorie));
			sb.AppendLine(String.Format("{0}, geb. {1:dd.MM.yyyy}", pRO.CustomerName, pRO.CustomerBirthdate));
			sb.Append(String.Format("wohnhaft {0}, {1} {2}", pRO.CustomerStreet, pRO.CustomerPostalCode, pRO.CustomerTown));
			
			lblCustomerInfo.Text = sb.ToString();
			
			//if (pRO.ServiceInvoicePeriods.Count > 0)
			//{
			//	var c2s = pRO.ServiceInvoicePeriods[0].CostBearer2SupportConcept;
			//	var org = c2s.CostBearer.Organisation;
			//	if (org != null)
			//	{
			//		//lblKreditorennr.Text =  org.
			//	}
			//}
		}
    }
}