context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using Android.App;
  using Android.Content;
  using Android.Graphics;
  using Android.OS;
  using Android.Runtime;
  using Android.Util;
  using Android.Views;
  using Android.Webkit;
  using Android.Widget;
  using FFImageLoading.Views;
  using GalaSoft.MvvmLight.Helpers;
  using MALClient.Android.Activities;
  using MALClient.Android.DIalogs;
  using MALClient.Android.Listeners;
  using MALClient.Android.Resources;
  using MALClient.Android.Web;
  using MALClient.Models.Enums;
  using MALClient.XShared.NavArgs;
  using MALClient.XShared.Utils;
  using MALClient.XShared.ViewModels;
  using MALClient.XShared.ViewModels.Forums.Items;
  
  namespace MALClient.Android.UserControls.ForumItems
  {
      public class ForumTopicItem : UserControlBase<ForumTopicMessageEntryViewModel, LinearLayout>
      {
          private class WebViewTag
          {
  
              public DataJavascriptInterface DataJavascriptInterface { get; }
              public int ContentHash { get; set; }
  
              public WebViewTag(DataJavascriptInterface dataJavascriptInterface)
              {
                  DataJavascriptInterface = dataJavascriptInterface;
              }
  
          }
  
          private ListenableWebClient _client;
          private DataJavascriptInterface _dataJavascriptInterface;
          private readonly List<Binding> _bindings = new List<Binding>();
  
          #region Constructors
  
          public ForumTopicItem(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
          {
          }
  
          public ForumTopicItem(Context context) : base(context)
          {
          }
  
          public ForumTopicItem(Context context, IAttributeSet attrs) : base(context, attrs)
          {
          }
  
          public ForumTopicItem(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
          {
          }
  
          public ForumTopicItem(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
          {
          }
  
          #endregion
  
  
          protected override int ResourceId => Resource.Layout.ForumTopicPageItem;
  
          protected override void BindModelFling()
          {
              if (!ForumTopicPageItemAuthorImage.IntoIfLoaded(ViewModel.Data.Poster.MalUser.ImgUrl))
                  ForumTopicPageItemAuthorImage.Visibility = ViewStates.Gone;
              
  
              if (double.IsNaN(ViewModel.ComputedHtmlHeight))
              {
                  UpdateViewWithNewHeight(ForumTopicPageItemWebView, DimensionsHelper.DpToPx(200));
                  ForumTopicPageItemWebView.Visibility = ViewStates.Invisible;
              }
              else
              {
                  var tag = ForumTopicPageItemWebView.Tag.Unwrap<WebViewTag>();
                  if (tag.ContentHash == 0 || tag.ContentHash != ViewModel.Data.HtmlContent.GetHashCode())
                  {
                      ForumTopicPageItemWebView.LoadDataWithBaseURL(null, ResourceLocator.CssManager.WrapWithCss(ViewModel.Data.HtmlContent), "text/html; charset=utf-8", "UTF-8", null);
                      tag.ContentHash = ViewModel.Data.HtmlContent.GetHashCode();
                      ForumTopicPageItemWebView.Tag = tag.Wrap();
                  }
              }
  
          }
  
          protected override void BindModelFull()
          {
              try
              {
                  if (string.IsNullOrEmpty(ViewModel.Data.Poster.MalUser.ImgUrl))
                      ForumTopicPageItemAuthorImage.Visibility = ViewStates.Gone;
                  else
                  {
                      if (ForumTopicPageItemAuthorImage.Tag == null || (string)ForumTopicPageItemAuthorImage.Tag != ViewModel.Data.Poster.MalUser.ImgUrl)
                      {
                          ForumTopicPageItemAuthorImage.Into(ViewModel.Data.Poster.MalUser.ImgUrl);
                      }
                  }
                  ForumTopicPageItemWebView.Visibility = ViewStates.Visible;
  
  
                  _bindings.Add(this.SetBinding(() => ViewModel.Data).WhenSourceChanges(() =>
                  {
                      try
                      {
                          var tag = ForumTopicPageItemWebView.Tag.Unwrap<WebViewTag>();
                          if (tag.ContentHash == 0 || tag.ContentHash != ViewModel.Data.HtmlContent.GetHashCode())
                          {
                              ForumTopicPageItemWebView.LoadDataWithBaseURL(null, ResourceLocator.CssManager.WrapWithCss(ViewModel.Data.HtmlContent), "text/html; charset=utf-8", "UTF-8", null);
                              tag.ContentHash = ViewModel.Data.HtmlContent.GetHashCode();
                              ForumTopicPageItemWebView.Tag = tag.Wrap();
                          }
                      }
                      catch (Exception e)
                      {
                          //webview disposed
                      }
  
                  }));
  
                  _bindings.Add(this.SetBinding(() => ViewModel.ComputedHtmlHeight).WhenSourceChanges(() =>
                  {
                      try
                      {
                          if (ForumTopicPageItemWebView.Height < ViewModel.ComputedHtmlHeight)
                          {
                              UpdateViewWithNewHeight(ForumTopicPageItemWebView, (int)ViewModel.ComputedHtmlHeight);
                          }
                          else
                          {
                              UpdateViewWithNewHeight(ForumTopicPageItemWebView, DimensionsHelper.DpToPx(200));
                          }
                      }
                      catch (Exception e)
                      {
                         //webview disposed 
                      }
  
                  }));
  
                  _bindings.Add(this.SetBinding(() => ViewModel.Loading).WhenSourceChanges(() =>
                  {
                      if (ViewModel.Loading)
                      {
                          ForumTopicPageItemLoadingOverlay.Visibility = ViewStates.Gone;
                      }
                      else
                      {
                          ForumTopicPageItemLoadingOverlay.Visibility = ViewStates.Gone;
                      }
                  }));
              }
              catch (Exception e)
              {
                  //webview disposed
              }
          }
  
  
  
  
          protected override void BindModelBasic()
          {
              ForumTopicPageItemPostAuthor.Text = ViewModel.Data.Poster.MalUser.Name;
              ForumTopicPageItemPostNumber.Text = ViewModel.Data.MessageNumber;
              ForumTopicPageItemPostDate.Text = ViewModel.Data.CreateDate;
             // if (string.IsNullOrEmpty(ViewModel.Data.Poster.Status))
             //     ForumTopicPageItemDetailOnline.Visibility = ViewStates.Gone;
              //else
             // {
              //    ForumTopicPageItemDetailOnline.Visibility = ViewStates.Visible;
              //    ForumTopicPageItemDetailOnline.Text = ViewModel.Data.Poster.Status;
              //}
              if (string.IsNullOrEmpty(ViewModel.Data.Poster.Joined))
                  ForumTopicPageItemDetailsJoined.Visibility = ViewStates.Gone;
              else
              {
                  ForumTopicPageItemDetailsJoined.Visibility = ViewStates.Visible;
                  ForumTopicPageItemDetailsJoined.Text = ViewModel.Data.Poster.Joined;
              }
              if (string.IsNullOrEmpty(ViewModel.Data.Poster.Posts))
                  ForumTopicPageItemDetailsPostCount.Visibility = ViewStates.Gone;
              else
              {
                  ForumTopicPageItemDetailsPostCount.Visibility = ViewStates.Visible;
                  ForumTopicPageItemDetailsPostCount.Text = ViewModel.Data.Poster.Posts;
              }
  
              ForumTopicPageItemEditButton.Visibility = ViewModel.Data.CanEdit ? ViewStates.Visible : ViewStates.Gone;
              ForumTopicPageItemDeleteButton.Visibility = ViewModel.Data.CanDelete ? ViewStates.Visible : ViewStates.Gone;
              ForumTopicPageItemSendMessageButton.Visibility = ViewModel.MessagingVisible ? ViewStates.Visible : ViewStates.Gone;
  
              if (string.IsNullOrEmpty(ViewModel.Data.EditDate))
                  ForumTopicPageItemModifiedLabel.Visibility = ViewStates.Gone;
              else
              {
                  ForumTopicPageItemModifiedLabel.Visibility = ViewStates.Visible;
                  ForumTopicPageItemModifiedLabel.Text = ViewModel.Data.EditDate;
              }
          }
  
          protected override void RootContainerInit()
          {
              var jsInterface = new DataJavascriptInterface(Context);
              ForumTopicPageItemWebView.Settings.JavaScriptEnabled = true;
              _client = new ListenableWebClient();
              _client.NavigationInterceptOpportunity = NavigationInterceptOpportunity;
              ForumTopicPageItemWebView.SetWebViewClient(_client);
              ForumTopicPageItemWebView.AddJavascriptInterface(jsInterface, "android");
              ForumTopicPageItemWebView.Tag = new WebViewTag(jsInterface).Wrap();
              ForumTopicPageItemWebView.VerticalScrollBarEnabled = false;
              ForumTopicPageItemWebView.ScrollbarFadingEnabled = true;
              ForumTopicPageItemWebView.HorizontalScrollBarEnabled = false;
              ForumTopicPageItemWebView.ScrollBarDefaultDelayBeforeFade = 0;
              ForumTopicPageItemWebView.SetBackgroundColor(Color.Transparent);
              _dataJavascriptInterface = jsInterface;
  
              _dataJavascriptInterface.NewResponse += DataJavascriptInterfaceOnNewResponse;
  
              ForumTopicPageItemEditButton.SetOnClickListener(new OnClickListener(async view =>
              {
                  await ViewModel.StartEdit();
                  var str = await TextInputDialogBuilder.BuildForumPostTextInputDialog(Context, TextInputDialogBuilder.ForumPostTextInputContext.Edit, ViewModel.BBcodeContent);
                  if (!string.IsNullOrEmpty(str))
                  {
                      ViewModel.BBcodeContent = str;
                      ViewModel.SubmitEditCommand.Execute(null);
                  }
              }));
  
              ForumTopicPageItemQuoteButton.SetOnClickListener(new OnClickListener(async view =>
              {
  
gt:                 var semaphore = new SemaphoreSlim(0);