using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Newtonsoft.Json;
using RecrownedAthenaeum.Data;
using System.IO;

namespace RecrownedAthenaeum.Pipeline.TextureAtlas
{
    [ContentTypeWriter]
    class TextureAtlasWriter : ContentTypeWriter<TextureAtlasData>
    {
        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "RecrownedAthenaeum.ContentReaders.TextureAtlasDataReader, RecrownedAthenaeum";
        }

        protected override void Write(ContentWriter output, TextureAtlasData value)
        {
            output.Write(Path.GetFileNameWithoutExtension(value.textureName));
            output.Write(value.regions.Length);

            for (int i = 0; i < value.regions.Length; i++)
            {
                output.Write(value.regions[i].name);
                output.Write(value.regions[i].bounds.X);
                output.Write(value.regions[i].bounds.Y);
                output.Write(value.regions[i].bounds.Width);
                output.Write(value.regions[i].bounds.Height);
                bool hasNPatch = value.regions[i].ninePatchData != null;
                output.Write(hasNPatch);
                if (hasNPatch)
                {
                    output.Write(value.regions[i].ninePatchData.left);
                    output.Write(value.regions[i].ninePatchData.right);
                    output.Write(value.regions[i].ninePatchData.bottom);
                    output.Write(value.regions[i].ninePatchData.top);
                }
            }
        }
    }
}
