using PSO2SERVER.Database;
using PSO2SERVER.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Text;
using static PSO2SERVER.Models.CharacterStruct;

namespace PSO2SERVER.Protocol.Packets
{
    public class CharacterListPacket : Packet
    {

        // Ninji note: This packet may be followed by extra data,
        // after a fixed-length array of character data structures.
        // Needs more investigation at some point.
        // --- 
        // CK note: Extra data is likely current equipment, playtime, etc.
        // All of that data is currently unaccounted for at the moment.
        //忍者注意:这个包后面可能有额外的数据，
        //在固定长度的字符数据结构数组之后。
        //需要更多的调查。
        // ---
        // CK注:额外的数据可能是当前设备，游戏时间等。
        //所有这些数据目前都是未知的。

        private int AccountId;

        public CharacterListPacket(int AccountId)
        {
            this.AccountId = AccountId;
        }

        #region implemented abstract members of Packet

        public override byte[] Build()
        {
            var writer = new PacketWriter();

            using (var db = new ServerEf())
            {
                var chars = db.Characters
                    .Where(w => w.Account.AccountId == AccountId)
                    .OrderBy(o => o.CharacterID) // TODO: 按照最后游玩的角色排序
                    .Select(s => s);

                writer.Write((uint)chars.Count()); // 写入玩家数量

                writer.Write((uint)0);

                foreach (var ch in chars)
                {
                    //JobParam jobParam = v;
                    //jobParam.mainClass = ClassType.Luster;
                    //jobParam.subClass = ClassType.Phantom;
                    //jobParam.entries.Luster.level = 100;

                    writer.Write((uint)0);

                    writer.Write((uint)ch.CharacterID);//4
                    writer.Write((uint)ch.AccountID);//4
                    writer.Write((uint)ch.Unk1);//4
                    writer.Write((uint)ch.VoiceType);//4
                    writer.Write((ushort)ch.Unk2);//2
                    writer.Write(ch.VoicePitch);//2
                    writer.WriteFixedLengthUtf16(ch.Name, 16);
                    //Logger.WriteInternal("[CHR] 新增名为 {0} 的新角色.", ch.Name);
                    writer.Write((uint)ch.Unk3);
                    writer.WriteStruct(ch.Looks);
                    writer.WriteStruct(ch.Jobs);
                    writer.WriteBytes(0, 148);

                }
                //for (var i = 0; i < 640; i++)
                //    writer.Write((byte)1);

                //for (var i = 0; i < 30; i++)
                //    writer.Write((uint)1);

                //writer.Write(new byte[240]);
                //writer.Write(new byte[240]);
            }

            return writer.ToArray();
        }

        public override PacketHeader GetHeader()
        {
            return new PacketHeader(0x11, 0x03, PacketFlags.None);
        }

        #endregion
    }
}