private async Task GetCurrentTrip()
        {
           

            if (MainViewVM.CurrentTrip == null ||MainViewVM.CurrentTrip.ID == 0)
            {
                try
                {
                    string json = await Localdata.read("trip.json");
                    if (json != null && json !="")
                    {
                        var data = JsonConvert.DeserializeObject<Trip>(json);
                        if (data.ID != -1)
                        {
                            this.Loading = true;
                            RaiseAll();

                            SetStatus(data.StatusID.Value);
                            MainViewVM.CurrentTrip = data;

                            if (data.StatusID.Value== 5)
                            {
                                Bobs_Parties bobs_parties = new Bobs_Parties()
                                {
                                    Bobs_ID = MainViewVM.CurrentTrip.Bobs_ID,
                                    Party_ID = MainViewVM.CurrentTrip.Party_ID,
                                    Trips_ID = MainViewVM.CurrentTrip.ID,
                                    Users_ID = MainViewVM.CurrentTrip.Users_ID

                                };

                                Libraries.Socket socketSendToUser = new Libraries.Socket()
                                {
                                    From = MainViewVM.USER.ID,
                                    To = MainViewVM.CurrentTrip.Users_ID,
                                    Status = true,
                                    Object = JsonConvert.SerializeObject(bobs_parties),
                                    Object2 = true
                                };
                                MainViewVM.socket.Emit("trip_DONE:send", JsonConvert.SerializeObject(socketSendToUser));
                            }
                           

                            Location location = await LocationService.GetCurrent();
                            Users_Destinations destination = Task.FromResult<Users_Destinations>(await DestinationRepository.GetDestinationById(MainViewVM.CurrentTrip.Destinations_ID)).Result;
                            Bob.All bob = Task.FromResult<Bob.All>(await BobsRepository.GetBobById(MainViewVM.CurrentTrip.Bobs_ID)).Result;
                            Party party = Task.FromResult<Party>(await PartyRepository.GetPartyById(MainViewVM.CurrentTrip.Party_ID)).Result;
                            VindRitVM.SelectedBob = bob;
                            VindRitVM.SelectedParty = party;

                            if (bobTypes != null)
                            {
                                VindRitFilterVM.SelectedBobsType = bobTypes.Where(r => r.ID == bob.Bob.BobsType_ID).First();
                            }
                            VindRitFilterVM.SelectedDestination = destination;

                            if (MainViewVM.CurrentTrip.StatusID.HasValue)
                            {
                                if (MainViewVM.CurrentTrip.StatusID == 2 || MainViewVM.CurrentTrip.StatusID == 8)
                                {
                                    ShowRoute((Location)VindRitVM.SelectedParty.Location, (Location)VindRitFilterVM.SelectedDestination.Location);
                                }
                                else
                                {
                                    ShowRoute(location, (Location)VindRitFilterVM.SelectedDestination.Location);
                                }
                            }

                            

                            ShowedOnParty = true;
                            this.EnableFind = false;
                            this.Loading = false;
                            RaiseAll();
                            return;
                        }
                    }

                    SetStatus(0);
                    ShowedOnParty = true;
                    this.EnableFind = true;



                }
                catch (Exception ex)
                {
                    SetStatus(0);
                    ShowedOnParty = true;
                    this.EnableFind = true;
                   
                }
            }
            else
            {
                this.Loading = true;
                RaiseAll();

                MainViewVM.CurrentTrip = Task.FromResult<Trip>(await TripRepository.GetCurrentTrip()).Result;

                if (MainViewVM.CurrentTrip.StatusID.Value == 5)
                {
                    Bobs_Parties bobs_parties = new Bobs_Parties()
                    {
                        Bobs_ID = MainViewVM.CurrentTrip.Bobs_ID,
                        Party_ID = MainViewVM.CurrentTrip.Party_ID,
                        Trips_ID = MainViewVM.CurrentTrip.ID,
                        Users_ID = MainViewVM.CurrentTrip.Users_ID

                    };

                    Libraries.Socket socketSendToUser = new Libraries.Socket()
                    {
                        From = MainViewVM.USER.ID,
                        To = MainViewVM.CurrentTrip.Users_ID,
                        Status = true,
                        Object = JsonConvert.SerializeObject(bobs_parties),
                        Object2 = true
                    };
                    MainViewVM.socket.Emit("trip_DONE:send", JsonConvert.SerializeObject(socketSendToUser));
                }

                Location location = await LocationService.GetCurrent();
                Users_Destinations destination = Task.FromResult<Users_Destinations>(await DestinationRepository.GetDestinationById(MainViewVM.CurrentTrip.Destinations_ID)).Result;
                Bob.All bob = Task.FromResult<Bob.All>(await BobsRepository.GetBobById(MainViewVM.CurrentTrip.Bobs_ID)).Result;
                Party party = Task.FromResult<Party>(await  PartyRepository.GetPartyById(MainViewVM.CurrentTrip.Party_ID)).Result;
                VindRitVM.SelectedBob = bob;
                VindRitVM.SelectedParty = party;

                if (bobTypes != null)
                {
                    VindRitFilterVM.SelectedBobsType = bobTypes.Where(r => r.ID == bob.Bob.BobsType_ID).First();
                }
                VindRitFilterVM.SelectedDestination = destination;


               

                ShowedOnParty = true;

                if (MainViewVM.CurrentTrip.StatusID.HasValue)
                {
                    if (MainViewVM.CurrentTrip.StatusID == 2 || MainViewVM.CurrentTrip.StatusID == 8)
                    {
                        ShowRoute((Location)VindRitVM.SelectedParty.Location, (Location)VindRitFilterVM.SelectedDestination.Location);
                    }
                    else
                    {
                        ShowRoute(location, (Location)VindRitFilterVM.SelectedDestination.Location);
                    }

                    SetStatus(MainViewVM.CurrentTrip.StatusID.Value);
                }

                this.EnableFind = false;
                this.Loading = false;
               

            }
            RaiseAll();




        }
        private async void Timer_Tick(object sender, object e)
        {
            Location location = await LocationService.GetCurrent();

            Toast.Tile("Party: " + this.GetSelectedParty.Name, "Bestemming: " + this.GetSelectedDestination.Name, "Status " + this.Status);


            if (location != null)
            {
                if (MainViewVM.CurrentTrip.StatusID == 2 || MainViewVM.CurrentTrip.StatusID == 8)
                {
                    ShowRoute((Location)VindRitVM.SelectedParty.Location, (Location)VindRitFilterVM.SelectedDestination.Location);
                }
                else
                {
                    ShowRoute(location, (Location)VindRitFilterVM.SelectedDestination.Location);
                }

                //checkhowfaraway
                Response farEnough = Task.FromResult<Response>(await TripRepository.Difference((Location)VindRitFilterVM.SelectedDestination.Location, location)).Result;

                if (farEnough.Success == true)
                {
                    //kleiner dan 1km
                    timer.Stop();

                    if (canShowDialog==true)
                    {
                        canShowDialog = false;

                        bool done = Task.FromResult<bool>(await OnDestination()).Result;
                        if (done == true)
                        {
                            Bob.All bob = Task.FromResult<Bob.All>(await BobsRepository.GetBobById(MainViewVM.CurrentTrip.Bobs_ID)).Result;

                            BobisDone("Trip is afgerond");
                            Bobs_Parties itemBobs = new Bobs_Parties()
                            {
                                Bobs_ID=MainViewVM.CurrentTrip.Bobs_ID,
                                Party_ID=MainViewVM.CurrentTrip.Party_ID,
                                Trips_ID=MainViewVM.CurrentTrip.ID,
                                Users_ID=MainViewVM.CurrentTrip.Users_ID
                            };

                            RatingDialog(itemBobs);
                        }
                        else
                        {
                            timer.Start();
                        }
                    }


                }



            }
        }
        public static async Task<Response> AddRating(Bobs_Parties item)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(URL.BASE);

                    var newObject = JsonConvert.SerializeObject(item);

                    HttpResponseMessage result = await client.PostAsync(URL.TRIPS_RATING, new StringContent(newObject, Encoding.UTF8, "application/json"));
                    string json = await result.Content.ReadAsStringAsync();
                    Response data = JsonConvert.DeserializeObject<Response>(json);

                    return data;
                }
            }
            catch (JsonException jex)
            {
                return new Response() { Error = "Parse Error: " + jex.ToString(), Success = false };

            }
            catch (Exception ex)
            {
                return new Response() { Error = ex.Message.ToString(), Success = false };

            }
        }
        private async void RatingDialog(Bobs_Parties item)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {

                var dialog = new ContentDialog()
                {
                    Title = "Rating",
                };

                // Setup Content
                var panel = new StackPanel();

                panel.Children.Add(new TextBlock
                {
                    Text = "Uw rit is ten einde, hoeveel rating wilt u deze bob geven? ",
                    TextWrapping = TextWrapping.Wrap,
                    Margin = new Thickness(0, 0, 0, 15)
                });

                List<string> items = new List<string>();
                for (int i = 1; i < 5; i++)
                {
                    items.Add(i.ToString());
                }
                var cb = new ComboBox
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch
                };
                cb.ItemsSource = items;

                var txt = new TextBox
                {
                    TextWrapping = TextWrapping.Wrap,
                    HorizontalContentAlignment = HorizontalAlignment.Stretch
                };



                panel.Children.Add(cb);
                panel.Children.Add(txt);
                dialog.Content = panel;

                // Add Buttons
                dialog.PrimaryButtonText = "Ok";
                dialog.PrimaryButtonClick += async delegate
                {
                    double rating;
                    string text = cb.SelectedValue.ToString();
                    string comment = txt.Text;

                    double.TryParse(text, out rating);


                    item.Rating = rating;

                    Response res = await TripRepository.AddRating(item);
                    var ok = res.Success;

                    if (ok == true || ok == false)
                    {
                        Libraries.Socket socketSendToUser = new Libraries.Socket()
                        {
                            To = MainViewVM.USER.ID,
                            Status = true,
                        };

                        MainViewVM.socket.Emit("trip_DONE:send", JsonConvert.SerializeObject(socketSendToUser));
                        
                        this.Map.MapElements.Clear();
                    }

                       
                 

                };



                // Show Dialog
                var result = await dialog.ShowAsync();
                if (result == ContentDialogResult.None)
                {

                }
            });
        }
        private async void BobisDone(string text)
        {
            this.Map.MapElements.Clear();
            ClearAllMapItems();

            Bobs_Parties bobs_parties = new Bobs_Parties()
            {
                Bobs_ID = MainViewVM.CurrentTrip.Bobs_ID,
                Party_ID = MainViewVM.CurrentTrip.Party_ID,
                Trips_ID = MainViewVM.CurrentTrip.ID,
                Users_ID = MainViewVM.CurrentTrip.Users_ID

            };


            Response active = Task.FromResult<Response>(await TripRepository.PutActive(MainViewVM.CurrentTrip.ID, false)).Result;

            if (active.Success == true)
            {

                Libraries.Socket socketSendToBob = new Libraries.Socket()
                {
                    From = MainViewVM.CurrentTrip.Users_ID,
                    To = MainViewVM.USER.ID,
                    Status = true,
                    Object = JsonConvert.SerializeObject(bobs_parties),
                    Object2 = false

                };
                Libraries.Socket socketSendToUser = new Libraries.Socket()
                {
                    From = MainViewVM.USER.ID,
                    To = MainViewVM.CurrentTrip.Users_ID,
                    Status = true,
                    Object = JsonConvert.SerializeObject(bobs_parties),
                    Object2 = true
                };

                await UserRepository.PostPoint();

                this.Map.MapElements.Clear();

                MainViewVM.socket.Emit("trip_DONE:send", JsonConvert.SerializeObject(socketSendToUser));
                MainViewVM.socket.Emit("trip_DONE:send", JsonConvert.SerializeObject(socketSendToBob));


                var dialog = new MessageDialog(text);
                dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
                dialog.DefaultCommandIndex = 0;
                var result = await dialog.ShowAsync();
                int id = int.Parse(result.Id.ToString());

                this.IsEnabledArrived = true;
                this.IsEnabledCancel = true;

                this.IsEnabledOffer = true;
                this.VisibleCancel = Visibility.Collapsed;
                this.VisibleOffer = Visibility.Collapsed;
                VindRitBobVM.Request -= 1;
                this.Loading = false;
                this.Status = null;
                SetStatus(0);




                canShowDialog = true;
                RaiseAll();


            }
        }