private async void BtnKey_Click(object sender, RoutedEventArgs e)
        {
            HttpRequest req             = new HttpRequest();
            var         KeyRoomSelected = selectRoom.Name.Split('_');

            FeedsJson.FeedProprety feedProprety = await getDataJson(KeyRoomSelected[2]);

            if (MqttConnection.ChatRoomNameKey == feedProprety.key)
            {
                var    valuedata = feedProprety.license.Split('*');
                string NewKey    = downloadData.DownloadString("https://passwordsgenerator.net/calc.php?Length=50&Symbols=0&Lowercase=1&Uppercase=1&Numbers=1&Nosimilar=1&Last=1");
                NewKey = NewKey.Substring(0, 50);
                if (await req.UpdateFeed(feedProprety.name, feedProprety.key, feedProprety.description, NewKey, valuedata[1], valuedata[2]))
                {
                    MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_KEY_" + NewKey);
                    DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnKey);
                    ShowSnackBar("Key update successfully!");
                }
                else
                {
                    DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnKey);
                    ShowSnackBar("Error updating Key!");
                }
            }
            else
            {
                DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnKey);
                ShowSnackBar("At first, you must be joined to the room!");
            }
        }
        private void insertRoom(Grid MainGridRooms, FeedsJson.FeedProprety dataJson)
        {
            TextBlock RoomName = textBlockRoom(dataJson.name, TextWrapping.NoWrap, FontStyles.Normal, (Style)FindResource("MaterialDesignHeadline6TextBlock"), 15, VerticalAlignment.Center, HorizontalAlignment.Left, "#303F9F");

            dataJson.created_at.ToLocalTime();
            TextBlock DateGroupCreation = textBlockRoom(dataJson.created_at.Hour + ":" + dataJson.created_at.Minute + "  " + dataJson.created_at.ToShortDateString(), TextWrapping.NoWrap, FontStyles.Normal, (Style)FindResource("MaterialDesignBody2TextBlock"), 11, VerticalAlignment.Center, HorizontalAlignment.Right, "#212121");
            string    EncData           = dataJson.last_value;

            string[] KeyDec;
            string   DecData = "";

            try
            {
                KeyDec  = dataJson.license.Split('*');
                DecData = StringCipher.Decrypt(EncData, KeyDec[0]);
            }
            catch (Exception)
            {
                DecData = "ErrorUser_Bad Data / Key End-To-End";
            }
            var       message             = DecData.Split('_');
            TextBlock UserTypeLastMessage = textBlockRoom(message[0] + ":", TextWrapping.NoWrap, FontStyles.Normal, (Style)FindResource("MaterialDesignSubtitle2TextBlock"), 12, VerticalAlignment.Bottom, HorizontalAlignment.Left, "#212121");
            TextBlock LastMessage         = textBlockRoom(message[1], TextWrapping.NoWrap, FontStyles.Italic, (Style)FindResource("MaterialDesignBody2TextBlock"), 11, VerticalAlignment.Bottom, HorizontalAlignment.Left, "#616161");
            TextBlock DateLastMessage     = textBlockRoom(dataJson.updated_at.Hour + ":" + dataJson.updated_at.Minute, TextWrapping.NoWrap, FontStyles.Normal, (Style)FindResource("MaterialDesignBody2TextBlock"), 11, VerticalAlignment.Bottom, HorizontalAlignment.Right, "#616161");

            Grid   contenentData = gridRoom(RoomName, DateGroupCreation, UserTypeLastMessage, LastMessage, DateLastMessage);
            Button FinalData     = finalRoomCard(contenentData, dataJson);

            RowDefinition rowDef = new RowDefinition();

            rowDef.Height = new GridLength(70);
            Grid.SetRow(FinalData, MainGridRooms.RowDefinitions.Count);
            MainGridRooms.RowDefinitions.Add(rowDef);
            MainGridRooms.Children.Add(FinalData);
        }
        private async Task <FeedsJson.FeedProprety> getDataJson(string feed)
        {
            HttpRequest req      = new HttpRequest();
            string      feedData = await req.getJsonData("https://io.adafruit.com/api/v2/" + MqttConnection.UserName + "/feeds/" + feed + "?X-AIO-Key=" + MqttConnection.AioKey);

            FeedsJson.FeedProprety dataJson = JsonConvert.DeserializeObject <FeedsJson.FeedProprety>(feedData);
            //Clipboard.SetText(feedData);
            return(dataJson);
        }
        private async void BtnShare_Click(object sender, RoutedEventArgs e)
        {
            var KeyRoomSelected = selectRoom.Name.Split('_');

            FeedsJson.FeedProprety feedProprety = await getDataJson(KeyRoomSelected[2]);

            var    licenseValue = feedProprety.license.Split('*');
            string KeyRoomDec   = feedProprety.key + "|" + MqttConnection.UserName + "|" + MqttConnection.AioKey + "|" + licenseValue[0] + "|" + MqttConnection.NickName + "|" + licenseValue[1] + "|" + licenseValue[2] + "|" + MqttConnection.MqttServer + "|" + MqttConnection.MqttPort.ToString();
            string KeyRoomEnc   = StringCipher.Encrypt(KeyRoomDec, "");

            Clipboard.SetText(KeyRoomEnc);
            DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnShare);
            ShowSnackBar("Key room copied in ClipBoard!");
        }
        private Button finalRoomCard(Grid roomDataContainer, FeedsJson.FeedProprety dataJson)
        {
            Button FinalRoom = new Button();

            FinalRoom.Padding = new Thickness(8, 8, 8, 4);
            ShadowAssist.SetShadowDepth(FinalRoom, ShadowDepth.Depth2);
            ButtonAssist.SetCornerRadius(FinalRoom, new CornerRadius(1));
            RippleAssist.SetFeedback(FinalRoom, (SolidColorBrush)(new BrushConverter().ConvertFrom("#212121")));
            FinalRoom.BorderBrush         = Brushes.White;
            FinalRoom.Foreground          = (SolidColorBrush)(new BrushConverter().ConvertFrom("#212121"));
            FinalRoom.Background          = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFFFFF"));
            FinalRoom.Width               = 305;
            FinalRoom.Height              = 60;
            FinalRoom.VerticalAlignment   = VerticalAlignment.Center;
            FinalRoom.HorizontalAlignment = HorizontalAlignment.Center;
            FinalRoom.Content             = roomDataContainer;
            FinalRoom.Name   = "Room_" + Rooms.RowDefinitions.Count.ToString() + "_" + dataJson.id;
            FinalRoom.Click += new RoutedEventHandler(selectRoom_Click);
            return(FinalRoom);
        }
        public bool createFeed(string name, string description, string Key_EndToEnd, string Url, string control)
        {
            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://io.adafruit.com/api/v2/" + MqttConnection.UserName + "/feeds?X-AIO-Key=" + MqttConnection.AioKey + "&group_key=default");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";


                string key = getCorretName(name);
                if (key == "")
                {
                    return(false);
                }

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"description\":\"" + description + "\"," +
                                  "\"key\":\"" + key + "\"," +
                                  "\"license\":\"" + Key_EndToEnd + "*" + Url + "*" + control + "\"," +
                                  "\"name\":\"" + name + "\"}";
                    streamWriter.Write(json);
                }

                string datareceived = "";
                var    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    datareceived = streamReader.ReadToEnd();
                }
                FeedsJson.FeedProprety feedProprety = JsonConvert.DeserializeObject <FeedsJson.FeedProprety>(datareceived);
                MqttConnection.setChatRoomNameKey(feedProprety.key);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        private async void BtnTrash_Click(object sender, RoutedEventArgs e)
        {
            HttpRequest req             = new HttpRequest();
            var         KeyRoomSelected = selectRoom.Name.Split('_');

            FeedsJson.FeedProprety feedProprety = await getDataJson(KeyRoomSelected[2]);

            MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_CLOSEROOM_" + feedProprety.key);

            if (await req.DeleteFeed(feedProprety.key))
            {
                DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnKey);
                ShowSnackBar("Room delete successfully!");
                RoomProgressCircle.IsIndeterminate = true;
                refreshRoom("");
            }
            else
            {
                DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnKey);
                ShowSnackBar("Error deleting Key!");
            }
        }
        private async void BtnJoin_Click(object sender, RoutedEventArgs e)
        {
            var KeyRoomSelected = selectRoom.Name.Split('_');

            FeedsJson.FeedProprety feedProprety = await getDataJson(KeyRoomSelected[2]);

            if (MqttConnection.ChatRoomNameKey != feedProprety.key)
            {
                MqttConnection.MqttUnsubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                var valuedata = feedProprety.license.Split('*');
                MqttConnection.ChatRoomNameKey = feedProprety.key;
                MqttConnection.Key_EndToEnd    = valuedata[0];
                MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey);
                MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_JOINGROUP_" + feedProprety.name + "_" + valuedata[1]);
                DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnJoin);
                ShowSnackBar("Joined successfully!");
            }
            else
            {
                DrawerHost.CloseDrawerCommand.Execute(DrawerHostContent, BtnJoin);
                ShowSnackBar("You are already joined!");
            }
        }
        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;
            }
        }