/// <summary>
        /// This method is called after authentication is successfull.
        /// Implement functionality that is useful as initial server communication. 
        /// </summary>
        /// <param name="account"></param>
        async void PerformAuth2TestRequests(Account account)
        {
            Authorized = true;
            App.SuccessfulLoginAction();

            try
            {

                /*
                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests before looop");


                foreach (KeyValuePair<string, string> p in account.Properties)
                {
                    System.Diagnostics.Debug.WriteLine("Property: Key:" + p.Key + " Value:" + p.Value);
                }
                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests before looop");

                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests: Url:" + AuthProvider.ApiRequests);
                System.Diagnostics.Debug.WriteLine("Request Url:" + AuthProvider.ApiRequests);
                */
                Uri requestLocalToken = new Uri(AuthProvider.ApiRequests + account.Properties["access_token"]);
                System.Diagnostics.Debug.WriteLine("Requesting local token");
                System.Diagnostics.Debug.WriteLine("Using access_token: " + account.Properties["access_token"]);
                OAuth2Request request1 = new OAuth2Request("GET", requestLocalToken, null, account);
                //OAuth2Request request1 = new OAuth2Request("GET", requestLocalToken, null, null);

                IResponse response1 = await request1.GetResponseAsync();
                System.Diagnostics.Debug.WriteLine("After Response");


                Dictionary<string, string> responseDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response1.GetResponseText());


                string localToken = "";
                string username = "";

                if (response1.StatusCode == 200)
                {
                    System.Diagnostics.Debug.WriteLine("Response code from backend: 200");
                    localToken = responseDict["access_token"];
                    username = responseDict["userName"];
                    System.Diagnostics.Debug.WriteLine("username: "******"localToken: " + localToken);

                    StudentsController sc = new StudentsController();
                    DbStudent dbStudent = new DbStudent();
                    if (dbStudent.CheckIfStudentExist(username))
                    {
                        System.Diagnostics.Debug.WriteLine("Student did exist");
                        Student student = dbStudent.GetStudent(username);
                        student.accessToken = localToken;
                        dbStudent.UpdateStudent(student);
                        DevicesController dc = new DevicesController();
                        dc.UpdateServersDb();
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Student did not exist");
                        dbStudent.DeleteAllStudents();
                        Student student = new Student();
                        student.username = username;
                        student.accessToken = localToken;
                        student.receiveNotifications = true;
                        student.receiveJobNotifications = true;
                        student.receiveProjectNotifications = true;
                        dbStudent.InsertStudent(student);
                        DevicesController dc = new DevicesController();
                        DbDevice dbDevice = new DbDevice();
                        dbDevice.FixStudentForeignKey(username);
                        dc.UpdateServersDb();
                    }
                }
                /*
                string studentEndpoint = "http://kompetansetorgetserver1.azurewebsites.net/api/v1/students";
                Uri testAuthorize = new Uri(studentEndpoint);


                //authorization: bearer b2Dvqzi9Ux_FAjbBYat6PE-LgNGKL_HDBWbnJ3Fb9cwfjaE8NQdqcvC8jwSB5QJUIVRog_gQQPjaRI0DT7ahu7TEpqP28URtPr1LjgaV - liCqgIuTdSHW_NqD3qh - 5shVh - h7TCin7XNHq8GSkGg5qtOlcHeFPSZ4xMwMbw5_1rBfKYJr3w0_D5R9jk0hJPEfJldCTYcawatz7wVfbmz0qKHAkrKxZyaqum6IHJWdczWz5K26RCfZWMwEmK1uLN5

                var client = new HttpClient();
                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests before Setting AuthenticationHeaderValue");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", localToken);
                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests after Setting AuthenticationHeaderValue");
            System.Diagnostics.Debug.WriteLine(client.DefaultRequestHeaders.Authorization.Parameter);


                var response = await client.GetAsync(testAuthorize);


                System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests: StatusCode:" + response.StatusCode);
                                                 // + " ResponseUri:" + response.ResponseUri);
                //System.Diagnostics.Debug.WriteLine("PerformAuth2TestRequests: Headers:");


                foreach (KeyValuePair<string, string> h in response.Headers)
                {
                    System.Diagnostics.Debug.WriteLine("Header: Key:" + h.Key + " Value:" + h.Value);
                } 
                System.Diagnostics.Debug.WriteLine("Response(" + response.StatusCode);
                string jsonString = await response.Content.ReadAsStringAsync();
                System.Diagnostics.Debug.WriteLine(jsonString);
                */
                // TODO Implement relevant GET, PUT or POST Requests
                // Notifies the app that the login was successful and that its safe to shift page.
                Authorized = true;
                App.SuccessfulLoginAction();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception: PerformAuth2TestRequests: Message:" + ex.Message);
                foreach (KeyValuePair<string, string> p in account.Properties)
                {
                    System.Diagnostics.Debug.WriteLine("Key:" + p.Key + " Value:" + p.Value);
                }
            }

        }
        private async void TestPostStudentsStudyGroupToServer(object sender, EventArgs e)
        {
            List<string> studyGroups = new List<string>();
            string idrett = Hasher.Base64Encode("idrett");
            string datateknologi = Hasher.Base64Encode("datateknologi");
            string realfag = Hasher.Base64Encode("realfag");
            string samfunn = Hasher.Base64Encode("samfunnsfag");

            studyGroups.Add(idrett);
            studyGroups.Add(datateknologi);
            //studyGroups.Add(samfunnsfag);
            //studyGroups.Add(realfag);
            StudyGroup samfunnsfag = new StudyGroup();
            StudentsController sc = new StudentsController();
            bool success = await sc.PostStudentsStudyGroupToServer(studyGroups);

            System.Diagnostics.Debug.WriteLine(
                "TestPostStudentsStudyGroupToServer: PostStudentsStudyGroupToServer success: " + success);
        }
        public async Task PostToServer()
        {
            StudentsController sc = new StudentsController();
            await sc.PostStudentsStudyGroupToServer(GetCheckedStudyGroups());

            if (!Authenticater.Authorized)
            {
                GoToLogin();
            }
        }
        private async void TestGetStudyGroupStudent_OnClicked(object sender, EventArgs e)
        {
            StudentsController sc = new StudentsController();
            List<StudyGroup> list = await sc.GetStudentsStudyGroupFromServer();
            // checks if still Authorized 
            if (!Authenticater.Authorized)
            {
                System.Diagnostics.Debug.WriteLine("TestGetStudyGroupStudent inside If: Authorized:  " +
                                                   Authenticater.Authorized);
                GoToLogin();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("TestGetStudyGroupStudent inside Else: Authorized:  " +
                                                   Authenticater.Authorized);
                if (list == null)
                {
                    System.Diagnostics.Debug.WriteLine("Server communication failed");
                }

                System.Diagnostics.Debug.WriteLine("Studygroups.Count: " + list.Count);

                foreach (var sg in list)
                {
                    System.Diagnostics.Debug.WriteLine("id: " + sg.id);
                    System.Diagnostics.Debug.WriteLine("name: " + sg.name);
                }
            }
        }
        public async Task GetAllFilters()
        {

            DbStudyGroup sgc = new DbStudyGroup();
            StudentsController sc = new StudentsController();

            studyGroupsFilter = sgc.GetAllStudyGroups(); //set checked to false
            List<StudyGroup> checkedStudyGroups = await sc.GetStudentsStudyGroupFromServer(); // use these to check some to true

            System.Diagnostics.Debug.WriteLine("studyGroupsFilter.Count: " + studyGroupsFilter.Count); 
            foreach (var sg in studyGroupsFilter)
            {
                if (checkedStudyGroups.Contains(sg))
                {
                    sg.filterChecked = true;
                }

                else
                {
                     sg.filterChecked = false;
                }
                //studyGroupsFilter.Add(sg);
            }
            System.Diagnostics.Debug.WriteLine("Another studyGroupsFilter.Count: " + studyGroupsFilter.Count);
            foreach (var studyGroup in studyGroupsFilter)
            {
                studyDict.Add(studyGroup.name, studyGroup.id);
            }
        }