private async Task <string> fetchPatientId(string user)
        {
            String      s      = String.Format("MATCH (a:patient) WHERE a.username='******' RETURN id(a)", user);
            queryObject query1 = new queryObject(s);

            string[] results = new string[10];
            // TODO: Authorisation check

            string output = await query1.cypherPOST(sharedClient);

            Debug.WriteLine(output);
            query1.parseFromData(output, new string[] { "data" }, out results, true);
            return(results[0]);
        }
        private async Task <string[]> downloadPrivateKeys(string user, string id)
        {
            String      s      = String.Format("MATCH (a:doctor) WHERE a.username='******' RETURN a", user);
            queryObject query1 = new queryObject(s);

            string[] results = new string[10];
            // TODO: Authorisation check

            string output = await query1.cypherPOST(sharedClient);

            Debug.WriteLine(output);
            query1.parseFromData(output, new string[] { "privateKey", String.Format("keyfor{0}", id) }, out results);
            return(results);
        }
        private async Task <string[]> saltHashPrivate(string user)
        {
            String      s      = String.Format("MATCH(a:doctor) WHERE a.username = \"{0}\" RETURN a", user);
            queryObject query1 = new queryObject(s);

            string[] results = new string[10];
            if (authorised)
            {
                string output = await query1.cypherPOST(client);

                query1.parseFromData(output, new string[] { "salt", "password", "privateKey" }, out results);
                Debug.WriteLine(output);
                return(results);
            }
            return(results);
        }
        async Task <List <MeasurementPoint> > fetchMeasurements()
        {
            string      preQuery    = String.Format("MATCH(a:patient) -[ :belongs]->(b:measurement) WHERE a.username = \"{0}\" RETURN b", userBeingTreated);
            queryObject uploadQuery = new queryObject(preQuery);
            string      output      = await uploadQuery.cypherPOST(sharedClient);

            Debug.WriteLine(output);
            // Parsing
            JsonObject value = JsonObject.Parse(output);
            IJsonValue j, k, m, l;

            value.TryGetValue("data", out j);
            JsonArray value2 = j.GetArray();
            // In case the search does not return any results, this array will be empty
            List <MeasurementPoint> measurementList = new List <MeasurementPoint> {
            };

            if (value2.Count != 0) // Given there are results
            {
                for (int i = 0; i < value2.Count; i++)
                {
                    JsonArray  value3  = value2[i].GetArray();
                    JsonObject object2 = value3[0].GetObject();
                    object2.TryGetValue("data", out k);
                    JsonObject object3 = k.GetObject();
                    object3.TryGetValue("date", out m);
                    string   measurementDate = m.GetString();
                    string   fmt             = "dd/MM/yyyy HH:mm:ss"; // Format of the date
                    DateTime mesDateTime     = DateTime.ParseExact(measurementDate, fmt, null);
                    object3.TryGetValue("value", out l);
                    string           measurementValue   = l.GetString();
                    MeasurementPoint currentMeasurement = new MeasurementPoint {
                        date = mesDateTime, value = Convert.ToDouble(measurementValue)
                    };
                    measurementList.Add(currentMeasurement);
                }
            }
            // Sort
            measurementList.OrderBy(mes => mes.date);
            return(measurementList);
        }
        private async void button4_Click(object sender, RoutedEventArgs e)
        {
            // Upload voltammetry data which is in textbox
            Debug.WriteLine(textBox4.Text);
            // textBlock2_Copy.Text = String.Format("You are currently treating {0}.", patientDialog.patientToTreat)
            // Commented out for not uploading
            DateTime localDate = DateTime.Now;
            string   dateNow   = localDate.ToString(new CultureInfo("en-GB")); // display date in DATE-TIME Format

            Debug.WriteLine(dateNow);

            string preQuery = "MATCH (b:patient) WHERE b.username = \"";

            preQuery += userBeingTreated;     // replace with username
            preQuery += "\" CREATE(b)-[: belongs]->(a: measurement{ type:\"histamine\", value: \"";
            preQuery += textBox4.Text;
            preQuery += "\", date: \"";
            preQuery += dateNow;
            preQuery += "\"";
            preQuery += "}) RETURN b";
            queryObject uploadQuery = new queryObject(preQuery);

            string output = await uploadQuery.cypherPOST(sharedClient);
        }
        private async void LoginLoad()
        {
            // Login screen
            ContentDialog1 signInDialog = new ContentDialog1();

            Debug.WriteLine("login");
            await signInDialog.ShowAsync();

            Debug.WriteLine("login shows");
            // Because it is statically allocated all the instances of content dialog share it, thus it only makes sense to access the property from the type itself
            var client = ContentDialog1.client;

            sharedClient = client;
            // Patient list request
            //Debug.WriteLine(signInDialog.username);
            string preQuery = String.Format("MATCH (a:doctor)-[: treat]->(b:patient) WHERE a.username = \"{0}\"  RETURN b", signInDialog.username);
            // Preparing a JSON query
            queryObject query1 = new queryObject(preQuery);
            string      output = await query1.cypherPOST(client);

            // Deserialization of data
            JsonObject value = JsonObject.Parse(output);
            IJsonValue j, k, m;

            value.TryGetValue("data", out j);
            JsonArray value2 = j.GetArray();
            // In case the search does not return any results, this array will be empty
            List <PatientSheet> patientList = new List <PatientSheet> {
            };                     // Meaning it will be empty if zero found

            if (value2.Count != 0) // If there are patients
            {
                for (int i = 0; i < value2.Count; i++)
                {
                    JsonArray  value3  = value2[i].GetArray();
                    JsonObject object2 = value3[0].GetObject();
                    object2.TryGetValue("data", out k);
                    JsonObject object3 = k.GetObject();
                    // Switching it to name
                    object3.TryGetValue("username", out m);
                    string username = m.GetString();
                    string id       = await fetchPatientId(username); // patients user

                    Debug.WriteLine(id);
                    string[] keys = await downloadPrivateKeys(signInDialog.username, id); // doctors user

                    Debug.WriteLine(keys[0]);
                    string decryptedDoctorPrivate = keyGenerator.Decrypt(keys[0], signInDialog.password);
                    Debug.WriteLine(keys[1]);
                    // Patient's private key is decrypted. It can be decrypted by doctor's private key, but only in pieces.
                    // Partition string by _
                    string[] splitKeys   = keys[1].Split('_');
                    string   combinedKey = "";
                    foreach (string splittedKey in splitKeys)
                    {
                        if (splittedKey.Length > 0)
                        {
                            string decryptedKeyPart;
                            Debug.Write("encrypted:");
                            Debug.WriteLine(splittedKey);
                            keyGenerator.AsymmetricDecrypt(decryptedDoctorPrivate, splittedKey, out decryptedKeyPart);
                            Debug.Write("decrypted:");
                            Debug.WriteLine(decryptedKeyPart);
                            combinedKey += decryptedKeyPart;
                        }
                    }
                    Debug.WriteLine(combinedKey);
                    // Combined key is patient's private key, with those we can decrypt the other relevant data
                    string dob;
                    object3.TryGetValue("dob", out m);
                    keyGenerator.AsymmetricDecrypt(combinedKey, m.GetString(), out dob);
                    Debug.WriteLine(dob);
                    string name;
                    object3.TryGetValue("name", out m);
                    keyGenerator.AsymmetricDecrypt(combinedKey, m.GetString(), out name);
                    Debug.WriteLine(name);
                    PatientSheet patientItem = new PatientSheet {
                        patientName = name, patientDOB = dob, patientUser = username
                    };
                    patientList.Add(patientItem);
                }
            }
            ContentDialog2 patientDialog = new ContentDialog2(signInDialog.username);

            patientDialog.patientList = patientList; // You could make patientlist private if you do an initaliser for it
            await patientDialog.ShowAsync();

            commandBar.Text       = String.Format("Currently treating {0}.", patientDialog.patientDisplayName);
            this.userBeingTreated = patientDialog.patientToTreat;
            this.doctor           = signInDialog.username;
            // When everything is done, load the patient measurement data into the plot
            List <MeasurementPoint> mes = await fetchMeasurements();

            Debug.Write("mes:");
            Debug.WriteLine(mes.Count);
            if (mes.Count != 0)
            {
                updatePatientPlot(mes);
            }
        }