context: using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Telegram.Td.Api;
  using Template10.Common;
  using Unigram.Collections;
  using Unigram.Common;
  using Unigram.Core.Common;
  using Unigram.Services;
  using Windows.ApplicationModel.DataTransfer;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Navigation;
  
  namespace Unigram.ViewModels
  {
      public class ShareViewModel : TLViewModelBase
      {
          public ShareViewModel(IProtoService protoService, ICacheService cacheService, ISettingsService settingsService, IEventAggregator aggregator, ChatsViewModel dialogs)
              : base(protoService, cacheService, settingsService, aggregator)
          {
              Items = new MvxObservableCollection<Chat>();
              SelectedItems = new MvxObservableCollection<Chat>();
  
              SendCommand = new RelayCommand(SendExecute, () => SelectedItems?.Count > 0);
          }
  
          private SearchChatsCollection _search;
          public SearchChatsCollection Search
          {
              get
              {
                  return _search;
              }
              set
              {
                  Set(ref _search, value);
              }
          }
  
          private TopChatsCollection _topChats;
          public TopChatsCollection TopChats
          {
              get
              {
                  return _topChats;
              }
              set
              {
                  Set(ref _topChats, value);
              }
          }
  
          public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> state)
          {
              //if (mode == NavigationMode.New)
              //{
              //    _dialogs = null;
              //}
  
              var response = await ProtoService.SendAsync(new GetChats(long.MaxValue, 0, int.MaxValue));
              if (response is Telegram.Td.Api.Chats chats)
              {
                  var list = ProtoService.GetChats(chats.ChatIds);
                  Items.Clear();
  
                  if (_searchType != SearchChatsType.BasicAndSupergroups)
                  {
                      var myId = ProtoService.GetMyId();
                      var self = list.FirstOrDefault(x => x.Type is ChatTypePrivate privata && privata.UserId == myId);
                      if (self == null)
                      {
                          self = await ProtoService.SendAsync(new CreatePrivateChat(myId, false)) as Chat;
                      }
  
                      if (self != null)
                      {
                          list.Remove(self);
                          list.Insert(0, self);
                      }
                  }
  
                  foreach (var chat in list)
                  {
                      if (_searchType == SearchChatsType.BasicAndSupergroups)
                      {
                          if (chat.Type is ChatTypeBasicGroup basic)
                          {
                              var basicGroup = ProtoService.GetBasicGroup(basic.BasicGroupId);
                              if (basicGroup == null)
                              {
                                  continue;
                              }
  
                              if (basicGroup.CanInviteUsers())
                              {
                                  Items.Add(chat);
                              }
                          }
                          else if (chat.Type is ChatTypeSupergroup super)
                          {
                              var supergroup = ProtoService.GetSupergroup(super.SupergroupId);
                              if (supergroup == null)
                              {
                                  continue;
                              }
  
                              if (supergroup.CanInviteUsers())
                              {
                                  Items.Add(chat);
                              }
                          }
                      }
                      else
                      {
                          if (CacheService.CanPostMessages(chat))
                          {
                              Items.Add(chat);
                          }
                      }
                  }
              }
          }
  
          private MvxObservableCollection<Chat> _selectedItems = new MvxObservableCollection<Chat>();
          public MvxObservableCollection<Chat> SelectedItems
          {
              get
              {
                  return _selectedItems;
              }
              set
              {
                  Set(ref _selectedItems, value);
                  SendCommand?.RaiseCanExecuteChanged();
              }
          }
  
          private string _comment;
          public string Comment
          {
              get
              {
                  return _comment;
              }
              set
              {
                  Set(ref _comment, value);
              }
          }
  
          private IList<Message> _messages;
          public IList<Message> Messages
          {
              get
              {
                  return _messages;
              }
              set
              {
                  Set(ref _messages, value);
              }
          }
  
          private User _inviteBot;
          public User InviteBot
          {
              get
              {
                  return _inviteBot;
              }
              set
              {
                  Set(ref _inviteBot, value);
              }
          }
  
          private InputMessageContent _inputMedia;
          public InputMessageContent InputMedia
          {
              get
              {
                  return _inputMedia;
              }
              set
              {
                  Set(ref _inputMedia, value);
              }
          }
  
          public bool IsWithMyScore { get; set; }
  
          public bool IsCopyLinkEnabled
          {
              get
              {
                  return _shareLink != null && DataTransferManager.IsSupported();
              }
          }
  
          private Uri _shareLink;
          public Uri ShareLink
          {
              get
              {
                  return _shareLink;
              }
              set
              {
                  Set(ref _shareLink, value);
                  RaisePropertyChanged(() => IsCopyLinkEnabled);
              }
          }
  
          private string _shareTitle;
          public string ShareTitle
          {
              get
              {
                  return _shareTitle;
              }
              set
              {
                  Set(ref _shareTitle, value);
              }
          }
  
          private bool _isCommentEnabled;
          public bool IsCommentEnabled
          {
              get
              {
                  return _isCommentEnabled;
              }
              set
              {
                  Set(ref _isCommentEnabled, value);
              }
          }
  
          private SearchChatsType _searchType;
          public SearchChatsType SearchType
          {
              get
              {
                  return _searchType;
              }
              set
              {
                  Set(ref _searchType, value);
              }
          }
  
          private InlineKeyboardButtonTypeSwitchInline _switchInline;
          public InlineKeyboardButtonTypeSwitchInline SwitchInline
          {
              get { return _switchInline; }
              set { _switchInline = value; }
          }
  
          private User _switchInlineBot;
          public User SwitchInlineBot
          {
              get { return _switchInlineBot; }
              set { _switchInlineBot = value; }
          }
  
          public string SendMessage { get; set; }
          public bool SendMessageUrl { get; set; }
  
  
  
          public MvxObservableCollection<Chat> Items { get; private set; }
  
  
  
          public RelayCommand SendCommand { get; }
          private async void SendExecute()
          {
              var chats = SelectedItems.ToList();
              if (chats.Count == 0)
              {
                  return;
              }
  
              if (!string.IsNullOrEmpty(_comment))
              {
  
gt:                 var formatted = GetFormattedText(_comment);