using System;
using System.Collections.Generic;
using System.Text;
using DotRecast.Core.Collections;
using Entitas;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.Logic.Entity.Contexts;
using JNGame.Math;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Newtonsoft.Json;
using UnityEngine;

namespace Game.JNGState.Logic.Data
{

    [Serializable]
    public class EDPlayerValue : GDataValue
    {
        public int? Auth = null;
    }
    
    public class EDPlayerData : GDataBase<EDPlayerData,EDPlayerValue,EDPlayer>
    {
        public EDPlayerData()
        {
        }

        public EDPlayerData(EDPlayer node) : base(node)
        {
            Value.Auth = node.Controller.Auth;
        }

        public override bool IsEquals(ISData data)
        {
            var node = data as EDPlayerData;
            if (node is null) return false;
            return base.IsEquals(data) && Value.Auth.Equals(node.Value.Auth);
        }

        public override EDPlayerValue GetDifference(ISData diffValue = null)
        {
            var diff = diffValue as EDPlayerData;
            var value = base.GetDifference(diffValue);
            if (value is null || diff is null) return null;
            
            if (diff.Value.Auth is not null) value.Auth = diff.Value.Auth;
            
            return value;
        }

        public override void UData(EDPlayerValue data)
        {
            base.UData(data);
            if (data.Auth is not null) Value.Auth = data.Auth;
        }
    }
    
    
    public class EDPlayerDataSystem : GDataBaseSystem<EDPlayerData,EDPlayer>
    {

        public EDPlayerDataSystem(SStateDataEnum type) : base(type)
        {
        }
        
        public override int NetID => (int)NetDataEnum.EDPlayerData;
        
        public override JNTileContext<EDPlayer> NodeContext => Contexts.GetContext<EDPlayerContext>();


        public override Dictionary<ulong, EDPlayerData> GetLatest()
        {
            var nodes = new Dictionary<ulong, EDPlayerData>();
            NodeContext.GetEntities().ForEach(child =>
            {
                nodes.Add(child.Id,new EDPlayerData(child));
            });
            return nodes;
        }

    }
    
}