using System;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network.Action;
using Plugins.JNGame.Sync.Frame;
using UnityEngine;

namespace Game.Plugins.App
{
    public class JNGSyncFrame : JNSyncFrame
    {
        protected override void OnSendInput(JNFrameInputs inputs)
        {
            //发送帧数据给服务端
            App.Socket.Send((int)NActionEnum.NSyncFrameInput,inputs);
        }

        protected override async UniTask<JNFrameInfos> OnServerData(int start, int end)
        {
            Debug.Log($"OnServerData - {start}");
            try
            {
                var data = (await App.Api.GetByte($"/sync/frame?start={start}"));
                if (data is { Length: > 0 })
                {
                    JNFrameInfos info = JNFrameInfos.Parser.ParseFrom(data);
                    Debug.Log($"OnServerData - {start} 结束");
                    return info;
                }
            }
            catch(Exception e)
            {
                // ignored
                Debug.LogError(e.Message);
            }

            return new JNFrameInfos();
        }
    }
}