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

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
            if (pRO.Services != null)
            {
                foreach (ServicesOverviewRO.ServiceDetail s in pRO.Services)
                {
                    s.ServiceCategory = "";

                    if (s.ServiceDescription == "")
                    {
                        
                    }
                }

            }

            SetKopfbereich(pRO);

            pRO.ApprovedFLSPerMonth = pRO.ApprovedFLSPerWeek * 4.33m;
            decimal gesamt = ((decimal)pRO.TotalFLMBillable + ((decimal)pRO.TotalFLMBillable/70*30)) / 60;
            decimal mehrMinusStunden = gesamt - pRO.ApprovedFLSPerMonth;
            decimal uebertragVormonat = BerechneUebertragVormonat(pRO);

            bool uebertrag = MussUebertragBerechnen(pRO);
            
            cellMehrMinusStunden.Text = String.Format("{0:0.000}", mehrMinusStunden);
            cellUebertragVormonat.Text = String.Format("{0:0.000}", uebertragVormonat);
            if (uebertrag)
            {
                cellUebertragNaechsterMonat.Text = String.Format("{0:0.000}", mehrMinusStunden + uebertragVormonat);
            }
            else
            {
                cellUebertragNaechsterMonat.Text = String.Format("{0:0.000}", 0);
            }

            
            cellWoechentlichKlientenbezogen.Text = String.Format("{0:0.00}", (pRO.ApprovedFLSPerMonth - uebertragVormonat) * 0.7m / 4.33m);
            cellMonatlichKlientenbezogen.Text = String.Format("{0:0.00}", pRO.ApprovedFLSPerMonth * 0.7m);
            this.bindingSource1.DataSource = pRO;
        }

        private decimal BerechneUebertragVormonat(ServicesOverviewRO pRO)
        {
            decimal uebertrag = 0;
            if (pRO.CostBearer2SupportConceptList != null && pRO.CostBearer2SupportConceptList.Count > 0)
            {
                var c2s = pRO.CostBearer2SupportConceptList[0];

                IList<ServiceRecord> allServiceRecords = c2s.ServiceRecords;

                decimal flmSoll = 0;
                

                DateTime start = c2s.StartDate.Value;
                int months = 0;
                while (start < pRO.StartDate)
                {
                    
                    decimal soll = pRO.ApprovedFLSPerWeek*4.33m;
                    if (start.Day != 1)
                    {

                    }
                    flmSoll += soll;
                    decimal ist = GetIstImMonat(allServiceRecords, start);
                    ist += ist/70*30;
                    ist /= 60;
                    var uebertragMonat = ist - soll;

                    uebertrag += uebertragMonat;
                    start = start.AddMonths(1);
                    start = new DateTime(start.Year, start.Month, 1);
                    months++;
                    var mod = months % 3;
                    if (mod == 0)
                        uebertrag = 0;
                }
                
             
            }
            return uebertrag;
        }

	    private decimal GetIstImMonat(IList<ServiceRecord> allServiceRecords, DateTime start)
	    {
	        DateTime monatEnd = start.AddMonths(1);
            monatEnd = new DateTime(monatEnd.Year, monatEnd.Month, 1);

	        decimal ist = 0;

            Dictionary<long, bool> groupBookingDict = new Dictionary<long, bool>();
            foreach (var item in allServiceRecords)
            {
                if (!item.GroupOid.HasValue || !groupBookingDict.ContainsKey(item.GroupOid.Value))
                {
                    if (item.Start != null)
                    {
                        if (item.Start >= start && item.Start.Value < monatEnd)
                        {
                            if (item.ServiceDescription != null && item.ServiceDescription.ServiceCategory != null && item.ServiceDescription.ServiceCategory.IsBillable)
                            {
                                ist += (item.RoundedDuration * item.ServiceDescription.ServiceCategory.Percentage) / 100;

                                if (item.GroupOid.HasValue)
                                    groupBookingDict.Add(item.GroupOid.Value, true);
                            }
                        }
                    }
                }
            }

	        return ist;
	    }

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

	            int months = 0;

	            DateTime start = c2s.StartDate.Value;

	            while (start < pRO.EndDate)
	            {
	                months++;
	                start = start.AddMonths(1);
	            }

	            var mod = months%3;
	            if (mod == 0)
	                return false;
	        }
            return true;
	    }

	    private void SetKopfbereich(ServicesOverviewRO pRO)
	    {
	        String employee1 = "";
	        String employee2 = "";

            if (pRO.CostBearer2SupportConceptList != null)
            {
                var cb2sc = pRO.CostBearer2SupportConceptList[0];

                if (cb2sc.CostBearerContactPerson != null)
                {
                    if (cb2sc.CostBearerContactPerson.Sex.HasValue &&
                        cb2sc.CostBearerContactPerson.Sex.Value == Sex.Female)
                    {
                        cellAnsprechpartner.Text = "Frau ";
                    }
                    else if (cb2sc.CostBearerContactPerson.Sex.HasValue &&
                        cb2sc.CostBearerContactPerson.Sex.Value == Sex.Male)
                    {
                        cellAnsprechpartner.Text = "Herr ";
                    }
                    cellAnsprechpartner.Text += cb2sc.CostBearerContactPerson.LastName;
                }
                foreach (var scap in cb2sc.ApprovalPeriodList)
                {
                    if (scap.StartDate <= pRO.EndDate && scap.EndDate >= pRO.StartDate)
                    {
                        if (scap.ServiceCategory != null)
                        {
                            cellLeistung.Text = scap.ServiceCategory.Name;
                        }
                    }
                }

                var customer = cb2sc.SupportConcept.Customer;

                foreach (var c2o in customer.Employee2CustomerList)
                {
                    foreach (var valueEntry in c2o.ValueList)
                    {
                        if (valueEntry.Entry.SystemEntryID != null && valueEntry.Entry.SystemEntryID.Value == SystemEntryID.EmployeeRoleMainAttendant)
                        {
                            if (String.IsNullOrEmpty(employee1))
                            {
                                employee1 = c2o.Employee.Person.FirstName + " " + c2o.Employee.Person.LastName;
                            }
                            else
                            {
                                employee2 = c2o.Employee.Person.FirstName + " " + c2o.Employee.Person.LastName;
                            }
                        }
                    }
                }
            }
	        pRO.MainAttendant = employee1;
	        cellBetreuer2.Text = employee2;

	    }
    }
}
