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.DCEntityMapper;
using BeWo.Service.ServiceImplementations;

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

        public Finanzauswertung()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(QueryRO pRO)
        {

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

        private QueryRO CreateDataSource(QueryRO ro)
        {
            QueryRO newQueryRo = new QueryRO();

            AnalysisServiceImp service = new AnalysisServiceImp();
            var financeList = service.GetOutstandingReceivables(false, false);

            OperationsServiceImp op = new OperationsServiceImp();

            foreach (var item in financeList)
            {
                if (item.SupportConcept != null && item.SupportConcept.ActivationType == ActivationTypeId.Active)
                {
                    QueryRO.Row row = new QueryRO.Row();

                    row.Field1 = String.Format("{0}\r\n{1}", item.CustomerName, item.SupportConceptTimeSpanString);
                    row.Field2 = String.Format("{0}", item.CostBearerName);

                    row.Field3 = String.Format("{0:c}", item.AmountTotal);
                    row.Field4 = String.Format("{0:c}", item.AmountPaid);
                    row.Field5 = String.Format("{0:c}", item.OpenAmount);


                    var sc = DAOFactory.GenericDAO.LoadByID<SupportConcept>(item.SupportConcept.SupportConceptOid);

                    decimal billableHours = 0;
                    decimal approvedHours = 0;

                    foreach (var cb2sc in sc.CostBearer2SupportConceptList)
                    {
                        var calc = Calculations.GetInstance(cb2sc.CostBearer.ID);
                        var relDC =
                            MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MapToNewDC(cb2sc);
                        approvedHours += calc.GetApprovedHoursTillNow(relDC, null) ?? 0;

                        billableHours +=
                            calc.GetBillableDurationInMinutes(
                                cb2sc.ServiceRecords.Where(sr => sr.Start.Value.Date <= DateTime.Now.Date).ToList().
                                    MapToNewDCs())/60;

                    }
                    row.Field6 = String.Format("{0:0.00}", approvedHours);
                    row.Field7 = String.Format("{0:0.00}", billableHours);

                    newQueryRo.Rows.Add(row);
                }
            }

            //newQueryRo.Rows.Sort((a, b) => a.Field1.ToString().CompareTo(b.Field1));
            
            return newQueryRo;
        }


    }

}
