context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Text;
  
  using Android.App;
  using Android.Content;
  using Android.Content.Res;
  using Android.Graphics;
  using Android.OS;
  using Android.Runtime;
  using Android.Util;
  using Android.Views;
  using Android.Widget;
  using Com.Shehabic.Droppy;
  using GalaSoft.MvvmLight.Helpers;
  using MALClient.Android.DIalogs;
  using MALClient.Android.Flyouts;
  using MALClient.Android.Listeners;
  using MALClient.Android.Resources;
  using MALClient.Models.Enums;
  using MALClient.XShared.Utils;
  using MALClient.XShared.ViewModels;
  using Orientation = Android.Content.Res.Orientation;
  
  namespace MALClient.Android.UserControls.AnimeItems
  {
      public class AnimeCompactItem : UserControlBase<AnimeItemViewModel,FrameLayout>
      {
          private int _position;
          private readonly Action<AnimeItemViewModel> _onClick;
  
          public int Position
          {
              get { return _position; }
              set { _position = value; }
          }
  
          #region Constructors
  
          public AnimeCompactItem(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
          {
          }
  
          public AnimeCompactItem(Context context,int position, Action<AnimeItemViewModel> onClick) : base(context)
          {
              _position = position;
              _onClick = onClick;
          }
  
          public AnimeCompactItem(Context context, IAttributeSet attrs) : base(context, attrs)
          {
          }
  
          public AnimeCompactItem(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
          {
          }
  
          public AnimeCompactItem(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
          {
          }
  
  
          #endregion
  
          protected override int ResourceId => Resource.Layout.AnimeCompactItem;
  
          protected override void BindModelFling()
          {
             
          }
  
          protected override void BindModelFull()
          {
              RootContainer.SetOnClickListener(new OnClickListener(v => ContainerOnClick()));
  
              ViewModel.PropertyChanged += ViewModelOnPropertyChanged;       
          }
  
          protected override void RootContainerInit()
          {
              AnimeCompactItemWatchedButton.SetOnClickListener(new OnClickListener(view => ShowWatchedDialog()));
              AnimeCompactItemScoreButton.SetOnClickListener(new OnClickListener(view => ShowRatingDialog()));
              AnimeCompactItemStatusButton.SetOnClickListener(new OnClickListener(view => ShowStatusDialog()));
              AnimeCompactItemTagsButton.SetOnClickListener(new OnClickListener(OnTagsButtonClick));
  
              if(Context.Resources.Configuration.Orientation == Orientation.Landscape)
                  OnConfigurationChanged(Context.Resources.Configuration);
          }
  
          protected override void BindModelBasic()
          {
              ViewModel.AnimeItemDisplayContext = ViewModelLocator.AnimeList.AnimeItemsDisplayContext;
  
              (RootContainer.GetChildAt(0) as ViewGroup).GetChildAt(0).SetBackgroundResource(_position % 2 == 0
                  ? ResourceExtension.BrushRowAlternate1Res
                  : ResourceExtension.BrushRowAlternate2LighterRes);
  
              AnimeCompactItemType.Text = ViewModel.PureType;
              AnimeCompactItemTitle.Text = ViewModel.Title;
  
              AnimeCompactItemFavouriteIndicator.Visibility =
                  ViewModel.IsFavouriteVisibility ? ViewStates.Visible : ViewStates.Gone;
  
              AnimeCompactItemTagsButton.Visibility = ViewModel.TagsControlVisibility
                  ? ViewStates.Visible
                  : ViewStates.Gone;
  
              AnimeCompactItemGlobalScore.Text = ViewModel.GlobalScoreBind;
  
              if (string.IsNullOrEmpty(ViewModel.TopLeftInfoBind))
                  AnimeCompactItemTopLeftInfo.Visibility = ViewStates.Gone;
              else
              {
                  AnimeCompactItemTopLeftInfo.Visibility = ViewStates.Visible;
                  AnimeCompactItemTopLeftInfo.Text = ViewModel.TopLeftInfoBind;
              }
  
              SetPriorityIndicator();
  
              AnimeCompactItemScoreLabel.Text = ViewModel.MyScoreBind;
              AnimeCompactItemStatusLabel.Text = ViewModel.MyStatusBind;
              AnimeCompactItemWatchedButton.Text = ViewModel.MyEpisodesBind;
          }
  
          private void ViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
          {
              switch (propertyChangedEventArgs.PropertyName)
              {
                  case nameof(ViewModel.MyStatusBindShort):
                      AnimeCompactItemWatchedButton.Text = ViewModel.MyEpisodesBind;
                      break;
                  case nameof(ViewModel.MyEpisodesBindShort):
                      AnimeCompactItemStatusLabel.Text = ViewModel.MyStatusBind;
                      break;
                  case nameof(ViewModel.MyScoreBindShort):
                      AnimeCompactItemScoreLabel.Text = ViewModel.MyScoreBind;
                      break;
  
                  case nameof(ViewModel.Priority):
                      SetPriorityIndicator();
                      break;
              }
          }
  
          private void SetPriorityIndicator()
          {
  
              if (Settings.ShowPriorities)
              {
                  switch (ViewModel.Priority)
                  {
                      case AnimePriority.Low:
                          if (Settings.ShowLowPriorities)
                          {
                              RootBorderElement.BackgroundTintList =
                                  ColorStateList.ValueOf(ResourceExtension.LowPriorityColour);
                          }
                          else
                          {
                              RootBorderElement.BackgroundTintList = null;
                          }
                          break;
                      case AnimePriority.Medium:
                          RootBorderElement.BackgroundTintList =
                              ColorStateList.ValueOf(ResourceExtension.MediumPriorityColour);
                          break;
                      case AnimePriority.High:
                          RootBorderElement.BackgroundTintList =
                              ColorStateList.ValueOf(ResourceExtension.HighPriorityColour);
                          break;
                      default:
                          throw new ArgumentOutOfRangeException();
                  }
  
              }
              else
              {
                  RootBorderElement.BackgroundTintList = null;
              }
          }
  
          protected override void CleanupPreviousModel()
          {
              ViewModel.PropertyChanged -= ViewModelOnPropertyChanged;
          }
  
          private DroppyMenuPopup _tagsMenu;
          private void OnTagsButtonClick(View view)
          {
              _tagsMenu = AnimeItemFlyoutBuilder.BuildForAnimeItemTags(Context, view, ViewModel,
                  () => _tagsMenu.Dismiss(true));
              _tagsMenu.Show();
          }
  
          #region Dialogs
  
          private void ShowStatusDialog()
          {
              if (ViewModel.Auth)
                  AnimeUpdateDialogBuilder.BuildStatusDialog(ViewModel, ViewModel.ParentAbstraction.RepresentsAnime, ViewModel.ChangeStatus);
          }
  
          private void ShowWatchedDialog()
          {
              if (ViewModel.Auth)
                  AnimeUpdateDialogBuilder.BuildWatchedDialog(ViewModel, null, ViewModel.ParentAbstraction.RepresentsAnime ? false : Settings.MangaFocusVolumes);
          }
          private void ShowRatingDialog()
          {
              if (ViewModel.Auth)
                  AnimeUpdateDialogBuilder.BuildScoreDialog(ViewModel, f => ViewModel.ChangeScore(f.ToString()));
          }
          #endregion
  
          private void ContainerOnClick()
          {
  
gt:             if (_onClick != null)