using System;
using UnityEngine;

namespace ChillConnect
{
    public class RewardAniModel : BaseModel
    {
        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnDispose()
        {
        }

        // protected override void OnReset()
        // {
        // }

        // #endregion

        // #region 读取数据

        // protected override void OnReadData()
        // {
        // }

        #endregion

        #region 本地存储



        // protected override void WriteLocalStorage()
        // {

        // }

        #endregion

        #region 消息

        protected override void AddListener()
        {

        }

        protected override void RemoveListener()
        {

        }

        #endregion
    }

    public class RewardAniData : IDisposable
    {
        public float CoinSum;

        public float AddCoinSum;

        public int RewardId;
        public Vector2 target;
        public Vector2 origin;
        public Vector2 center;

        public event Action<float> UpdataCb;
        public event Action EndEventCb;
        public event Action UICloseEventCb;

        private float coinAddTime = 0.45f;


        public bool IsPlayAudio;

        public RewardAniData(int id, float sum, Vector2 starPot = default, Vector2 tar = default)
        {
            RewardId = id;
            CoinSum = sum;
            origin = starPot;
            target = tar;
            AddCoinSum = CoinSum;
        }

        public void Updata()
        {
            float val = CoinSum * Time.deltaTime / coinAddTime;
            if (AddCoinSum >= val)
            {
                AddCoinSum -= val;
                UpdataCb?.Invoke(val);
            }
            else if (AddCoinSum > 0)
            {
                UpdataCb?.Invoke(AddCoinSum);
                AddCoinSum = 0;
            }
            else
            {
                EndEvent();
            }
        }

        public void EndEvent()
        {
            EndEventCb?.Invoke();
            EndEventCb = null;
        }

        public void UICloseEvent()
        {
            UICloseEventCb?.Invoke();
        }

        public void Dispose()
        {
            UpdataCb = null;
            EndEventCb = null;
            UICloseEventCb = null;
        }
    }
}