﻿using System;
using System.Collections.Generic;
using Common;
using wServer.realm;

namespace wServer.networking.data
{
    public struct ObjectStatusData
    {
        public int ObjectId;
        public WorldPosData Position;
        public KeyValuePair<StatData, object>[] Stats;

        public static ObjectStatusData Read(Client psr, NReader rdr)
        {
            ObjectStatusData ret = new ObjectStatusData();
            ret.ObjectId = rdr.ReadInt32();
            ret.Position = WorldPosData.Read(psr, rdr);
            ret.Stats = new KeyValuePair<StatData, object>[rdr.ReadInt16()];
            for (int i = 0; i < ret.Stats.Length; i++)
            {
                StatData type = (StatData)rdr.ReadByte();
                if (type.IsStringStat())
                    ret.Stats[i] = new KeyValuePair<StatData, object>(type, rdr.ReadUTF());
                else
                    ret.Stats[i] = new KeyValuePair<StatData, object>(type, rdr.ReadInt32());
            }
            return ret;
        }

        public void Write(Client psr, NWriter wtr)
        {
            try
            {
                wtr.Write(ObjectId);
                Position.Write(psr, wtr);
                wtr.Write((ushort)Stats.Length);
                foreach (var i in Stats)
                {
                    wtr.Write((byte)i.Key);
                    if (i.Key.IsStringStat() && i.Value != null)
                        wtr.WriteUTF(i.Value.ToString());
                    else
                        wtr.Write((int)i.Value);
                }
            }
            catch (Exception) { }
        }
    }
}