using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using BeWo.Core.Service;
using System.Windows;
using BS.Shared.DataContracts.Reports;
using BS.Shared.DataContracts;
using BS.Shared.Services;

namespace BeWo.View.Report.Auslastung
{

    public class AuslastungGenerator
    {
        public static List<AuslastungRow> CreateAuslastungRows(AuslastungAnalysisRootDC root, AuslastungConfig config)
        {
            List<AuslastungRow> rows = new List<AuslastungRow>();
            try
            {
                if (root.EmployeeAnalysisList != null) 
                {
                    List<AuslastungAnalysisEmployeeDC> sortedList;
                    
                    sortedList = root.EmployeeAnalysisList.OrderBy(s => s.EmployeeLastName + ", " + s.EmployeeFirstName).ToList();
                    
                    foreach (var dc in sortedList)
                    {
                        AuslastungRow row = new AuslastungRow();
                        row.AuslastungAnalysisEmployee = dc;
                        row.DetailRows = CreateDetailRows(dc, config);
                        rows.Add(row);
                        
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace + "\n\nINNER:\n\n" + ex.InnerException);
            }
            return rows;
        }

        public static List<AuslastungDetailRow> CreateDetailRows(AuslastungAnalysisEmployeeDC dc, AuslastungConfig config)
        {
            List<AuslastungDetailRow> result = new List<AuslastungDetailRow>();

            List<int> dates = new List<int>();
            List<AuslastungValue> dateHeader = new List<AuslastungValue>();

            TimeSpan timeSpan = config.EndDate.Subtract(config.StartDate);
            AuslastungDetailRow header = new AuslastungDetailRow(AuslastungRowTypes.Header);
            header.CustomerName = "Klient/in";
            result.Add(header);


            decimal approvedFLSTotal = 0;
            decimal flsTotal = 0;

            int index = 1;


            foreach (AuslastungAnalysisSupportConceptDC scDC in dc.SupportConceptAnalysisList)
            {
                AuslastungDetailRow detailRow = new AuslastungDetailRow(AuslastungRowTypes.Detail);
                if (scDC.CustomerOid.HasValue)
                    detailRow.CustomerName = String.Format("{0} {1}", scDC.CustomerFirstName, scDC.CustomerLastName);
                else
                    detailRow.CustomerName = "Sonstiges";
                detailRow.FLS = 0;

                
                
                detailRow.Number = index++;
                if (scDC.CostBearer2SupportConcept != null)
                {
                    Calculations calc = null;
                    calc = Calculations.GetInstance(scDC.CostBearer2SupportConcept.CostBearer.CostBearerID);

                    detailRow.ApprovedFLS = calc.GetApprovedHoursPerWeek(scDC.CostBearer2SupportConcept, config.StartDate) ?? 0m;

                    detailRow.StartDate = scDC.CostBearer2SupportConcept.StartDate;
                    detailRow.EndDate = scDC.CostBearer2SupportConcept.EndDate;
                }
                approvedFLSTotal += detailRow.ApprovedFLS;

                
                foreach (ServiceRecordFLSOverviewDC srDC in scDC.ServiceRecords)
                {
                    DateTime date = srDC.Start.Value;
                    if (date >= config.StartDate && date <= config.EndDate)
                    {
                        if (srDC.ServiceDescription != null && srDC.ServiceDescription.Category != null && srDC.ServiceDescription.Category.IsBillable)
                        {
                            detailRow.FLS += srDC.RoundedDuration;
                            flsTotal += srDC.RoundedDuration;
                        }
                        else if (scDC.CustomerOid == null && scDC.CostBearer2SupportConcept == null)
                        {
                            detailRow.FLS += srDC.RoundedDuration;
                            flsTotal += srDC.RoundedDuration;
                        }
                     
                    }
                }
                detailRow.FLS = detailRow.FLS / 60;
                result.Add(detailRow);
            }


            AuslastungDetailRow totalRow = new AuslastungDetailRow(AuslastungRowTypes.Total);
            totalRow.CustomerName = "";
            
            totalRow.ApprovedFLS = approvedFLSTotal;
            totalRow.FLS = flsTotal / 60;

            result.Add(totalRow);
            return result;
        }

        
        
    }
}
