/* Rename group*/
 private void flybtn_RenameGroup_Click(object sender, RoutedEventArgs e)
 {
     RenameGroup_YesConfirmation.IsEnabled = false;
     var item = list_group.SelectedItem as Group;
     oldGroup = item;
     tbox_RenameGroup.Text = item.Name;
     tbox_RenameGroup.Focus(FocusState.Keyboard);
     tbox_RenameGroup.SelectionStart = 0;
     tbox_RenameGroup.SelectionLength = tbox_RenameGroup.Text.Length;
     tbox_Validate_RenameGroup.Text = string.Empty;
     tbox_Validate_RenameGroup.Visibility = Visibility.Collapsed;
     //tbox_RenameGroup.BorderBrush = new SolidColorBrush(Colors.Black);
 }
        private async void list_group_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //list_group.SelectionChanged -= list_group_SelectionChanged;
            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            allowAction = false;
            if (list_group.SelectedItem != null)
                selectedGroup = (Group)list_group.SelectedItem;
            layout_loading.Visibility = Visibility.Visible;
            //await Task.Delay(50);

            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                if (!Rename.IsEnabled)
                {
                    Rename.IsEnabled = true;
                }
                if (!AddContact.IsEnabled)
                {
                    AddContact.IsEnabled = true;
                }
                if (!RemoveContact.IsEnabled)
                {
                    RemoveContact.IsEnabled = true;
                }
                if (list_group.SelectedItem != null)
                {
                    var group = list_group.SelectedItem as Group;
                    selectedGroupId = group.Id;
                    listContacts = MyUtils.GetContactsViewInGroup(Page_LocalContacts.ListContactViews, string.Empty, selectedGroupId);
                    //ResetListviewContact(listContacts.Count);
                    if (listContacts.Count > 0)
                    {
                        listview_contacts_headers.IsEnabled = true;
                    }
                    else
                    {
                        var headers = InitListButton();
                        SetDefaultStyle(headers);
                        sortContactButton = null;
                        listview_contacts_headers.IsEnabled = false;
                    }

                    //listview_contacts.ItemsSource = listContacts;
                    SortContactInGroupMode();
                }
                else
                {
                    btn_rename.IsEnabled = false;
                    btn_add_contact.IsEnabled = false;
                    btn_remove_contact.IsEnabled = false;
                }

                allowAction = true;
                layout_loading.Visibility = Visibility.Collapsed;
                //list_group.SelectionChanged += list_group_SelectionChanged;
            });
        }
 private void list_group_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     // Get corresponding contacts
     if (list_group.SelectedItem != null)
     {
         var group = list_group.SelectedItem as Group;
         selectedGroup = group;
         //SetDefaultStyle();
         LoadContactsByGroupName(selectedGroup.Name);
         UpdateGroupsButton(true);
     }
 }
        private async void btn_delete_pr_group_Click(object sender, RoutedEventArgs e)
        {
            var msg = UC_AddressBook.ResourcesStringLoader.GetString("DIALOG-Message-ask_delete_selected_group");
           
            var result = await Modals.UIConfirmPopup.ShowAsyncConfirmPopup(msg);
            if (result == PopupResult.Yes)
            {
                //var gname = list_group_delete.SelectedItems;
                var gname = list_group_delete.Items.Where(l=>((GroupView)l).IsChecked == true).ToList();

                if (gname != null)
                {
                    foreach (Group g in gname)
                    {
                        var name = g.Name;
                        if (listAddGroupName.Contains(name))
                        {
                            listAddGroupName.Remove(name);
                        }
                        if (listGroupName.Contains(name))
                        {
                            listGroupName.Remove(name);
                        }

                        ProcessListContact(name);

                        if (selectedGroup != null)
                        {
                            if (listGroupName.FirstOrDefault(l => l.ToString().Equals(selectedGroup.Name)) == null)
                                selectedGroup = null;
                        }
                    }
                    // Reload list group delete
                    LoadGroupListDelete();
                }

                MainGrid.Visibility = Visibility.Visible;
                stk_group_mode.Visibility = Visibility.Visible;
                grd_delete_group.Visibility = Visibility.Collapsed;
                stk_delete_group.Visibility = Visibility.Collapsed;
                //UpdateGroupsButton(false);
                isReload = true;
                isResumeSelectContacts = true;
            }
        }
        private async void cb_list_printer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (allowPrinterAction)
            {
                var index = cb_list_printer.SelectedIndex;
                if (index >= 0)
                {
                    if (selectedPrinterIndex >= 0 && index != selectedPrinterIndex)
                    {
                        var msg = UC_AddressBook.ResourcesStringLoader.GetString("DIALOG-Message-ask_change_printer");
                        if (UC_AddressBook.CurrentPrinterContact != null )
                        {
                            var ask = await Modals.UIConfirmPopup.ShowAsyncConfirmPopup(msg);
                            if (ask == PopupResult.No)
                            {
                                allowPrinterAction = false;
                                cb_list_printer.SelectedIndex = selectedPrinterIndex;
                                allowPrinterAction = true;
                                return;
                            }

                            if (printerError != null)
                            {
                                grid_search.Visibility = Visibility.Collapsed;
                                stk_contact_mode.Visibility = Visibility.Collapsed;
                                grid_contact_mode.Visibility = Visibility.Collapsed;
                                btn_send_printer.IsEnabled = false;
                            }
                            else
                            {
                                grid_search.Visibility = Visibility.Visible;
                                stk_contact_mode.Visibility = Visibility.Visible;
                                grid_contact_mode.Visibility = Visibility.Visible;
                                btn_send_printer.IsEnabled = true;
                            }

                        }
                        else
                        {
                            grid_search.Visibility = Visibility.Visible;
                            stk_contact_mode.Visibility = Visibility.Visible;
                            grid_contact_mode.Visibility = Visibility.Visible;
                            btn_send_printer.IsEnabled = true;
                            
                        }
                    }
                    selectedPrinterIndex = index;
                    //if (index >= 0 && !isUpdate)
                    if (index >= 0)
                    {
                        var printer = listPrinter[index];
                        ReloadPrinterContact(printer);
                        viewMode = ViewMode.MODE_CONTACT;
                    }
                    if (grid_search.Visibility == Visibility.Collapsed)
                    {
                        grid_search.Visibility = Visibility.Visible;
                    }
                    UpdateViewBaseOnMode(viewMode);

                    selectedGroup = null;

                    btn_send_printer.IsEnabled = true;
                    UC_AddressBook.Instance.UpdateCountContactPrinter(listContact_EF.Count());
                    UC_AddressBook.Instance.UpdateCountContactItemMenu();
                }
            }
        }
        private async void DoRefreshListView()
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                listview_contacts.ItemsSource = null;
                listview_contacts.ItemsSource = listContact_EF.OrderByDescending(l => l.Id);
                if (listContact_EF.Count != 0)
                {
                    printer_listview_contacts_header.IsEnabled = true;
                    chkAllNoHeader.Visibility = Visibility.Visible;
                    UC_AddressBook.Instance.UpdateCountContactPrinter(listContact_EF.Count());
                    UC_AddressBook.Instance.UpdateCountContactItemMenu();
                }
                else
                {
                    printer_listview_contacts_header.IsEnabled = false;
                    chkAllNoHeader.Visibility = Visibility.Collapsed;
                }

                if (list_group.SelectedItem != null)
                {
                    var group = list_group.SelectedItem as Group;
                    selectedGroup = group;
                    LoadContactsByGroupName(selectedGroup.Name);
                    UpdateGroupsButton(true);
                }

                if (printerError != null)
                {
                    string msg = UC_AddressBook.ResourcesStringLoader.GetString("connect_printer_fail");
                    var error = string.Format(msg, printerError.PrinterIP) + "\n" + printerError.EM.Message;
                    Modals.UIConfirmPopup.ShowAsyncInfoPopup(error);
                    grid_search.Visibility = Visibility.Collapsed;
                    stk_contact_mode.Visibility = Visibility.Collapsed;
                    grid_contact_mode.Visibility = Visibility.Collapsed;
                    btn_send_printer.IsEnabled = false;
                }
                else
                {
                    grid_search.Visibility = Visibility.Visible;
                    stk_contact_mode.Visibility = Visibility.Visible;
                    grid_contact_mode.Visibility = Visibility.Visible;
                    btn_send_printer.IsEnabled = true;
                }

                UpdateGUIWhenStopingProcess();
            });
        }