using UnityEngine;
using NeonTea.Quakeball.TeaNet.Packets;

namespace NeonTea.Quakeball.Networking.Packets {
    public class PlayerActionPckt : Packet {
        public ulong PlayerId;

        public PlayerAction Action;
        public Serializable Serializable;

        public PlayerActionPckt() { }
        public PlayerActionPckt(PlayerAction action, Serializable serializable, ulong id = 0) {
            Action = action;
            Serializable = serializable;
            PlayerId = id;
        }

        public override void Read(ByteBuffer buffer) {
            PlayerId = buffer.ReadULong();
            switch (buffer.Read()) {
                case 0:
                    Action = PlayerAction.Jump;
                    break;
                case 1:
                    Action = PlayerAction.Shoot;
                    Serializable = new ShootData();
                    Serializable.Read(buffer);
                    break;
            }
        }

        public override void Write(ByteBuffer buffer) {
            buffer.Write(PlayerId);
            buffer.Write((byte)Action);
            if (Action == PlayerAction.Shoot) {
                buffer.Write(Serializable);
            }
        }
    }

    public enum PlayerAction {
        Null = byte.MaxValue,
        Jump = 0,
        Shoot = 1,
    }

    public class ShootData : Serializable {
        public float Pitch;
        public float Yaw;
        public PlayerSyncPacket SyncPckt;

        public ShootData() { }
        public ShootData(float pitch, float yaw, PlayerSyncPacket syncPckt) {
            Pitch = pitch;
            Yaw = yaw;
            SyncPckt = syncPckt;
        }

        public void Read(ByteBuffer buffer) {
            Pitch = buffer.ReadFloat();
            Yaw = buffer.ReadFloat();
            SyncPckt = new PlayerSyncPacket();
            SyncPckt.Read(buffer);
        }

        public void Write(ByteBuffer buffer) {
            buffer.Write(Pitch);
            buffer.Write(Yaw);
            SyncPckt.Write(buffer);
        }
    }
}