using System;
using System.Collections.Generic;
using Game.JNGState.Logic.Data;
using JNGame.Sync.System;
using JNGame.Sync.View;
using UnityEngine;

namespace Game.JNGFrame.View
{
    
    public abstract class VDEntityBasis<T> : ViewData<T> where T : IGDataBase
    {

        private readonly Dictionary<int, Action<(GameObject View, T Data)>> Event = new();
        
        protected VDEntityBasis(SViewSystem root) : base(root)
        {
        }

        public override void Execute()
        {
            base.Execute();
            
            //调用改动
            var dataList = GetData();
            
            foreach (var data in dataList){
                
                if (views.TryGetValue(data.Id,out var view))
                {
                    data.WriteCodes.ForEach(code =>
                    {
                        Event[code]?.Invoke((view.Data,data));
                    });
                }
            }
            
        }

        public override void ViewUpdate(T data, GameObject view)
        {
            
        }

        public void Register(int code,Action<(GameObject View, T Data)> action)
        {
            Event[code] = action;
        }
        
        
    }
}