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;

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

        public Fahrtkostenauswertung()
        {
            InitializeComponent();
            

        }

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

        private QueryRO CreateDataSource(QueryRO ro)
        {
            DateTime dt;
            if (DateTime.TryParse(ro.Rows[0].Field2.ToString(), out dt))
            {
                xrUberschriftMonat.Text = String.Format("{0:MMMM}", dt);
                xrUberschriftJahr.Text = String.Format("{0:yyyy}", dt);
            }

            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>();
            }
            
            QueryRO newQueryRo = new QueryRO();
            
            foreach (var e in empListe)
            {
                QueryRO.Row empRow = new QueryRO.Row();
                empRow.ChildRows1 = new List<QueryRO.Row>();
                newQueryRo.Rows.Add(empRow);

                empRow.Field1 = e.Person.FirstNameLastName;

                DateTimeSpan span = new DateTimeSpan();
                span.StartDateTime = dt;
                span.EndDateTime = dt.AddMonths(1).AddSeconds(-1);

                var records = DAOFactory.SearchDAO.FindEmployeeServiceRecords(e.Oid.Value, span, null, null);

                foreach (var sr in records.Where(s1 => s1.DistanceInMeter.HasValue && s1.DistanceInMeter.Value > 0).OrderBy(s => s.Start))
                {
                    QueryRO.Row srRow = new QueryRO.Row();
                    empRow.ChildRows1.Add(srRow);

                    if (sr.Start.Value.Second > 0)
                    {
                        srRow.Field1 = String.Format("{0:dd.MM.yyyy}", sr.Start);
                    }
                    else
                    {
                        srRow.Field1 = String.Format("{0:dd.MM.yyyy HH:mm}-{1:HH:mm}", sr.Start, sr.End);
                    }
                    srRow.Field2 = String.Format("{0}", sr.DistanceInMeter);
                    srRow.Field3 = sr.Notice;
                }
            }

           
            foreach (var ccr in newQueryRo.Rows)
            {
                ccr.ChildRows1.Sort((a, b) => a.Field1.ToString().CompareTo(b.Field1));
            }
            return newQueryRo;
        }

        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;
        }

        

    }

    internal class TotalSummary
    {
        internal decimal flmTotal;
        internal decimal amountTotal;
        internal int countTotal;
        internal decimal approvedMinFl;
        internal decimal approvedMinAls;
        internal decimal approvedMinOthers;
    }
}
