using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace AnotherReplayReader.ReplayFile
{
    internal static class ReplayExtensions
    {
        public static string ReadUTF16String(this BinaryReader reader)
        {
            var currentBytes = new List<byte>();
            var lastTwoBytes = Array.Empty<byte>();
            while (true)
            {
                lastTwoBytes = reader.ReadBytes(2);
                if (lastTwoBytes.Length != 2)
                {
                    throw new InvalidDataException();
                }
                if (lastTwoBytes.All(x => x == 0))
                {
                    break;
                }
                currentBytes.AddRange(lastTwoBytes);
            }

            return Encoding.Unicode.GetString(currentBytes.ToArray());
        }

        public static void Write(this BinaryWriter writer, ReplayChunk chunk) => chunk.WriteTo(writer);

        public static void Write(this BinaryWriter writer, ReplayFooter footer) => footer.WriteTo(writer);

        public static void Write(this BinaryWriter writer, Replay replay) => replay.WriteTo(writer);
    }


}