using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using BS.Shared;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
	        var approved = GetApprovedHoursPerYear(pRO);
	        if (approved.HasValue)
	        {
		        lblApprovedPerYear.Text = String.Format("{0:0.##} FLS", approved);
	        }
	        pRO.Month = String.Format("{0:MMM yy}", pRO.StartDate);

	        bool isWohnheim = false;

	        if (pRO.Services != null)
	        {
		        //var servicesBillable = new List<ServicesOverviewRO.ServiceDetail>();

		        foreach (var s in pRO.Services)
		        {
			        if (s.ServiceCategory.StartsWith("Wohnheim"))
			        {
				        s.TimeRangeString = s.ServiceDescription;
				        isWohnheim = true;
			        }

			        //if (s.IsBillable)
				    //    servicesBillable.Add(s);
		        }


		        //pRO.Services = servicesBillable;
	        }

	        if (isWohnheim)
	        {
		        int offset = 60;

				xrLabel10.Width += offset;
				xrLabel4.Left += offset;
		        xrLabel4.Width -= offset;

				lblApprovedPerYear.Width += offset;
				xrLabel2.Left += offset;
				xrLabel2.Width -= offset;

				xrLabel7.Width += offset;
				xrLabel16.Left += offset;
				xrLabel16.Width -= offset;

				cellTime.Width += offset;
				cellTimeSummary.Width += offset;
	        }
	        this.bindingSource1.DataSource = pRO;
        }

	    private decimal? GetApprovedHoursPerYear(ServicesOverviewRO ro)
	    {
		    if (ro.CostBearer2SupportConceptList.Count > 0)
		    {
			    var cb2sc = ro.CostBearer2SupportConceptList[0];
			    if (cb2sc.ApprovalPeriodList.Count > 0)
			    {
			        foreach (var scap in cb2sc.ApprovalPeriodList)
			        {
			            if (scap.StartDate <= ro.EndDate && scap.EndDate >= ro.StartDate)
			            {
			                if (scap.ApprovedBEInterval.HasValue &&
                                scap.ApprovedBEInterval.Value == SupportConceptApprovalInterval.Yearly)
			                {
			                    return scap.ApprovedBEPerInterval;
			                }
			            }
			        }
				    
			    }
		    }
		    return null;
	    }
    }
}
