private void OnRoadMapReceived(object o, Manager.EventArgs.RoadMapReceivedEventArgs e)
        {
            if (e.Error)
            {
                Debug.WriteLine("Aucun rendez-vous n'existe pour le jour sélectionné");
                MessageBox.Show("Aucun rendez-vous n'existe pour le jour sélectionné");
                return;
            }
            roadmap = e.RoadMap;
            map.SetView(e.RoadMap.RouteResult.Summary.BoundingRectangle);
            var strokeThickness = e.RoadMap.Trips.Count * 2 + 1;
            foreach (var portion in e.RoadMap.Trips)
            {
                Random rand = new Random();
                byte r = (byte)rand.Next(0, 255);
                byte g = (byte)rand.Next(0, 255);
                byte b = (byte)rand.Next(0, 255);
                MapPolyline polyline = new MapPolyline();
                polyline.StrokeThickness = strokeThickness;
                polyline.Opacity = 1;
                polyline.Locations = new LocationCollection();
                foreach (var point in portion.RoutePath)
                    polyline.Locations.Add(point);
                map.Children.Add(polyline);
                polyline.Stroke = new SolidColorBrush(Color.FromArgb(255, 27, 161, 226));
                if (portion.IsTripFail)
                {
                    polyline.StrokeThickness += 2;
                    polyline.Stroke = new SolidColorBrush(Colors.Red);
                }
                if (portion.End.Subject == "End")
                    polyline.Stroke = new SolidColorBrush(Colors.Black);

                strokeThickness -= 2;
            }

            foreach (var rdv in e.RoadMap.Meetings)
            {
                if (rdv.IsLocationFail)
                    continue;
                Pushpin p = new Pushpin();
                if (rdv.Subject == string.Empty)
                {
                    p.Content = "Start";
                }
                else
                {
                    p.Content = rdv.Subject;
                }
                p.Background = new SolidColorBrush(Colors.Black);
                p.Location = rdv.Location;
                map.Children.Add(p);
            }
            
        }
        public void GetRoadMapAsync(DateTime date, ReferenceMeeting startPosition, ReferenceMeeting endPosition)
        {
            IMeetingManager meetingManager;
            ITripManager tripManager;
            RoadMap roadMap = new RoadMap();
            meetingManager = new MeetingManager();
            tripManager = new TripManager();
            roadMap.Date = date;

            //Lorsque on reçoit tous les RDV géolocalisés
            meetingManager.AllMeetingsRetreived += (o, eventMeeting) =>
            {
                if (eventMeeting.Error)
                {
                    RoadMapReceived(this, new RoadMapReceivedEventArgs(null) { Error = true, MessageError = eventMeeting.MessageError});
                    meetingManager.AllMeetingsRetreived = null;
                    return;
                }

                startPosition.DateTime = eventMeeting.Meetings.FirstOrDefault().DateTime.Date;
                endPosition.DateTime = eventMeeting.Meetings.FirstOrDefault().DateTime.Date.AddDays(1);
                //Position de départ
                roadMap.Meetings.Add(startPosition);
                //Enregistrement des rendez-vous dans la roadmap
                roadMap.Meetings.AddRange(eventMeeting.Meetings);
                //Position d'arrivée
                roadMap.Meetings.Add(endPosition);

                //Lorsque on reçoit le chemin complet de la journée
                tripManager.TripReceived += (o2, eventTrip) =>
                {
                    if (eventTrip.Error)
                    {
                        RoadMapReceived(this, new RoadMapReceivedEventArgs(null) { Error = true, MessageError = "L'adresse de départ ou d'arrivée décrite dans les paramètres de l'application est incorrecte!" });
                        tripManager.TripReceived = null;
                        return;
                    }
                    roadMap.RouteResult = eventTrip.RouteResult;
                    roadMap.Trips.AddRange(eventTrip.Trips);
                    RoadMapReceived(this, new RoadMapReceivedEventArgs(roadMap)); 
                    tripManager.TripReceived = null;
                };
                if (roadMap.ValidMeetings.Count <= 0)
                {
                    RoadMapReceived(this, new RoadMapReceivedEventArgs(null) { Error = true });
                    return;
                }
                //Calcule du chemin complet de façon asynchrone
                tripManager.GetTripAsync(roadMap.ValidMeetings);
            };
            //Récupération et vérification des rendez-vous
            meetingManager.GetAllMeetingsAsync(date);
        }
 public RoadMapReceivedEventArgs(RoadMap roadMap)
 {
     RoadMap = roadMap;
     Error = false;
     MessageError = "";
 }
 public DateAndPositions(int id, RoadMap roadmap) 
     : this(id, roadmap.Date, roadmap.Meetings.FirstOrDefault(), roadmap.Meetings.LastOrDefault())
 {
     _roadmap = roadmap;
 }
        private void OnRoadMapReceived(object o, PlanMyWay.Lib.Manager.EventArgs.RoadMapReceivedEventArgs e)
        {
            try
            {
                if (e.Error)
                {
                    MessageBox.Show(e.MessageError);
                    indicator.IsIndeterminate = false;
                    indicator.IsVisible = false;
                    btn_refresh.IsEnabled = true;
                    btn_setting.IsEnabled = true;
                    btn_export.IsEnabled = true;
                    traitementEnd = true;
                    this.NavigationService.GoBack();
                    return;
                }

                myRoadM = e.RoadMap;
                map_Rdv.SetView(e.RoadMap.RouteResult.Summary.BoundingRectangle);
                var strokeThickness = e.RoadMap.Trips.Count * 2 + 1;
                int endRdv = 0;
                foreach (var portion in e.RoadMap.Trips)
                {
                    endRdv++;
                    //Random rand = new Random();
                    //byte r = (byte)rand.Next(0, 255);
                    //byte g = (byte)rand.Next(0, 255);
                    //byte b = (byte)rand.Next(0, 255);
                    MapPolyline polyline = new MapPolyline();
                    polyline.StrokeThickness = strokeThickness;
                    polyline.Opacity = 1;
                    polyline.Locations = new LocationCollection();
                    foreach (var point in portion.RoutePath)
                        polyline.Locations.Add(point);
                    polyline.Stroke = PivotRoadTrip.blue;
                    
                    if (portion.IsTripFail)
                    {
                        polyline.StrokeThickness += 2;
                        polyline.Stroke = PivotRoadTrip.red;
                    }
                    else if (endRdv == e.RoadMap.ValidMeetings.Count - 1)
                    {
                        polyline.Stroke = PivotRoadTrip.black;
                    }
                    strokeThickness -= 2;
                    map_Rdv.Children.Add(polyline);
                }

                //Création des points sur la carte correspondant au rendez-vous
                int counRdv = 0;
                foreach (var rdv in e.RoadMap.Meetings)
                {
                    
                    if (rdv.IsLocationFail)
                        continue;
                    Pushpin p = new Pushpin();
                    if (counRdv == 0 && rdv.GetType() == typeof(ReferenceMeeting))
                    {
                        p.Content = "Départ";
                    }
                    else if (rdv.GetType() == typeof(ReferenceMeeting))
                    {
                        p.Content = "Arrivée";
                    }
                    else
                    {
                        p.Content = "RDV" + (counRdv) + " : " + rdv.Subject;
                       
                    }

                    p.Background = new SolidColorBrush(Colors.Black);
                    p.Location = rdv.Location;
                    map_Rdv.Children.Add(p);
                    counRdv++;
                }
                if (e.RoadMap != null)
                {
                    m = e.RoadMap.ValidMeetings;
                    meetingLocationFail = e.RoadMap.LocationFailMeetings;
                    tripTimeFail = e.RoadMap.FailTrips;
                    myRoadM = e.RoadMap;
                }
                myRMM.RoadMapReceived = null;
                InitGrid();
                InitAnomlie();
                btn_refresh.IsEnabled = true;
                btn_setting.IsEnabled = true;
                btn_export.IsEnabled = true;
                traitementEnd = true;
            }
            catch(Exception exception)
            {
                MessageBox.Show("Erreur de recepetion de la map bing, Réesseyer");
                Console.WriteLine(exception.Message.ToString());
                traitementEnd = true;
            }
        }
        static private Total AddTableXLS(ref SpreadsheetDocument doc, RoadMap roadmap)
        {
            Total total = new Total();
            int countRows = doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows.Indexes.Count();
            doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows[countRows].Cells[0].SetValue(String.Empty);
            countRows = doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows.Indexes.Count();
            doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows[countRows].Cells[1].SetValue("Feuille de route du "+ roadmap.Date);

            Debug.WriteLine("tableau " + roadmap.Meetings.FirstOrDefault().DateTime.ToShortDateString());
            List<SharedStringDefinition> strList = new List<SharedStringDefinition>();
            strList.Add(doc.Workbook.SharedStrings.AddString("Départ"));
            strList.Add(doc.Workbook.SharedStrings.AddString("Arrivée"));
            strList.Add(doc.Workbook.SharedStrings.AddString("Distance(km)"));
            strList.Add(doc.Workbook.SharedStrings.AddString("Consomation(Litre)"));
            strList.Add(doc.Workbook.SharedStrings.AddString("Coût(€)"));

            countRows = doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows.Indexes.Count();
            Debug.WriteLine("countRows 1 : "+countRows);
            // Entêtes de colonne
            for (var i=0; i<strList.Count; i++)
            {
                doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows[countRows].Cells[i + 1].SetValue(strList[i]);
            }
            // 1 ligne par trip
            foreach (var trip in roadmap.Trips)
            {
                countRows = doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows.Indexes.Count();
                doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[1].SetValue(trip.Start.City);
                doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[2].SetValue(trip.End.City);
                //Consommmation d'essence en fonction de la conso moyenne du véhicule
                int distance = (int)Math.Round(trip.Distance, 0);
                Cell distanceCell =  doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[3];
                Cell ConsumptionCell = doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[4];
                distanceCell.SetValue(distance);
                //double consumption = distance * conso / 100;
                ConsumptionCell.Formula = distanceCell.CellName + "*" + consoDefinition.CellName + "/100";
                //Prix du trajet en fonction de la consommation pendant le trajet et le prix du carburant
                //double cost = consumption * gasCost;
                doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[5].Formula = ConsumptionCell.CellName + "*" + gasCostDefinition.CellName;
            }
            //Taille des colonnes
            doc.Workbook.Sheets[0].Sheet.AddColumnSizeDefinition(0, 1, 30);
            doc.Workbook.Sheets[0].Sheet.AddColumnSizeDefinition(1, 5, 22);
            //Mise à jour du prochain numéro de ligne libre
            countRows = doc.Workbook.Sheets.FirstOrDefault().Sheet.Rows.Indexes.Count();
            Debug.WriteLine("countRows 3 : " + countRows);
            //Récupération du numéro de ligne de début et de fin du tableau courrant
            int idRowTableStart, idRowTableEnd;
            idRowTableStart = countRows - roadmap.Trips.Count;
            idRowTableEnd = countRows - 1;
            Debug.WriteLine("idRowTableStart : " + idRowTableStart);
            Debug.WriteLine("idRowTableEnd : " + idRowTableEnd);
            Row rowTop = doc.Workbook.Sheets[0].Sheet.Rows[idRowTableStart];
            Row rowBottom = doc.Workbook.Sheets[0].Sheet.Rows[idRowTableEnd];
            //// Total
            doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[1].SetValue("Total");
            doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[3].Formula = "SUM(" + rowTop.Cells[3].CellName + ":" + rowBottom.Cells[3].CellName + ")";
            doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[4].Formula = "SUM(" + rowTop.Cells[4].CellName + ":" + rowBottom.Cells[4].CellName + ")";
            doc.Workbook.Sheets[0].Sheet.Rows[countRows].Cells[5].Formula = "SUM(" + rowTop.Cells[5].CellName + ":" + rowBottom.Cells[5].CellName + ")";
            //Incrémentation du total
            total.Distance += "SUM(" + rowTop.Cells[3].CellName + ":" + rowBottom.Cells[3].CellName + ")";
            total.Conso += "SUM(" + rowTop.Cells[4].CellName + ":" + rowBottom.Cells[4].CellName + ")";
            total.CoutTotal += "SUM(" + rowTop.Cells[5].CellName + ":" + rowBottom.Cells[5].CellName + ")";


            Debug.WriteLine(roadmap.Meetings.FirstOrDefault().DateTime.ToShortDateString());
            //Ajout d'un tableau
            Row rowCol = doc.Workbook.Sheets[0].Sheet.Rows[idRowTableStart - 1];
            Row rowTotal = doc.Workbook.Sheets[0].Sheet.Rows[idRowTableEnd + 1];
            Debug.WriteLine("rowCol : " + (idRowTableStart - 1));
            Debug.WriteLine("rowTotal : " + (idRowTableEnd + 1));
            TablePart table = doc.Workbook.Sheets[0].Sheet.AddTable(
                "tableau 1",
                "rapport du " + roadmap.Meetings.FirstOrDefault().DateTime.ToShortDateString(),
                rowCol.Cells[1],
                rowTotal.Cells[5]
                );
            //Précision explicite des colonnes du tableau
            for (var i = 0; i < strList.Count; i++)
            {
                table.TableColumns[i].Name = strList[i].String;
            }
            table.ShowTotalsRow = true;
            table.TableStyle.ShowColumnStripes = true;
            table.TableStyle.ShowRowStripes = true;
            return total;
        }
        private void OnRoadMapReceived(object o, PlanMyWay.Lib.Manager.EventArgs.RoadMapReceivedEventArgs e)
        {
            int counRdv = -1;
            int endRdv = 0;
            try
            {
                if (e.Error)
                {
                    Debug.WriteLine("Aucun rendez-vous n'existe pour le jour sélectionné");
                    MessageBox.Show("Aucun rendez-vous n'existe pour le jour sélectionné ou bien Vérifier les lieux de départ et d'arrivée dans l'application");
                    indicator.IsIndeterminate = false;
                    indicator.IsVisible = false;
                    btn_refresh.IsEnabled = true;
                    btn_setting.IsEnabled = true;
                    traitementEnd = true;
                    
                    return;
                }
                myRoadM = e.RoadMap;
                map_Rdv.SetView(e.RoadMap.RouteResult.Summary.BoundingRectangle);
                var strokeThickness = e.RoadMap.Trips.Count * 2 + 1;
                foreach (var portion in e.RoadMap.Trips)
                {
                    endRdv++;
                    Random rand = new Random();
                    byte r = (byte)rand.Next(0, 255);
                    byte g = (byte)rand.Next(0, 255);
                    byte b = (byte)rand.Next(0, 255);
                    MapPolyline polyline = new MapPolyline();
                    polyline.StrokeThickness = strokeThickness;
                    polyline.Opacity = 1;
                    polyline.Locations = new LocationCollection();
                    foreach (var point in portion.RoutePath)
                        polyline.Locations.Add(point);
                    map_Rdv.Children.Add(polyline);
                    polyline.Stroke = new SolidColorBrush(Color.FromArgb(255, 27, 161, 226));
                    if (portion.IsTripFail)
                    {
                        polyline.StrokeThickness += 2;
                        polyline.Stroke = new SolidColorBrush(Colors.Red);
                    }
                    if (endRdv == e.RoadMap.Trips.Count)
                        polyline.Stroke = new SolidColorBrush(Colors.Black);
                    strokeThickness -= 2;
                   
                }

                foreach (var rdv in e.RoadMap.Meetings)
                {
                    
                    if (rdv.IsLocationFail)
                        continue;
                    Pushpin p = new Pushpin();
                    if (counRdv == 1 && rdv.GetType() == typeof(ReferenceMeeting))
                    {
                        p.Content = "Départ";
                    }
                    else
                    {
                        counRdv++;
                        p.Content = "RDV" + (counRdv) + " : " + rdv.Subject;
                       
                    }
                    if (counRdv != 1 && rdv.GetType() == typeof(ReferenceMeeting))
                    {
                        p.Content = "Arrivée";
                    }

                    p.Background = new SolidColorBrush(Colors.Black);
                    p.Location = rdv.Location;
                    map_Rdv.Children.Add(p);
                }
                if (e.RoadMap != null)
                {
                    m = e.RoadMap.ValidMeetings;
                    meetingLocationFail = e.RoadMap.LocationFailMeetings;
                    tripTimeFail = e.RoadMap.FailTrips;
                    myRoadM = e.RoadMap;
                }
                InitGrid();
                InitAnomlie();
                btn_refresh.IsEnabled = true;
                btn_setting.IsEnabled = true;
                traitementEnd = true;
            }
            catch(Exception exception)
            {
                MessageBox.Show("Erreur de recepetion de la map bing, Réesseyer");
                Console.WriteLine(exception.Message.ToString());
                traitementEnd = true;
            }
        }