﻿// Decompiled with JetBrains decompiler
// Type: InAppPurchaseController
// Assembly: Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 19851F1B-4780-4223-BA01-2C20F2CD781E
// Assembly location: C:\Users\CGA Computer\Desktop\LEGO Universe Stuff\LOCO Server\Unity Web Player Extractor\Saved Files\LOCO Server.unity3d_unpacked\Assembly-CSharp.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Wb.Lpw.AssetPipeline.Client;
using Wb.Lpw.AssetPipeline.Common.DataDefinition;
using Wb.Lpw.Game.Common.Descriptions.Trading;
using Wb.Lpw.Game.Common.GameModule;
using Wb.Lpw.Game.Common.Logging;
using Wb.Lpw.Game.Common.Notification;
using Wb.Lpw.Game.Common.Store;
using Wb.Lpw.Platform.Protocol;
using Wb.Lpw.Platform.Protocol.Marketplace;
using Wb.Lpw.Shared.Common;
using Wb.Lpw.Shared.Network;

public class InAppPurchaseController : TradeGenericController
{
  private List<FordAppleStoreItem> _appleStoreItems = new List<FordAppleStoreItem>();
  private List<FordAppleStoreItem> _itemsToDisplay = new List<FordAppleStoreItem>();
  private UITradingCatalogScreen.CatalogInfos _catalogInfos;

  public AppleStoreItemDisplayInfo.Type KindToShow { get; set; }

  private void StartLoadingAnim()
  {
    WBUtility.ActivateRecursively(UITradingCatalogScreen.Instance.UICatalogInfos.GeneralLoadingAnimationParent, true);
    FlashAnimationX.SetAllChildAnimationsToLoop(UITradingCatalogScreen.Instance.UICatalogInfos.GeneralLoadingAnimationParent, "ANIM_Start", true);
    FlashAnimationX.PlayAllChildAnimations(UITradingCatalogScreen.Instance.UICatalogInfos.GeneralLoadingAnimationParent, "ANIM_Start", 0.0f, 1f);
  }

  private void StopLoadingAnim()
  {
    FlashAnimationX.SetAllChildAnimationsToLoop(UITradingCatalogScreen.Instance.UICatalogInfos.GeneralLoadingAnimationParent, "ANIM_Start", false);
    WBUtility.ActivateRecursively(UITradingCatalogScreen.Instance.UICatalogInfos.GeneralLoadingAnimationParent, false);
  }

  public override void Open(List<ItemRef<TradingPageDescription>> pages, UITradingCatalogScreen.CatalogInfos infos, int[] lockedBuckets = null)
  {
    this.ShowStore(infos);
  }

  public override void Show(UITradingCatalogScreen.CatalogInfos infos)
  {
    FlashAnimationX.ForceAllChildAnimationsToEnd(infos.MainObject, UITradingCatalogScreen.AnimationNames.InAppPurchaseStore, true);
    this.ShowStore(infos);
  }

  private void ShowStore(UITradingCatalogScreen.CatalogInfos infos)
  {
    GameLog.Brian.DebugFormat("Trace: InAppPurchaseController.ShowStore");
    this._tableView = infos.TableView;
    infos.CatalogTitleText.Text = FordLocalizationMgr.Menu.GetStringFromId("TRADING_IAP_TITLE", string.Empty, string.Empty);
    if (!AppleStoreMgr.Instance.IsInit)
    {
      CommonLog.Network.ErrorFormat("AppleStoreMgr is not yet initialized. For some reason, FordDomainRoom.OnReady who should call it did not call it this time. Check there to make sure it is called.");
      AppleStoreMgr.Instance.Initialize((IAppleStoreMgrObserver) new FordAppleStoreObserver());
    }
    this.StartLoadingAnim();
    if (infos != null)
    {
      this._pages = new List<ItemRef<TradingPageDescription>>();
      int[] numArray = new int[1];
      int index = 0;
      int num = -1;
      numArray[index] = num;
      this._lockedBuckets = numArray;
      this._catalogInfos = infos;
      this.ResetLayouts(this._lockedBuckets);
    }
    this.FillUpStore();
  }

  public override void Close()
  {
    base.Close();
    if (this._activeCoroutines <= 0)
      return;
    AppleStoreMgr.Instance.RemoveGetAppStoreItemsCallback(new Action<AppleStoreMgr.AppleStoreResult>(this.GetAppStoreItemsCallback));
    --this._activeCoroutines;
  }

  public override void Hide()
  {
    if (this._activeCoroutines > 0)
    {
      AppleStoreMgr.Instance.RemoveGetAppStoreItemsCallback(new Action<AppleStoreMgr.AppleStoreResult>(this.GetAppStoreItemsCallback));
      --this._activeCoroutines;
    }
    this.ResetLayouts(this._lockedBuckets);
    ResourceMgr.Instance.RemoveAllReferences((object) this);
  }

  [DebuggerHidden]
  private IEnumerator Cancel()
  {
    // ISSUE: object of a compiler-generated type is created
    return (IEnumerator) new InAppPurchaseController.\u003CCancel\u003Ec__Iterator11B()
    {
      \u003C\u003Ef__this = this
    };
  }

  private void FillUpStore()
  {
    ++this._activeCoroutines;
    AppleStoreMgr.Instance.GetAppStoreItems(new Action<AppleStoreMgr.AppleStoreResult>(this.GetAppStoreItemsCallback));
  }

  private void GetAppStoreItemsCallback(AppleStoreMgr.AppleStoreResult result)
  {
    if (!result.operationSuccessful || result.items == null)
    {
      NotificationMgr.ShowModal(new ModalDialogOptions()
      {
        Message = FordLocalizationMgr.Error.GetStringFromId("AppStore_IAP_CantGetItems", string.Empty, string.Empty),
        Title = FordLocalizationMgr.Error.GetStringFromId("ERROR_TITLE", string.Empty, string.Empty),
        UserChoice = true
      }, new NotificationOptions()
      {
        EndCallback = (NotificationEndFn) (notificationResult =>
        {
          if (notificationResult == NotificationResult.UserInputOk)
          {
            this.FillUpStore();
            --this._activeCoroutines;
          }
          else
            this.StartCoroutine("Cancel");
        })
      });
    }
    else
    {
      GameLog.Brian.Warn((object) "AppleStoreMgr.Instance.GetAppStoreItems");
      this._appleStoreItems = FordAppleStoreItem.SyncAssetDbWithAppStoreItems((IEnumerable<AppleStoreMgr.AppleStoreItem>) result.items);
      this.StopLoadingAnim();
      List<TradingPageData> pages = new List<TradingPageData>();
      TradingPageData tradingPageData = new TradingPageData()
      {
        LayoutBuckets = new List<TradingLayoutDescription.BucketType>(),
        Buckets = new List<Bucket>()
      };
      pages.Add(tradingPageData);
      int cellsPerPage = 0;
      this._itemsToDisplay.Clear();
      for (int index = 0; index < this._appleStoreItems.Count; ++index)
      {
        tradingPageData.LayoutBuckets.Add(TradingLayoutDescription.BucketType._2x2);
        tradingPageData.Buckets.Add(new Bucket());
        cellsPerPage += 2;
        this._itemsToDisplay.Add(this._appleStoreItems[index]);
      }
      this._itemsToDisplay.Sort((Comparison<FordAppleStoreItem>) ((a, b) =>
      {
        if (a.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.Membership && b.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.GoldBricks)
          return -1;
        if (a.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.GoldBricks && b.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.Membership)
          return 1;
        if (a.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.Membership && b.DisplayInfo.ItemKind == AppleStoreItemDisplayInfo.Type.Membership)
        {
          if (a.DisplayInfo.MembershipLength > b.DisplayInfo.MembershipLength)
            return -1;
          if (a.DisplayInfo.MembershipLength < b.DisplayInfo.MembershipLength)
            return 1;
          if (a.DisplayInfo.GoldBricksAmount > b.DisplayInfo.GoldBricksAmount)
            return -1;
          return a.DisplayInfo.GoldBricksAmount < b.DisplayInfo.GoldBricksAmount ? 1 : 0;
        }
        if (a.DisplayInfo.ItemKind != AppleStoreItemDisplayInfo.Type.GoldBricks || b.DisplayInfo.ItemKind != AppleStoreItemDisplayInfo.Type.GoldBricks)
          return 0;
        if (a.DisplayInfo.GoldBricksAmount > b.DisplayInfo.GoldBricksAmount)
          return -1;
        return a.DisplayInfo.GoldBricksAmount < b.DisplayInfo.GoldBricksAmount ? 1 : 0;
      }));
      this.CreateLayouts(pages, this._catalogInfos, this._lockedBuckets, cellsPerPage, MarketController.MarketMode.Buying);
      this._tableView.JumpToItem(this._itemsToDisplay.FindIndex((Predicate<FordAppleStoreItem>) (storeItem => storeItem.DisplayInfo.ItemKind == this.KindToShow)) * 2);
      --this._activeCoroutines;
      if (!Object.op_Inequality((Object) NetworkMgr.Instance, (Object) null) || NetworkMgr.Instance.BIGameModule == null)
        return;
      NetworkMgr.Instance.BIGameModule.SendEventIAPStoreView(this.KindToShow != AppleStoreItemDisplayInfo.Type.Membership ? GameModuleProtocol.BI.SendEventIAPStoreView.GB : GameModuleProtocol.BI.SendEventIAPStoreView.Membership, (Action<OperationResult, GameModuleProtocol.BI.SendEventIAPStoreViewResponse>) null);
    }
  }

  public override bool FillBucket(ItemBucket bucketToFill, ItemBucket.BucketState state, MarketplaceItem item = null, Wb.Lpw.Game.Common.Descriptions.InventoryItem invItem = null, int bucketUnlockPrice = 0)
  {
    return false;
  }

  public override bool FillBucket(int pageIndex, int bucketIndex, ItemBucket.BucketState state)
  {
    return false;
  }

  public override void ConfigBucket(ItemBucket newBucket, Bucket bucketInfo, int bucketSlotId)
  {
    if (bucketSlotId < 0 || bucketSlotId >= this._itemsToDisplay.Count)
      CommonLog.Interface.WarnFormat("BucketSlotId is out of range. bucketSlotId {0}, count {1}", (object) bucketSlotId, (object) this._itemsToDisplay.Count);
    else
      newBucket.SetAppStoreItem(this._itemsToDisplay[bucketSlotId]);
  }

  public void ButtonClicked(int bucketSlotId)
  {
    // ISSUE: object of a compiler-generated type is created
    // ISSUE: variable of a compiler-generated type
    InAppPurchaseController.\u003CButtonClicked\u003Ec__AnonStorey2BA clickedCAnonStorey2Ba = new InAppPurchaseController.\u003CButtonClicked\u003Ec__AnonStorey2BA();
    // ISSUE: reference to a compiler-generated field
    clickedCAnonStorey2Ba.item = this._itemsToDisplay[bucketSlotId];
    // ISSUE: reference to a compiler-generated field
    NetworkMgr.Instance.BIGameModule.SendEventIAPClick(clickedCAnonStorey2Ba.item.AppleStoreItem.CatalogSku, (Action<OperationResult, GameModuleProtocol.BI.SendEventIAPClickResponse>) null);
    // ISSUE: reference to a compiler-generated method
    Action callback = new Action(clickedCAnonStorey2Ba.\u003C\u003Em__43B);
    FordInputMgr.Instance.DisableAllInputs(true, "InAppPurchaseController", UIModalParentalGate.ParentalGateOpenedCond);
    FordPanelManager.Instance.ShowParentalGate(callback);
  }
}
