using System;
using System.Linq;
using BS.Shared;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using System.Collections.Generic;
using BS.Shared.Core;
using BS.Shared.Services;
using BS.Shared.DataContracts;
using BeWo.Service.Plugins;
using BeWo.Service.DCEntityMapper;
//using BeWo.Service.DCEntityMapper;

namespace BeWo.BeWoMobilReports
{
	public partial class Leistungsauswertung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
    {

        public Leistungsauswertung()
        {
            InitializeComponent();
            

        }

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

        private List<SollIstZeile> CreateDataSource(QueryRO ro)
        {

            
            IList<Employee> empListe = new List<Employee>();

            long oid;
            if (Int64.TryParse(ro.Rows[0].Field1.ToString(), out oid))
            {
                Employee emp = DAOFactory.GenericDAO.GetByID<Employee>(oid);

                empListe.Add(emp);
            }
            else
            {
                empListe = DAOFactory.GenericDAO.GetAllActive<Employee>();
            }

            DateTime at;
            DateTime dt;
            if (DateTime.TryParse(ro.Rows[0].Field2.ToString(), out at))
            {   
                dt = new DateTime(at.Year, 1, 1);
            }
            else
            {
                at = DateTime.Now;
                dt = new DateTime(at.Year, 1, 1);
            }

            List<SollIstZeile> alleZeilen = new List<SollIstZeile>();
            
            foreach (var e in empListe)
            {                
                DateTimeSpan span = new DateTimeSpan();
                span.StartDateTime = dt;
                span.EndDateTime = dt.AddYears(1).AddSeconds(-1);
                
                var records = DAOFactory.SearchDAO.FindEmployeeServiceRecords(e.Oid.Value, span, null, null);

                //Dictionary<long, SollIstZeile> customerDict = new Dictionary<long, SollIstZeile>();
               

                //Berechne Ist
                if (records.Count != 0)
                {
                    Dictionary<long, SollIstZeile> costbearerDict = new Dictionary<long, SollIstZeile>();

                    foreach (var sr in records.OrderBy(s => s.Start))
                    {
                        //1.Schritt zu welchem Klienten gehört der record

                        if (sr.CustomerOid.HasValue && sr.CostBearer2SupportConceptOid.HasValue)
                        {
                            SollIstZeile siz = null;


                            if (costbearerDict.ContainsKey(sr.CostBearer2SupportConceptOid.Value))
                            {
                                siz = costbearerDict[sr.CostBearer2SupportConceptOid.Value];
                            }
                            else
                            {
                                siz = new SollIstZeile();
                                siz.Mitarbeiter = String.Format("{0}, {1}", e.Person.LastName, e.Person.FirstName);
                                siz.Klient = String.Format("{0}, {1}", sr.Customer.Person.LastName, sr.Customer.Person.FirstName);
                                alleZeilen.Add(siz);
                                costbearerDict.Add(sr.CostBearer2SupportConceptOid.Value, siz);
                            }
                            //2. Schritt zu welchem  Monat

                            var duration = sr.RoundedDuration / 60;

                            if (sr.Start.Value.Month == 1)
                            {
                                siz.JanuarIst += duration;
                                                              
                            }
                            else if (sr.Start.Value.Month == 2)
                            {
                                siz.FebruarIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 3)
                            {
                                siz.MärzIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 4)
                            {
                                siz.AprilIst += duration;
                               
                            }
                            else if (sr.Start.Value.Month == 5)
                            {
                                siz.MaiIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 6)
                            {
                                siz.JuniIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 7)
                            {
                                siz.JuliIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 8)
                            {
                                siz.AugustIst += duration;
                             
                            }
                            else if (sr.Start.Value.Month == 9)
                            {
                                siz.SeptemberIst += duration;
                             
                            }
                            else if (sr.Start.Value.Month == 10)
                            {
                                siz.OktoberIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 11)
                            {
                                siz.NovemberIst += duration;
                                
                            }
                            else if (sr.Start.Value.Month == 12)
                            {
                                siz.DezemberIst += duration;
                                
                            }
                            siz.SummeIst += duration;
                            
                       }
                    
                    }

                    //Berechne Soll
                    foreach (var cbOid in costbearerDict.Keys)
                    {
                        CostBearer2SupportConcept c2s = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(cbOid);

                        // clac ist gleich null !!! 
                        var calc = PluginLoader.FindClass<Calculations>(c2s.CostBearer.ID) ?? Calculations.GetInstance(c2s.CostBearer.ID); 

                        var c2sDC = MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MapToNewDC(c2s);

                        var siz = costbearerDict[cbOid];

                        if (calc != null)
                        {
                            //Für Januar
                            siz.JanuarSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt, dt.AddMonths(1));

                            //Für Februar
                            siz.FebruarSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(1), dt.AddMonths(2));

                            //Für März
                            siz.MärzSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(2), dt.AddMonths(3));

                            //Für April
                            siz.AprilSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(3), dt.AddMonths(4));

                            //Für Mai
                            siz.MaiSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(4), dt.AddMonths(5));

                            //Für Juni
                            siz.JuniSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(5), dt.AddMonths(6));

                            //Für Juli
                            siz.JuliSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(6), dt.AddMonths(7));

                            //Für August
                            siz.AugustSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(7), dt.AddMonths(8));

                            //Für September
                            siz.SeptemberSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(8), dt.AddMonths(9));

                            //Für Oktober
                            siz.OktoberSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(9), dt.AddMonths(10));

                            //Für November
                            siz.NovemberSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(10), dt.AddMonths(11));

                            //Für Dezember
                            siz.DezemberSoll = calc.GetApprovedHoursForPeriod(c2sDC, dt.AddMonths(11), dt.AddYears(1));
                            
                            siz.SummeSoll = siz.JanuarSoll+siz.FebruarSoll+siz.MärzSoll+siz.AprilSoll+siz.MaiSoll+siz.JuniSoll+
                                            siz.JuliSoll+siz.AugustSoll+siz.SeptemberSoll+siz.OktoberSoll+siz.NovemberSoll+siz.DezemberSoll;
                        }
                        else
                        {
 
                        }
                    }                    
                }
                else {
                    SollIstZeile siz;
                    siz = new SollIstZeile();
                    siz.Mitarbeiter = String.Format("{0}, {1}", e.Person.LastName, e.Person.FirstName);  
                    alleZeilen.Add(siz);
                }
            }
            return alleZeilen;
        }

        private QueryRO.Row CreateMonatsRow(DateTime dt)
        {
            QueryRO.Row row = new QueryRO.Row();

            row.Field1 = String.Format("{0:MMM yyyy}", dt);

            row.ChildRows2 = new List<QueryRO.Row>();

            return row;
        }
    }
}
