using Mysqlx.Crud;
using Mysqlx.Session;
using Org.BouncyCastle.Utilities;
using PSO2SERVER.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.Remoting.Contexts;
using System.Text;

namespace PSO2SERVER.Protocol.Packets
{
    public class MovementActionServerPacket : Packet
    {
        /// Player that receives this packet.
        public ObjectHeader receiver { get; set; } = new ObjectHeader();
        /// Object that performed this action.
        public ObjectHeader performer { get; set; } = new ObjectHeader();
        public uint unk3 { get; set; } = 0;
        public byte[] unk4 { get; set; } = new byte[0x10];
        public byte[] unk5 { get; set; } = new byte[0x08];
        public byte[] unk6 { get; set; } = new byte[0x0C];
        public string action { get; set; } = string.Empty;
        public uint unk7 { get; set; } = 0;
        public uint unk8 { get; set; } = 0;
        public List<uint> unk9 { get; set; } = new List<uint>();
        public uint unk10 { get; set; } = 0;

        private readonly byte[] _preData;
        private readonly string _command;
        private readonly byte[] _rest;
        private readonly uint _thingCount;
        private readonly byte[] _things;
        private readonly byte[] _final;

        public MovementActionServerPacket(int user_playerid, ObjectHeader performer, byte[] preData
            , string command, byte[] rest, uint thingCount, byte[] things, byte[] final
            )
        {
            receiver = new ObjectHeader((uint)user_playerid, ObjectType.Player);
            this.performer = performer;
            _preData = preData;
            _command = command;
            _rest = rest;
            _thingCount = thingCount;
            _things = things;
            _final = things;
        }

        #region implemented abstract members of Packet

        public override byte[] Build()
        {
            PacketWriter pkt = new PacketWriter();
            pkt.WriteObjectHeader(receiver);
            pkt.WriteObjectHeader(performer);
            pkt.Write(_preData);
            pkt.WriteAscii(_command, 0x4315, 0x7A);
            pkt.Write(_rest);
            pkt.WriteMagic(_thingCount, 0x4315, 0x7A);
            pkt.Write(_things);
            pkt.Write(_final);

            return pkt.ToArray();
        }

        public override PacketHeader GetHeader()
        {
            return new PacketHeader(0x04, 0x80, PacketFlags.PACKED_OBJECT_RELATED);
        }

        #endregion
    }
}