private async void ReportunAuthorized_Click(object sender, RoutedEventArgs e)
        {
            clickingImages = false;
            ChatUserControl control = new ChatUserControl();

            control.setControlProperties(250, "SECURITY VIOLATION", "Unauthorized user was Reported successfully", "peach", null, false);
            ChatStack.Children.Add(control);

            // createsystemWarning("UNAUTHORIZED USER : REPORTED", 200, 200, "Unauthorized user was Reported successfully", false, false, "sdf", false, "sdf");
            await Task.Delay(200);

            myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
        }
        private async void ScrollTextBtnClick(object sender, RoutedEventArgs e)
        {
            //IEnumerable<PersonGroup> personGroups = await FaceServiceHelper.ListPersonGroupsAsync(SettingsHelper.Instance.WorkspaceKey);
            //await FaceServiceHelper.TrainPersonGroupAsync(personGroups.FirstOrDefault().PersonGroupId);


            ChatUserControl control = new ChatUserControl();

            control.setControlProperties(250, "SUCCESS", "A new User has been Authorized", "green", null, false);
            ChatStack.Children.Add(control);

            //createsystemWarning("AUTHORIZATION : SUCCESS", 200, 200, "A new User has been Authorized", false, false, "sdf", false, "dark");
            await Task.Delay(200);

            myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
        }
        private async void AddPersonBtnclick(object sender, RoutedEventArgs e)
        {
            IEnumerable <PersonGroup> personGroups = await FaceServiceHelper.ListPersonGroupsAsync(SettingsHelper.Instance.WorkspaceKey);

            clickingImages = false;
            //Person[] personsInGroup = await FaceServiceHelper.GetPersonsAsync(personGroups.FirstOrDefault().PersonGroupId);

            //CreatePersonResult result = await FaceServiceHelper.CreatePersonAsync(personGroups.FirstOrDefault().PersonGroupId, "Saeed");

            //foreach (var item in imagelist)
            //{
            //    AddPersistedFaceResult addResult;
            //    addResult = await FaceServiceHelper.AddPersonFaceAsync(
            //            personGroups.FirstOrDefault().PersonGroupId,
            //            result.PersonId,
            //             imageUrl: item,
            //             userData: item,
            //             targetFace: null);

            //}



            await FaceServiceHelper.TrainPersonGroupAsync(personGroups.FirstOrDefault().PersonGroupId);

            UnauthorizedGrid.Visibility = Visibility.Collapsed;



            ChatUserControl control = new ChatUserControl();

            control.setControlProperties(250, "AUTHORIZATION : SUCCESS", "A new User has been Authorized", "green", null, false);
            ChatStack.Children.Add(control);


            //createsystemWarning("", 200, 200, "", false, false, "sdf", false, "sdf");
            await Task.Delay(200);

            myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
        }
        private async Task ProcessCameraCapture(ImageAnalyzer e)
        {
            try
            {
                if (e == null)
                {
                    this.lastDetectedFaceSample         = null;
                    this.lastIdentifiedPersonSample     = null;
                    this.lastSimilarPersistedFaceSample = null;
                    this.lastEmotionSample = null;
                    this.debugText.Text    = "";

                    this.isProcessingPhoto = false;
                    return;
                }
                await Task.WhenAll(e.DetectFacesAsync(detectFaceAttributes: true));

                //await Task.WhenAll(e.DetectFacesAsync(detectFaceAttributes: true), e.AnalyzeImageAsync(detectCelebrities: false));


                DateTime start = DateTime.Now;
                await Task.Run(() => SavePicturesToServer(e));

                // Compute Emotion, Age and Gender
                //await Task.WhenAll(e.DetectEmotionAsync(), e.DetectFacesAsync(detectFaceAttributes: true));



                //check tags



                //foreach (var item in e.AnalysisResult.Description.Tags)
                //{
                //    if (item.Contains("fire"))
                //    {
                //        isfiredetected = true;
                //    }

                //}

                if (Constants.isFireDetected)
                {
                    if (isfiredetected)
                    {
                        var stream = new InMemoryRandomAccessStream();
                        await stream.WriteAsync(e.Data.AsBuffer());

                        stream.Seek(0);
                        await fireimage.SetSourceAsync(stream);

                        await Task.Delay(300);

                        ChatUserControl control = new ChatUserControl();
                        control.setControlProperties(500, "HAZARD", "Fire detected in sector 7", "peach", fireimage, true);
                        ChatStack.Children.Add(control);
                        // createsystemWarning("SYSTEM : ALERT", 400, 400, "Fire Detected in the meeting room", false, false, "sdf", true, "dark");

                        await Task.Delay(200);

                        myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
                        isfiredetected = false;
                    }
                }



                if (e.DetectedFaces == null || !e.DetectedFaces.Any())
                {
                    this.lastDetectedFaceSample = null;
                    LastPersistedCount          = 0;
                }
                else
                {
                    this.lastDetectedFaceSample = e.DetectedFaces;
                    //analysis image can be taken here and done
                    if (!ishelmet)
                    {
                        ishelmet = true;

                        ChatUserControl control = new ChatUserControl();
                        control.setControlProperties(280, "SAFETY VIOLATION", "Personnel with no Helmet", "blue", null, false);
                        ChatStack.Children.Add(control);

                        await Task.Delay(200);

                        myScroll.ChangeView(0, myScroll.ExtentHeight, 1);


                        var data = await getData("http://gitex2017backend.azurewebsites.net/api/ServiceRequest/AnswerQuestion?Answer=Personnel with no Helmet&Session=" + SessionID);
                    }



                    if (LastUpdatedDateTime.AddMinutes(4) < DateTime.Now)
                    {
                        ishelmet = true;
                    }
                }

                // Compute Face Identification and Unique Face Ids
                await Task.WhenAll(e.IdentifyFacesAsync(), e.FindSimilarPersistedFacesAsync());

                if (!e.IdentifiedPersons.Any())
                {
                    this.lastIdentifiedPersonSample = null;
                }
                else
                {
                    this.lastIdentifiedPersonSample = e.DetectedFaces.Select(f => new Tuple <Face, IdentifiedPerson>(f, e.IdentifiedPersons.FirstOrDefault(p => p.FaceId == f.FaceId)));
                }

                if (!e.SimilarFaceMatches.Any())
                {
                    this.lastSimilarPersistedFaceSample = null;
                }
                else
                {
                    this.lastSimilarPersistedFaceSample = e.SimilarFaceMatches;
                }

                int count = 0;
                unauth = true;
                if (lastIdentifiedPersonSample == null && e.DetectedFaces.Count() > 0)
                {
                    var croppedimage = await GetPrimaryFaceFromCameraCaptureAsync(e, e.DetectedFaces.FirstOrDefault().FaceRectangle);

                    if (croppedimage != null)
                    {
                        this.OnImageSearchCompleted(croppedimage);
                    }
                }
                else
                {
                    foreach (var item in lastIdentifiedPersonSample)
                    {
                        try
                        {
                            if (item.Item2 == null)
                            {
                                count++;
                                var croppedImage = e;

                                croppedImage = await GetPrimaryFaceFromCameraCaptureAsync(e, item.Item1.FaceRectangle);

                                if (croppedImage != null)
                                {
                                    this.OnImageSearchCompleted(croppedImage);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                if (!unauth)
                {
                    unauth = true;

                    ChatUserControl control = new ChatUserControl();
                    control.setControlProperties(200, "SECURITY VIOLATION", "Unauthorized personnel on Sector 7", "peach", null, false);
                    ChatStack.Children.Add(control);
                    myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
                }

                if (LastPersistedCount < count)
                {
                    // PopToast(string.Format("{0} Unauthorized faces found", count));
                }
                LastPersistedCount = count;
                //this.UpdateDemographics(e);

                this.debugText.Text    = string.Format("Latency: {0}ms", (int)(DateTime.Now - start).TotalMilliseconds);
                this.ErrorText.Text    = e.ErrorMessage;
                this.isProcessingPhoto = false;
            }
            catch (Exception ex)
            {
                this.isProcessingPhoto = false;
            }
        }
        private async void LoadingChats(object sender, object e)
        {
            try
            {
                //Calling the webservices
                CurrentTime.Text = DateTime.Now.ToString(@"HH:mm:ss");



                await Task.Run(() => GetMaterials());

                await Task.Run(() => getNewChats());

                if (messages != null && messages.Count > 0)
                {
                    ChatUserControl control = new ChatUserControl();
                    control.setControlProperties(350, "USER", messages.FirstOrDefault().Question, "green", null, false);
                    ChatStack.Children.Add(control);

                    await Task.Delay(200);

                    myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                //if any answer do it here
                if (PenQuestions != null && PenQuestions.Count > 0)
                {
                    switch (PenQuestions.FirstOrDefault().Type)
                    {
                    case "person":
                        var count = lastIdentifiedPersonSample.Count();
                        if (count > 0)
                        {
                            foreach (var item in lastIdentifiedPersonSample)
                            {
                                if (item.Item2 != null)
                                {
                                    if (item.Item2.Person.Name.ToLower().Contains("rami"))
                                    {
                                        answertext = "Yes,Rami is on site in sector 7";
                                    }
                                }
                            }
                            if (answertext == "")
                            {
                                answertext = "No, Rami is not currently on site in Sector 7";
                            }
                        }


                        break;

                    case "object":
                        break;

                    case "hammer":

                        if (Constants.materials.Count > 0)
                        {
                            foreach (var item in Constants.materials)
                            {
                                if (item.Confidence.ToLower().Contains("hammer"))
                                {
                                    ishammer = true;
                                }
                            }
                        }

                        if (ishammer)
                        {
                            answertext = "Yes, the Hammer is on stand in Sector 7";
                        }
                        else
                        {
                            answertext = "No, The Hammer is not detected in Sector 7";
                        }
                        break;

                    default:
                        answertext = "Sorry, I cannot detect right now";
                        PenQuestions.Clear();


                        break;
                    }
                }
                if (answertext != "")
                {
                    ChatUserControl control = new ChatUserControl();
                    control.setControlProperties(280, "USER RESPONSE", answertext, "blue", null, false);
                    ChatStack.Children.Add(control);

                    await Task.Delay(200);

                    myScroll.ChangeView(0, myScroll.ExtentHeight, 1);
                }
            }
            catch (Exception ex)
            {
            }
        }