using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class ResShower : MonoBehaviour
{
    [SerializeField] GameObject mTplCurrency;
    [SerializeField] RectTransform mRctCurrencyIcon;
    [SerializeField] Text mTxtCurrency;
    [SerializeField] Text mTxtCurrencyChange;

    Sequence mPopTweenSequence;
    Sequence mFadeTweenSequence;

    private Camera mMainCam;
    private Camera mUICam;

    private int mFinalCount;

    public void SetCamera(Camera pMainCam, Camera pUICam = null)
    {
        mMainCam = pMainCam;
        mUICam = pUICam;
    }

    public void FlyCoins(Vector3 pWorldPos, int pFlyCount, int pCoinCount, int pFinalCount)
    {
        mFinalCount = pFinalCount;
        float tFlyDelta = 0.05f;

        for (int i = 0; i < pFlyCount; i++)
        {
            FlyOneCoin(pWorldPos, tFlyDelta * i);
        }

        PopCoinChange(pCoinCount);
        StartCoroutine(SeperatlyAddCoin(tFlyDelta, pFlyCount, pCoinCount));
    }

    private void FlyOneCoin(Vector3 pWorldPos, float pDelay)
    {
        Vector2 tStartPos = GetLocalPosInRect(pWorldPos, mRctCurrencyIcon, mMainCam, mUICam);
        RectTransform tFlyCoin = Instantiate<GameObject>(mTplCurrency).GetComponent<RectTransform>();
        tFlyCoin.gameObject.SetActive(true);
        tFlyCoin.SetParent(mRctCurrencyIcon, false);
        tFlyCoin.localScale = Vector3.one;
        tFlyCoin.anchoredPosition = tStartPos;

        Vector3 tJumpPos = tStartPos + new Vector2(UnityEngine.Random.Range(-150f, 150f), UnityEngine.Random.Range(-80f, -120f));
        tFlyCoin.DOJumpAnchorPos(tJumpPos, 300, 1, 0.5f);

        tFlyCoin.DOAnchorPos(Vector2.zero, 0.5f).SetDelay(0.5f + pDelay).SetEase(Ease.Linear).onComplete = () =>
        {
            Destroy(tFlyCoin.gameObject);
        };
    }

    private IEnumerator SeperatlyAddCoin(float pFlyDelta, int pFlyCount, int pCoinCount)
    {
        int tCoinPerFly = 1, tCoinLastFly = 1;
        if (pCoinCount > pFlyCount)
        {
            tCoinPerFly = pCoinCount / pFlyCount;
            tCoinLastFly = pCoinCount - tCoinPerFly * pFlyCount;
            if (tCoinLastFly == 0)
                tCoinLastFly = tCoinPerFly;
        }

        yield return new WaitForSeconds(1f);

        for (int i = 0; i < pFlyCount; i++)
        {
            int tAddCount = i < pFlyCount - 1 ? tCoinPerFly : tCoinLastFly;
            RefreshCoins(tAddCount);
            if (i == pFlyCount - 1)
            {
                RefreshCoinsWithoutAni();
            }
            yield return new WaitForSeconds(pFlyDelta);
        }
    }

    public void RefreshCoins(int pAddCount)
    {
        int tCurrentNum = int.Parse(mTxtCurrency.text);
        tCurrentNum += pAddCount;
        mTxtCurrency.text = tCurrentNum.ToString();

    }

    public void RefreshCoinsWithoutAni()
    {
        mTxtCurrency.text = mFinalCount.ToString();
        //mRectCoinIcon.anchoredPosition = new Vector2(-(mTxtCoin.preferredWidth), mRectCoinIcon.anchoredPosition.y);
    }

    public void PopCoinChange(int pChangedNum)
    {
        mTxtCurrencyChange.text = pChangedNum >= 0 ? "+" + pChangedNum.ToString() : pChangedNum.ToString();
        Color tColor = pChangedNum > 0 ? Color.green : Color.red;
        tColor.a = 0;
        mTxtCurrencyChange.color = tColor;
        mTxtCurrencyChange.rectTransform.anchoredPosition = new Vector2(32, 0);

        mTxtCurrencyChange.rectTransform.DOKill();
        mPopTweenSequence = DOTween.Sequence();
        mPopTweenSequence.Append(mTxtCurrencyChange.rectTransform.DOAnchorPosY(-100, 0.3f).SetEase(Ease.OutBounce));
        mPopTweenSequence.AppendInterval(0.8f);
        mPopTweenSequence.Append(mTxtCurrencyChange.rectTransform.DOAnchorPosY(0, 0.3f).SetEase(Ease.InBounce));

        mTxtCurrencyChange.DOKill();
        mFadeTweenSequence = DOTween.Sequence();
        mFadeTweenSequence.Append(mTxtCurrencyChange.DOFade(1, 0.3f));
        mFadeTweenSequence.AppendInterval(0.8f);
        mFadeTweenSequence.Append(mTxtCurrencyChange.DOFade(0, 0.3f));

        mPopTweenSequence.Restart();
        mFadeTweenSequence.Restart();
    }

    private void PunchIcon(RectTransform pRect)
    {
        pRect.localScale = Vector3.one;
        pRect.DOKill();
        pRect.DOPunchScale(Vector3.one * 1.2f, 0.3f);
    }

    public void ShowNotEnoughCoin()
    {
        mTxtCurrency.color = Color.white;
        mTxtCurrency.rectTransform.anchoredPosition = new Vector2(32, 0);

        mTxtCurrency.color = Color.red;
        mTxtCurrency.rectTransform.DOKill();
        mTxtCurrency.rectTransform.DOPunchAnchorPos(new Vector2(0, 15), 0.5f).onComplete = () =>
        {
            mTxtCurrency.color = Color.white;
            mTxtCurrency.rectTransform.anchoredPosition = new Vector2(32, 0);
        };
    }

    public Vector2 GetLocalPosInRect(Vector3 pWorldPos, RectTransform pRect, Camera pMainCam, Camera pUICam = null)
    {
        Vector2 tScreenPos = RectTransformUtility.WorldToScreenPoint(pMainCam, pWorldPos);
        Vector2 tLocalPos = Vector2.zero;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(pRect, tScreenPos, pUICam, out tLocalPos);

        return tLocalPos;
    }
}
