/// <summary>
        /// Print the contents of the tempList
        /// </summary>
        private void ListToDebugTxt()
        {
            TextConnector tc       = new TextConnector();
            string        fullPath = tc.FullFilePath("debug" + mStartTimeMarker.ToString() + "-" + mEndTimeMarket.ToString() + ".csv", "debug");

            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }
            using (TextWriter tw = new StreamWriter(fullPath, true)) //Append mode
            {
                foreach (SAMPLE item in mTempList)
                {
                    tw.Write(" -- SECS:" + item.SECS.ToString());
                    tw.Write(", KM:" + item.KM.ToString());
                    tw.Write(", KPH:" + item.KPH.ToString());
                    tw.Write(", LAT:" + item.LAT.ToString());
                    tw.Write(", LON:" + item.LON.ToString());
                    tw.WriteLine(", SLOPE:" + item.SLOPE.ToString());
                    tw.Flush();
                }
            }
        }
        //Video segment and ride segment are equal. Move all points from the old ride to the new one.
        public List <SAMPLE> MoveAllPointsToNewRide(int videoTime, int startTime, int endTime, ref int thisOldRideCnt,
                                                    ref int thisNewRideCnt, GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide)
        {
#if (DEBUG)
            TextConnector tc       = new TextConnector();
            string        fullPath = tc.FullFilePath("debug" + startTime.ToString() + "-" + endTime.ToString() + ".csv", "debug");

            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }
#endif
            testSegment.Clear();
            int startCnt = startTime;
            int endCnt   = endTime;

            if (thisNewRide.RIDE.SAMPLES.Count() == 0) // This is the first record so we can't subtract 1 from the counters
            {
                CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
#if (DEBUG)
                SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
#endif
                thisNewRideCnt += 1;
                thisOldRideCnt++;
                startCnt++;
            }
            else
            {   //Check if point exists in new route before adding it
                if (Math.Abs(thisNewRide.RIDE.SAMPLES[thisNewRideCnt - 1].KM - thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM) > 0.01)
                {
                    CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                    CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
#if (DEBUG)
                    SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
#endif
                    thisNewRideCnt += 1;
                    thisOldRideCnt++;
                    //pointsMoved++;
                }
                else // Based on distance point already exists
                {
                    startCnt++;
                    //pointsSkipped++;
                }
            }

            while (startCnt <= endCnt)
            {
                if (thisOldRideCnt < thisOldRide.RIDE.SAMPLES.Count - 2) // Guard aginst EOF
                {
                    CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                    CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
#if (DEBUG)
                    SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
#endif
                    thisNewRideCnt++;
                }
                startCnt++;
                thisOldRideCnt++;
            }
            return(testSegment);
        }
        public List <SAMPLE> RemovePointsFromNewRide(int videoTime, int startTime, int endTime, ref int thisOldRideCnt, ref int thisNewRideCnt, GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide)
        {
            TextConnector tc       = new TextConnector();
            string        fullPath = tc.FullFilePath("debug" + startTime.ToString() + "-" + endTime.ToString() + ".csv", "debug");

            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }

            testSegment.Clear();
            int pointsMoved = 0;
            //int pointsSkipped = 0;
            int pointsToRemove = -1;
            int startCnt       = startTime;
            int endCnt         = endTime;
            int segmentTime    = endTime - startTime + 1;

            videoTime++;  // Add 1 second to the video that is lost during subtraction.
            // If we only need to remove 1 point the above increment will make pointsToRemove == 0.
            pointsToRemove = Math.Abs(videoTime - segmentTime);
            if (pointsToRemove == 0)
            {
                pointsToRemove = 1;
            }

            int pointsInterval = CalculateRomovePointsInterval(pointsToRemove, segmentTime);

            if (thisNewRide.RIDE.SAMPLES.Count() == 0) // This is the first record so we can't subtract 1 from the counters
            {
                CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
                SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
                thisNewRideCnt += 1;
                thisOldRideCnt++;
                startCnt++;
            }
            else
            {   //Check if point exists in new route before adding it
                if (Math.Abs(thisNewRide.RIDE.SAMPLES[thisNewRideCnt - 1].KM - thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM) > 0.01)
                {
                    CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                    CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
                    SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
                    thisNewRideCnt += 1;
                    thisOldRideCnt++;
                    pointsMoved++;
                }
                else // Based on distance point already exists
                {
                    startCnt++;
                    //pointsSkipped++;
                }
            }

            // THIS NEEDS A METHOD ---Do we need an extra  drop?
            //int lastSample = -1;
            //if ((pointsInterval * pointsToRemove) == segmentTime) lastSample = segmentTime - 1;



            if ((pointsInterval * pointsToRemove) >= segmentTime - 1)
            {
                pointsInterval--;
            }
            int pointsLeftToRemove = pointsToRemove;


            // if the videoTimeSegment is > than (routeSegmentTime/2) we need to remove more points than we keep.
            //
            if (videoTime > (segmentTime / 2))
            {
                NonConsecutivePointsRemoveProcess(ref startCnt,
                                                  ref endCnt,
                                                  ref pointsInterval,
                                                  //ref lastSample,
                                                  ref pointsLeftToRemove,
                                                  ref thisOldRideCnt,
                                                  //ref pointsSkipped,
                                                  ref thisNewRideCnt,
                                                  ref pointsMoved,
                                                  ref thisOldRide,
                                                  ref thisNewRide,
                                                  ref fullPath);
            }
            else
            {
                ConsecutivePointsRemoveProcess(ref startCnt,
                                               ref endCnt,
                                               ref pointsInterval,
                                               //ref lastSample,
                                               ref pointsLeftToRemove,
                                               ref thisOldRideCnt,
                                               //ref pointsSkipped,
                                               ref thisNewRideCnt,
                                               ref pointsMoved,
                                               ref thisOldRide,
                                               ref thisNewRide,
                                               ref videoTime,
                                               ref fullPath);
            }

            // Get last point from old route
            CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
            CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
            SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
            thisNewRideCnt += 1;
            thisOldRideCnt++;


            //// Save temp list
            //TextConnector tc = new TextConnector();
            //string fullPath = tc.FullFilePath("debug" + startTime.ToString() + "-"  + endTime.ToString() + ".csv", "debug");
            //SaveToTxt(testSegment, fullPath);
            return(testSegment);
        }
        /// <summary>
        /// Add points to the ride for video sync.
        /// videoTime = The number of seconds the video needs to travel the segment
        /// rideTime = The # of SECS at the end of the segment
        /// startTime = The # of SECS at the start of the segment
        /// endTime = the # of SECS at the end of the segment
        /// </summary>
        /// <returns></returns>
        public List <SAMPLE> AddPointsToNewRide(int videoTime, int startTime, int endTime, ref int thisOldRideCnt, ref int thisNewRideCnt, GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide)
        {
            testSegment.Clear();

#if (DEBUG)
            TextConnector tc       = new TextConnector();
            string        fullPath = tc.FullFilePath("debug" + startTime.ToString() + "-" + endTime.ToString() + ".csv", "debug");

            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }
#endif

            int pointsAdded = 0;
            int pointsMoved = 0;
            // Init vars
            int pointsToAdd = -1;
            int startCnt    = 0;
            int endCnt      = 0;
            int segmentTime = 0;
            // If this is not the first record we need to add a second to the video time
            videoTime++;
            segmentTime = endTime - startTime + 1;
            endCnt      = segmentTime - 1; // Last point is added manually
            pointsToAdd = videoTime - segmentTime;

            //Calculate distance and avg speed for the full segment
            GeoLoc     startpoint        = new GeoLoc(thisOldRide.RIDE.SAMPLES[startTime].LAT, thisOldRide.RIDE.SAMPLES[startTime].LON);
            GeoLoc     endpoint          = new GeoLoc(thisOldRide.RIDE.SAMPLES[endTime].LAT, thisOldRide.RIDE.SAMPLES[endTime].LON);
            GeoLocMath geoLocMath        = new GeoLocMath(startpoint, endpoint);
            double     distanceTravelled = geoLocMath.CalcDistanceBetweenGeoLocations();

            if (thisNewRide.RIDE.SAMPLES.Count() == 0)
            {
                // This is the first record.
                // Move initial point from old route to new route
                CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
#if (DEBUG)
                SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
#endif
                thisNewRideCnt += 1;
                thisOldRideCnt++;
                startCnt++;
                pointsMoved++;
                endCnt--; // THIS
            }
            else
            {   //Check if point exists in new route before adding it
                if (Math.Abs(thisNewRide.RIDE.SAMPLES[thisNewRideCnt - 1].KM - thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM) > 0.01)
                {
                    // Move initial point from old route to new route
                    CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
                    CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
#if (DEBUG)
                    SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
#endif
                    thisNewRideCnt += 1;
                    thisOldRideCnt++;
                    pointsMoved++;
                }
                else
                {
                    // First point is there already, move to the next one and decriment the number of points to move
                    startCnt++;
                    endCnt--;
                }
            }

            // If the last sample to add equals the segment length, add it right before the end
            //int lastSample = -1;
            //if ((pointsInterval * pointsToAdd) == endCnt) lastSample = endCnt - 1;

            if (pointsToAdd > (segmentTime / 2) && pointsToAdd < segmentTime)
            {
                //ADD every other point starting with (_segmentTime - _pointsToAdd) / 2
                MoreThanHalfPointsAddProcess(ref startCnt,
                                             ref endCnt,
                                             //ref pointsInterval,
                                             //ref lastSample,
                                             ref pointsToAdd,
                                             ref pointsAdded,
                                             ref thisOldRideCnt,
                                             ref thisNewRideCnt,
                                             ref pointsMoved,
                                             ref segmentTime,
                                             ref thisOldRide,
                                             ref thisNewRide,
                                             ref fullPath);
            }
            else if (pointsToAdd > (segmentTime / 2) && pointsToAdd > segmentTime)
            {
                Debug.WriteLine(" New Method here ");
                MorePointsThanWeHaveAddProcess(ref startCnt,
                                               ref endCnt,
                                               //ref pointsInterval,
                                               //ref lastSample,
                                               ref pointsToAdd,
                                               ref pointsAdded,
                                               ref thisOldRideCnt,
                                               ref thisNewRideCnt,
                                               ref segmentTime,
                                               ref thisOldRide,
                                               ref thisNewRide,
                                               ref fullPath);
            }
            else
            {
                //Calculate the number of points to add and the interval for adding them
                int pointsInterval = CalculateAddPointsInterval(pointsToAdd, segmentTime, endCnt, videoTime);
                LessThanHalfPointsAddProcess(ref startCnt,
                                             ref endCnt,
                                             ref pointsInterval,
                                             //ref lastSample,
                                             ref pointsToAdd,
                                             ref pointsAdded,
                                             ref thisOldRideCnt,
                                             ref thisNewRideCnt,
                                             ref pointsMoved,
                                             ref thisOldRide,
                                             ref thisNewRide,
                                             ref fullPath);
            }

            // Move last point from old route to new route
            CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide);
            CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment);
            SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt);
            thisNewRideCnt += 1;
            thisOldRideCnt++;

            return(testSegment);
        }        // ***** addPointsToNewRide ENDS