using System;
using System.Linq;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using System.Collections.Generic;
using BeWo.Service.DCEntityMapper;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Services;
using DevExpress.XtraPrinting;

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

        public NotRoundedServiceRecords()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(QueryRO pRO)
        {

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

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


            var srArray = DAOFactory.SearchDAO.GetBillableServiceRecords().ToArray();


            for (int i = 0; i < srArray.Length; i++)
            {
                ServiceRecord sr = srArray[i];


                if (sr.CustomerOid.HasValue && sr.EmployeeOid.HasValue && !sr.GroupOid.HasValue)
                {
                    var rd = sr.RoundedDuration;

                    decimal minutes = (decimal)sr.End.Value.Subtract(sr.Start.Value).TotalMinutes;

                    var calc = Calculations.GetInstance(sr.CostBearer2SupportConcept.CostBearer.ID);

                    CompactOrganisationDC org =
                        MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(sr.CostBearer2SupportConcept.CostBearer.Organisation);

                    var roundedDuration = calc.GetRoundedDuration(org, minutes);

                    if (rd != roundedDuration)
                    {
                        QueryRO.Row row = new QueryRO.Row();

                        String name1 = "";

                        Customer c1 = sr.Customer;
                        name1 = String.Format("{0}, {1}", c1.Person.LastName, c1.Person.FirstName);


                        //Employee e1 = sr.Employee;

                        CostBearer2SupportConcept c2s1 = sr.CostBearer2SupportConcept;


                        row.Field1 = String.Format("{0}", name1);
                        row.Field2 = String.Format("{0:dd.MM.yy} - {1:dd.MM.yy}", c2s1.StartDate, c2s1.EndDate);

                        row.Field3 = String.Format("{0:dd.MM.yy HH:mm} - {1:HH:mm}", sr.Start, sr.End);

                        row.Field4 = String.Format("{0:0}", rd);
                        row.Field5 = String.Format("{0:0}", roundedDuration);


                        newQueryRo.Rows.Add(row);

                    }



                }

            }

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

            if (newQueryRo.Rows.Count == 0)
            {
                lblGefunden.Visible = true;
                xrTable2.Visible = false;
            }
            else
            {
                lblGefunden.Visible = false;
            }
            return newQueryRo;
        }
    }
}
