private void HideSingleItem(LootCrateItem crateItem, bool isUpdateLayout, float preDelay = 0)
    {
        if (!_crateItems.Contains(crateItem))
        {
            traceError("LootCrateItem already removed, cannot hide it again!");
            return;
        }

        _crateItems.Remove(crateItem);

        FlyOutItem(crateItem, 0)
        .SetDelay(preDelay);

        if (!isUpdateLayout)
        {
            return;
        }

        this.Wait(preDelay + timeLayoutUpdate * speedScale, () => {
            Destroy(crateItem);

            if (_crateItems.Count == 0)
            {
                BtnClose();
                return;
            }

            LayoutUpdate();
        });
    }
    public void BtnOnItemClick(LootCrateItem crateItem, bool fromStory)
    {
        if (fromStory)
        {
            return;
        }
        if (_itemDetails != null)
        {
            traceError("Can only popup one ItemDetailsInterface at a time.");
            return;
        }

        DeselectItems();

        _currentLootCrateItem = crateItem;

        Item item = crateItem.itemData;

        _itemDetails           = ItemDetailsInterface.Open(item);
        _itemDetails.onRemove += (panel) => {
            //When the Item Details panel is removed, null this reference and the Identify button as well:
            _itemDetails    = null;
            _identifyButton = null;
        };

        _itemDetails.AddButton("Sell\n<color={1}>({0} gold)</color>".Format2(item.SellValue, ColorConstants.HTML_GOLD), _itemDetails.Btn_Sell);
        _itemDetails.AddButton("Shard", _itemDetails.Btn_Shard);

        var rectTrans = (RectTransform)_itemDetails.transform;

        rectTrans.pivot = new Vector2(0.5f, 0.5f);

        crateItem.isSelected = true;
    }
    private void OnItemRemoved(Item item)
    {
        if (_currentLootCrateItem == null)
        {
            traceError("How can an item be removed... yet there's none selected on the LootCrate screen?");
            return;
        }

        HideSingleItem(_currentLootCrateItem, true, timeBeforeHideSingleItem * speedScale);

        _currentLootCrateItem = null;
    }
    private Tweener FlyOutItem(LootCrateItem item, int id)
    {
        //Disable the "wavy" motion & target position chasing:
        var wavy = item.GetComponent <WavyPosition>();

        wavy.enabled = false;

        //Disable any interactivity on this item now:
        item.SetEnabledInChildren <Button>(false);

        //Fly everything up up up!
        return(item.transform.TweenMoveByY(GetScreenSize().y * 2.5f, timeItemsFlyUp * speedScale)
               .SetEase(Ease.InSine));
    }
    public void Initialize(LootCrate crate, System.Action callback = null, System.Action callbackOnComplete = null, bool fromStory = false)
    {
        this.crate = crate;
        List <Item> items = crate.GenerateItems();

        this.callbackOnComplete = callbackOnComplete;
        noMenuManagerRef        = fromStory;

        Debug.Log("Adding " + items.Count + " items.");

        /*
         * foreach(Item i in items) {
         *  Debug.Log(" --- --- [Item] " + i.Name);
         * }*/

        // NOTE-TO-SELF: DO call this API method! (Instead of one-by-one, batch-calls adds all generated items)
        API.Items.AddMany(items)
        .Catch(err => traceError("Something went wrong while adding newly generated items to server-side: " + err.Message));
        //.Then(res => trace("Added " + items.Count + " items server-side..."))

        DataManager.Instance.allItemsList.AddRange(items);

        if (!fromStory)
        {
            RemoveCrateFromPlayerData(callback);
        }

        // Crate Sequence
        TimelineTween.Create(
            (next) => {
            AudioManager.Instance.Play(SFX_UI.Earthquake);

            //btnGoBack.transform.DOScale(Vector2.zero, 0.5f * speedScale);
            btnGoBack.GetComponent <RectTransform>().DOAnchorPosY(0f, 0.5f * speedScale);

            crateBtn.transform.SetParent(this.transform);

            // Background Shadow here
            backgroundShadow.DOColor(new Color(0f, 0f, 0f, 0.7f), timeScrollerSlides * speedScale);

            crateBtn.transform.TweenMoveByY(50, timeScrollerSlides * speedScale)
            .SetEase(Ease.InOutSine)
            .OnComplete(() => next());
        },

            (next) => {
            // If on crate screen reset the crate list positions

            crateBtn.transform
            .DOShakePosition(timeCrateShake * speedScale, 10, 30, 90, false, false)
            .OnComplete(() => next());
        },

            (next) => {
            AudioManager.Instance.Stop(SFX_UI.Earthquake);
            AudioManager.Instance.Play(SFX_UI.Explosion);

            GameObject itemPrefab = GetPrefab("SubItems/LootCrateItem");

            var canvasSize = GetScreenSize();
            float itemSize = canvasSize.x / 3f;

            for (int i = 0; i < items.Count; i++)
            {
                Item itemData             = items[i];
                GameObject itemGO         = GameObject.Instantiate(itemPrefab);
                LootCrateItem item        = itemGO.GetComponent <LootCrateItem>();
                Button itemBtn            = itemGO.GetComponent <Button>();
                TextMeshProUGUI itemLabel = itemGO.GetComponentInChildren <TextMeshProUGUI>();
                RectTransform itemTrans   = (RectTransform)itemGO.transform;

                itemTrans.SetParent(this.transform);
                itemTrans.localPosition = Vector2.zero;
                itemTrans.SetWidthAndHeight(itemSize);

                item.itemData = itemData;
                item.UpdateIdentifiedIcon();

                _crateItems.Add(item);

                itemBtn.onClick.AddListener(() => BtnOnItemClick(item, fromStory));
            }

            LayoutUpdate();

            TimelineTween.Scatter(0.08f, _crateItems, (item, id) => {
                return(TimelineTween.Pulsate(item.transform, 1.3f, timeItemsExplode * speedScale));
            });

            Color c = crateBtn.GetComponent <Image>().color;
            c.a     = 0;
            crateBtn.GetComponent <Image>().DOColor(c, 0.3f).OnComplete(() => {
                Destroy(crateBtn.gameObject);
            });

            /*CanvasGroup cg = crateBtn.GetComponent<CanvasGroup>();
             * cg.DOFade(0, 0.3f)
             *  .OnComplete(() => {
             *      Destroy(crateBtn.gameObject);
             *  });*/

            crateBtn.transform
            .DOScale(2.0f, timeCrateExplode * speedScale)
            .SetEase(Ease.OutQuint);

            this.Wait(timeItemsExplode * 2.0f * speedScale, next);
        },

            (next) => {
            TimelineTween.Scatter(0.1f, _crateItems, (item, id) => {
                return(item.filler.DOFade(0, 1 * speedScale));
            });

            this.Wait(timeItemsFormCircle * 1.0f * speedScale, next);
        }
            );
    }