using System;
using System.Collections;
using System.Collections.Generic;
using MyGame.Scenes.Main.Scripts;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CoinManager : SingletonMonoBehaviour<CoinManager>
{
    private static readonly int Add = Animator.StringToHash("Add");
    
    // CoinView
    [SerializeField] private RectTransform coinIconTransform;
    [SerializeField] private TextWithCountUpInt coinCountText;
    [SerializeField] private Animator coinAnimator;
    
    [SerializeField] private RectTransform coinPrefab;
    [SerializeField] private BulkOrderAchievedEffect bulkOrderAchievedEffect;
    
    // Animation
    [SerializeField] private float coinUpDuration = 0.5f;
    [SerializeField] private float coinMoveDuration = 0.5f;

    [SerializeField] private RectTransform rootTransform;
    private int ownCoin;
    public int OwnCoin => ownCoin;
    private string coinTextFormat = "{0}";

    void Awake(){
        if(CheckInstance()) return ;
    }

    public void ChangeCoin(int count)
    {
        ownCoin = count;
        coinCountText.ChangeValue(coinTextFormat, ownCoin);
    }

    public void AddCoin(int count)
    {
        ownCoin += count;
        coinAnimator.SetTrigger(Add);
        SoundManager.Instance.PlaySE("se_coin_get");
        coinCountText.CountUpAnimation(coinTextFormat, ownCoin, coinUpDuration);
    }

    public void AddCoinWithEffect(int count, Vector3 pos, Action onComplete = null)
    {
        ownCoin += count;
        InstantiateEffect(coinPrefab, count, pos, coinIconTransform.position, () =>
        {
            coinCountText.CountUpAnimation(coinTextFormat, ownCoin, coinUpDuration);
            onComplete?.Invoke();
        });
    }

    private void InstantiateEffect(RectTransform prefab, int count, Vector3 from, Vector3 to, Action callback)
    {
        var coinEffect = Instantiate(prefab, from, Quaternion.identity, rootTransform);
        var animator = coinEffect.GetComponent<Animator>();
        this.CallLerp(coinMoveDuration, f =>
        {
            coinEffect.position = Vector3.Lerp(from, to, f * f);
        }, () =>
        {
            this.CallWaitForSeconds(0f, () => 
            {
                // コインが到着したらSEとバイブ再生。コインはずみアニメーションを再生
                animator.SetTrigger("Effect");
                Destroy(coinEffect.gameObject, 1f);
                VibrationManager.Instance.PlayVibrationOnceWeak();
                coinAnimator.SetTrigger(Add);
                SoundManager.Instance.PlaySE("se_coin_get");
                callback.Invoke();
            });
        });
    }
    
    public void AddCoinForBulkOrder(int count, Vector3 beginPos)
    {
        ownCoin += count;
        bulkOrderAchievedEffect.PlayCoinEffect(beginPos, coinIconTransform.position, () =>
        {
            coinAnimator.SetTrigger(Add);
            coinCountText.CountUpAnimation(coinTextFormat, ownCoin, coinUpDuration);
        });
    }
    
    public void SubCoin(int coin){
        ownCoin -= coin;
        if(coinCountText.gameObject.activeInHierarchy){
            coinCountText.CountUpAnimation(coinTextFormat, ownCoin, coinUpDuration);
        }else{
            coinCountText.ChangeValue(coinTextFormat, ownCoin);
        }
    }
}
