private void restore()
 {
     CreatorGroup           = "";
     NameGroup              = "";
     UrlStream              = "";
     MqttConnection.IsGroup = false;
     MqttConnection.MqttDisconnect();
     MqttConnection.setKeyEndToEnd("");
     MqttConnection.setUser("");
     MqttConnection.setAioKey("");
     MqttConnection.IamAdmin        = false;
     LogInContainer.IsEnabled       = true;
     ProgressCircle.IsIndeterminate = false;
     ShowSnackBar("The room does not exist or disabled!");
 }
        public MainWindow()
        {
            InitializeComponent();
            MqttConnection.setKeyEndToEnd("");

            VerifyConnectionDataFile(MqttConnection.Key_EndToEnd, Directory.GetCurrentDirectory() + "\\Data\\AdaDataConnection.dt", 1);
            VerifyConnectionDataFile(MqttConnection.Key_EndToEnd, Directory.GetCurrentDirectory() + "\\Data\\SolaceDataConnection.dt", 2);

            HideSnackBar.Tick    += HideSnackBar_Tick;
            HideSnackBar.Interval = new TimeSpan(0, 0, 0, 2, 0);

            Restore.Tick    += Restore_Tick;
            Restore.Interval = new TimeSpan(0, 0, 0, 3, 0);

            WebClient webClient = new WebClient();

            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadVersionTxt);
            webClient.DownloadStringAsync(new Uri("http://epnbxbkyd4kxzppgghiyviro.altervista.org/StreamParty/version.txt"));
        }
        private async void PasteKey_Click(object sender, RoutedEventArgs e)
        {
            string key_group_enc = Clipboard.GetText();
            string key_group_dec = StringCipher.Decrypt(key_group_enc, "");
            //MessageBox.Show(key_group_dec);
            var var_key_group_dec = key_group_dec.Split('|');

            HttpRequest req = new HttpRequest();

            try
            {
                MqttConnection.IsGroup = true;
                MqttConnection.setChatRoomNameKey(var_key_group_dec[0]);
                MqttConnection.setUser(var_key_group_dec[1]);
                MqttConnection.setAioKey(var_key_group_dec[2]);
                MqttConnection.setKeyEndToEnd(var_key_group_dec[3]);
                CreatorGroup            = var_key_group_dec[4];
                UrlStream               = var_key_group_dec[5];
                MqttConnection.IamAdmin = !bool.Parse(var_key_group_dec[6]);
                MqttConnection.setMqttServer(var_key_group_dec[7]);
                MqttConnection.setMqttPort(var_key_group_dec[8]);
            }
            catch (Exception)
            {
                ShowSnackBar("Invalid Key Room");
                return;
            }
            ProgressCircle.IsIndeterminate = true;
            LogInContainer.IsEnabled       = false;


            if (MqttConnection.MqttServer.Contains("adafruit"))
            {
                AdaServer_Click(sender, e);
                if (NickName.Text.Length < 25)
                {
                    if (string.IsNullOrWhiteSpace(NickName.Text))
                    {
                        ShowSnackBar("Please insert a NickName");
                        ProgressCircle.IsIndeterminate = false;
                        LogInContainer.IsEnabled       = true;
                        return;
                    }
                    MqttConnection.setNickname(NickName.Text);

                    if (await Task.Run(() => MqttConnection.MqttTryToConnect()))
                    {
                        string data = await req.getJsonData("https://io.adafruit.com/api/v2/" + MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey + "?X-AIO-Key=" + MqttConnection.AioKey);

                        if (data != "")
                        {
                            FeedsJson.FeedProprety feedProprety = JsonConvert.DeserializeObject <FeedsJson.FeedProprety>(data);
                            if (feedProprety.enabled)
                            {
                                NameGroup          = feedProprety.name;
                                UsernameTxt.Text   = "";
                                AioKeyTxt.Password = "";
                                MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
                                MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                                MqttConnection.IsSolace = false;
                                MqttConnection.IsGroup  = true;
                                Restore.Start();
                            }
                            else
                            {
                                restore();
                            }
                        }
                        else
                        {
                            restore();
                        }
                    }
                    else
                    {
                        ShowSnackBar("Error to connect!");
                        LogInContainer.IsEnabled       = true;
                        ProgressCircle.IsIndeterminate = false;
                    }
                }
                else
                {
                    ShowSnackBar("Max 25 char for Nickname");
                    LogInContainer.IsEnabled       = true;
                    ProgressCircle.IsIndeterminate = false;
                }
            }
            else if (MqttConnection.MqttServer.Contains("messaging.solace.cloud"))
            {
                SolaceServer_Click(sender, e);
                if (SolaceNickName.Text.Length < 25)
                {
                    if (string.IsNullOrWhiteSpace(SolaceNickName.Text))
                    {
                        ShowSnackBar("Please insert a NickName");
                        ProgressCircle.IsIndeterminate = false;
                        LogInContainer.IsEnabled       = true;
                        return;
                    }
                    MqttConnection.setNickname(SolaceNickName.Text);

                    if (await Task.Run(() => MqttConnection.MqttTryToConnect()))
                    {
                        MqttConnection.setChatRoomNameKey("SolaceRoom");
                        MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
                        MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                        MqttConnection.MqttUnsecurePublish(MqttConnection.UserName + "/clients", "{\"status\":\"connected\",\"at\":\"2020-10-14T00:23:28.481Z\",\"client\":{\"id\":\"dzbpiydeyvrngch_" + MqttConnection.NickName + "\",\"ip\":\"78.74.65.35\"}}");
                        MqttConnection.IsSolace = true;
                        MqttConnection.IsGroup  = true;
                        Restore.Start();
                    }
                    else
                    {
                        ShowSnackBar("Error to connect!");
                        LogInContainer.IsEnabled       = true;
                        ProgressCircle.IsIndeterminate = false;
                    }
                }
                else
                {
                    ShowSnackBar("Max 25 char for Nickname");
                    LogInContainer.IsEnabled       = true;
                    ProgressCircle.IsIndeterminate = false;
                }
            }
            else
            {
                ShowSnackBar("Invalid Server!");
                LogInContainer.IsEnabled       = true;
                ProgressCircle.IsIndeterminate = false;
            }
        }
        private async void GenerateBtn_Click(object sender, RoutedEventArgs e)
        {
            if (NameRoom.Text != "" && !string.IsNullOrWhiteSpace(UsernameTxt.Text) && !string.IsNullOrWhiteSpace(AioKeyTxt.Password) && !string.IsNullOrWhiteSpace(Key_EndToEnd.Text) && !Key_EndToEnd.Text.Contains("GENERATE"))
            {
                HttpRequest req = new HttpRequest();

                string KeyRoomDec = req.getCorretName(NameRoom.Text) + "|" + UsernameTxt.Text + "|" + AioKeyTxt.Password + "|" + Key_EndToEnd.Text + "|" + MqttConnection.NickName + "|" + UrlTxt.Text + "|" + CheckControll.IsChecked + "|" + MqttConnection.MqttServer + "|" + MqttConnection.MqttPort.ToString();
                string KeyRoomEnc = StringCipher.Encrypt(KeyRoomDec, "");

                if (CheckCurrentData.IsChecked == true)
                {
                    if (getNumFeed(req.getAllFeeds()) != 10)
                    {
                        MqttConnection.MqttUnsubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                        if (req.createFeed(NameRoom.Text, DescriptionTxt.Text, Key_EndToEnd.Text, UrlTxt.Text, CheckControll.IsChecked.ToString()))
                        {
                            MqttConnection.setKeyEndToEnd(Key_EndToEnd.Text);
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                            MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_ROOMGROUP_" + NameRoom.Text + "_" + UrlTxt.Text);
                            Clipboard.SetText(KeyRoomEnc);
                            MessageBox.Show("Room created!!!" + '\n' + '\n' + "Don't worry, I copy the access key to the group, share it with your friends, you just have to paste and they just have to copy it and click on \"I have a key\"");
                            Close();
                        }
                        else
                        {
                            MqttConnection.setKeyEndToEnd("");
                            MqttConnection.setChatRoomNameKey("chat");
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                            ShowSnackBar("Error creating room!");
                        }
                    }
                    else
                    {
                        ShowSnackBar("Max number of group created, delete one!");
                    }
                }
                else
                {
                    string tempUser   = MqttConnection.UserName;
                    string tempAioKey = MqttConnection.AioKey;
                    MqttConnection.MqttDisconnect();
                    MqttConnection.setUser(UsernameTxt.Text);
                    MqttConnection.setAioKey(AioKeyTxt.Password);
                    if (!(await MqttConnection.MqttFastConnect()))
                    {
                        MqttConnection.setUser(tempUser);
                        MqttConnection.setAioKey(tempAioKey);
                        ShowSnackBar("Erron to connect with new data!");
                        UsernameTxt.Focus();
                        return;
                    }

                    if (getNumFeed(req.getAllFeeds()) != 10)
                    {
                        if (req.createFeed(NameRoom.Text, DescriptionTxt.Text, Key_EndToEnd.Text, UrlTxt.Text, CheckControll.IsChecked.ToString()))
                        {
                            MqttConnection.setKeyEndToEnd(Key_EndToEnd.Text);
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/clients");
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                            MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_ROOMGROUP_" + NameRoom.Text + "_" + UrlTxt.Text);
                            Clipboard.SetText(KeyRoomEnc);
                            MessageBox.Show("Room created!!!" + '\n' + '\n' + "Don't worry, I copy the access key to the group, share it with your friends, you just have to paste and they just have to copy it and click on \"I have a key\"");
                            Close();
                        }
                        else
                        {
                            MqttConnection.setKeyEndToEnd("");
                            MqttConnection.setChatRoomNameKey("chat");
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/clients");
                            MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                            ShowSnackBar("Error creating room!");
                        }
                    }
                    else
                    {
                        ShowSnackBar("Max number of group created, delete one!");
                    }
                }
            }
            else
            {
                ShowSnackBar("Complete the box please");
            }
        }