private static SessionBlock BuildNewSessionBlock(CurrentWeekend currentWeekend, SessionInfo currentSession, List <TelemetryEvent> currentSessionEvents, List <DriverEntry> globalDrivers, List <DriverEntry> localDrivers)
        {
            SessionBlock sb = new SessionBlock();

            sb.CurrentWeekend = currentWeekend;
            sb.SessionInfo    = currentSession;
            sb.Drivers.AddRange(globalDrivers);
            sb.Drivers.AddRange(localDrivers);
            sb.Events = currentSessionEvents;
            return(sb);
        }
        internal static SessionBlock[] AnalyseTelemetry(TelemetryEvent[] events)
        {
            List <SessionBlock> ret = new List <SessionBlock>();

            CurrentWeekend        currentWeekend       = null;
            SessionInfo           currentSession       = null;
            List <TelemetryEvent> currentSessionEvents = new List <TelemetryEvent>();
            List <DriverEntry>    globalDrivers        = new List <DriverEntry>();
            List <DriverEntry>    localDrivers         = new List <DriverEntry>();
            double lastSessionTime = 0;

            for (int f = 0; f < events.Length; f++)
            {
                TelemetryEvent te = events[f];

                if (te is DriverEntry)
                {
                    if (currentWeekend == null)
                    {
                        DriverEntry existing = globalDrivers.FirstOrDefault(i => (i.CarIdx == (te as DriverEntry).CarIdx));
                        if (existing != null)
                        {
                            if (AreDriverEntriesTheSame(existing, te as DriverEntry) == false)
                            {
                                throw new ApplicationException("Duplicit driver entry CarIdx.");
                            }
                        }
                        else
                        {
                            globalDrivers.Add(te as DriverEntry);
                        }
                    }
                    else
                    {
                        if (localDrivers.FirstOrDefault(i => (i.CarIdx == (te as DriverEntry).CarIdx)) != null)
                        {
                            throw new ApplicationException("Duplicit driver entry CarIdx.");
                        }
                        localDrivers.Add(te as DriverEntry);
                    }
                }
                else if (te is DriverWithdrawl)
                {
                }
                else if (te is CurrentWeekend)
                {
                    if (currentWeekend != null)
                    {
                        SessionBlock sb = BuildNewSessionBlock(
                            currentWeekend, currentSession, currentSessionEvents,
                            globalDrivers, localDrivers);
                        ret.Add(sb);

                        currentWeekend = null;
                        localDrivers   = new List <DriverEntry>();
                    }
                    CurrentWeekend x = te as CurrentWeekend;
                    if (x.AtTrack == false)
                    {
                        currentWeekend = null;
                    }
                    else
                    {
                        currentWeekend = x;
                    }
                }
                else if (te is SessionInfo)
                {
                    //if (currentWeekend == null && currentSession == null) continue;

                    SessionInfo x = te as SessionInfo;
                    //bool isNewSession = (currentSession == null
                    //  || IsNewSession(x));
                    bool isNewSession = (currentSession == null ||
                                         x.CurrentTime <= lastSessionTime);

                    if (isNewSession)
                    {
                        if (currentSession != null && currentWeekend != null)
                        {
                            SessionBlock sb = BuildNewSessionBlock(
                                currentWeekend, currentSession, currentSessionEvents,
                                globalDrivers, localDrivers);
                            ret.Add(sb);
                        }
                        currentSession       = x;
                        currentSessionEvents = new List <TelemetryEvent>();
                    }
                    else
                    {
                        currentSessionEvents.Add(te);
                    }

                    lastSessionTime = x.CurrentTime;
                }
                else
                {
                    if (currentSession != null)
                    {
                        currentSessionEvents.Add(te);
                    }
                }
            }

            return(ret.ToArray());
        }