//Methods
        private async void getChatroom()
        {
           
            try
            {

                if (VindRitChatVM.ID == null)
                {
                    string json = await Localdata.read("chatroom.json");
                    var definition = new { ID = 0, UserID=0 };
                    var data = JsonConvert.DeserializeAnonymousType(json, definition);
                    if (data.ID == -1)
                    {
                        //geen chatroom
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                        {

                            Type type = null;
                            if (MainViewVM.USER.IsBob == true)
                            {
                                type = typeof(VindRitBob);
                            }
                            else
                            {
                                type = typeof(VindRit);
                            }
                            Frame rootFrame = MainViewVM.MainFrame as Frame;
                            rootFrame.Navigate(type);
                        });
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

                    }
                    else
                    {
                        VindRitChatVM.ID = data.ID;
                        MainViewVM.ChatRoom = new ChatRoom() { ID = VindRitChatVM.ID.Value };

                      
                        GetChatComments();

                     
                    }
                }
                else
                {
                    MainViewVM.ChatRoom = new ChatRoom() { ID = VindRitChatVM.ID.Value };

                   
                    GetChatComments();


                }
                   

            }
            catch (Exception ex)
            {

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                {
                    Type type = null;
                    if (MainViewVM.USER.IsBob == true)
                    {
                        type = typeof(VindRitBob);
                    }
                    else
                    {
                        type = typeof(VindRit);
                    }
                    Frame rootFrame = MainViewVM.MainFrame as Frame;
                    rootFrame.Navigate(type);

                });
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            }

           



            



        }
        private async void Clear()
        {
            try
            {
                var definition = new { ID = -1, UserID = -1 };
                var data = JsonConvert.SerializeObject(definition);
                var data2 = JsonConvert.SerializeObject(new Trip() { ID = -1 });


                bool ok_chatroom = Task.FromResult<bool>(await Localdata.save("chatroom.json", data)).Result;
                bool ok_trip = Task.FromResult<bool>(await Localdata.save("trip.json", data2)).Result;
            }
                catch (Exception ex)
            {
                var error = ex.Message;
            }

        }
        private async void OpenChatroom(int chatroomID)
        {
            var definition = new { ID = chatroomID, UserID = MainViewVM.USER.ID };
            var data = JsonConvert.SerializeObject(definition);
            bool ok_chatroom = Task.FromResult<bool>(await Localdata.save("chatroom.json", data)).Result;

            if (ok_chatroom == true)
            {
                //#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                //                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                //                {
                //                    Frame rootFrame = MainViewVM.MainFrame as Frame;
                //                    rootFrame.Navigate(typeof(VindRitChat),true);
                //                });
                //#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            }


        }
        private async Task<Boolean> Login_task(string email, string pass)
        {
            this.Error = "";
            this.Loading = true;
            this.EnableLogin = false;
            RaiseAll();
            Tests();

            if (this.Online==true)
            {
                Response res = await LoginRepository.Login(email, md5.Create(pass));
                if (res.Success == true)
                {
                   

                    try
                    {
                        User user = await UserRepository.GetUser();
                        MainViewVM.USER = user;

                        string jsonUser = await Localdata.read("user.json");
                        var definitionMail = new { Email = "", Password = "" };
                        if (jsonUser != null)
                        {
                            var dataUser = JsonConvert.DeserializeAnonymousType(jsonUser, definitionMail);
                            if (user.ID != MainViewVM.USER.ID)
                            {
                                Clear();
                            }
                        }


                        var json = JsonConvert.SerializeObject(new { Email = email, Password = md5.Create(pass) });
                        await Localdata.save("user.json", json);

 
                        string jsonChat = await Localdata.read("chatroom.json");
                        string jsonTrip = await Localdata.read("trip.json");
                        var definition = new { ID = 0, UserID = 0 };

                        if (jsonChat != null && jsonTrip != null)
                        {
                            var dataChat = JsonConvert.DeserializeAnonymousType(jsonChat, definition);
                            var dataTrip = JsonConvert.DeserializeObject<Trip>(jsonTrip);
                            if (dataChat == null | dataTrip == null)
                            {
                                Clear();
                            }
                            else if (MainViewVM.USER.IsBob == false && dataChat.UserID != MainViewVM.USER.ID)
                            {
                                Clear();

                            }
                            else if (MainViewVM.USER.IsBob == false && dataTrip.Users_ID != MainViewVM.USER.ID)
                            {
                                Clear();


                            }
                            else if (MainViewVM.USER.IsBob == true && dataTrip.Bobs_ID != MainViewVM.USER.Bobs_ID)
                            {
                                Clear();
                            }
                            else
                            {
                                // Clear();
                                MainViewVM.CurrentTrip = await TripRepository.GetCurrentTrip();

                                if (MainViewVM.CurrentTrip != null)
                                {
                                    VindRitChatVM.ID = dataChat.ID;
                                    if (dataTrip.ID != null && MainViewVM.CurrentTrip.ID == dataTrip.ID)
                                    {
                                        Messenger.Default.Send<NavigateTo>(new NavigateTo()
                                        {
                                            Name = "trip_location:reload"
                                        });
                                    }
                                }
                                else
                                {
                                    Clear();
                                }

                            }
                        }
                        MainViewVM.socket = IO.Socket(URL.SOCKET);
                        MainViewVM.socket.Connect();

                        Messenger.Default.Send<GoToPage>(new GoToPage()
                        {
                            Name = "MainView"
                        });

                    }
                    catch (Exception ex)
                    {

                        var error = ex.Message;
                    }
                    

                    this.Loading = false;
                    this.EnableLogin = true;
                    RaiseAll();

                }
                else
                {
                    this.Loading = false;
                    this.EnableLogin = true;
                    RaiseAll();

                    switch (res.Error)
                    {
                        case "Invalid Login":
                            this.Error = "Gegeven email en wachtwoord komen niet overeen of bestaan niet.";
                            break;
                        case "Server Offline":
                            this.Error = "De server is offline";
                            task = Login_task(this.Email, this.Pass);
                            break;
                        case "Connectie error":
                            this.Error = "Connectie error";
                            task = Login_task(this.Email, this.Pass);
                            break;
                        default:
                            this.Error = "Connectie error";
                           
                            break;
                    }

                    RaiseAll();



                }

                return res.Success;
            }
            else
            {

                this.Loading = false;
                this.EnableLogin = true;
                RaiseAll();

                return false;
            }
            
        }
        private async void TripDone()
        {
            var definition = new { ID = -1, UserID = -1 };
            var data = JsonConvert.SerializeObject(definition);
            var data2 = JsonConvert.SerializeObject(new Trip() { ID = -1 });


            bool ok_chatroom = Task.FromResult<bool>(await Localdata.save("chatroom.json", data)).Result;
            bool ok_trip = Task.FromResult<bool>(await Localdata.save("trip.json", data2)).Result;


            //alles op null zetten, van start
            VindRitFilterVM.SelectedDestination = new Users_Destinations();
            MainViewVM.ChatRoom = null;
            VindRitChatVM.ID = null;
            VindRitVM.SelectedParty = null;
            VindRitVM.SelectedBob = null;
            VindRitVM.SelectedUser = null;
            VindRitVM.BobAccepted = false;
            VindRitVM.StatusID = 0;

            MainViewVM.CurrentTrip = null;

            Toast.TileClear();



            Frame rootFrame = MainViewVM.MainFrame as Frame;

            if (MainViewVM.USER.IsBob == true)
            {

                Messenger.Default.Send<NavigateTo>(new NavigateTo()
                {
                    Name = "loaded",
                    View = typeof(VindRitBob)

                });
            }
            else
            {
                Messenger.Default.Send<NavigateTo>(new NavigateTo()
                {
                    Name = "loaded",
                    View = typeof(VindRit)
                });
            }


        }