using System.Linq;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using System.IO;

// TODO: replace these with the processor input and output types.

namespace IEModelProcessor
{
    [ContentProcessor(DisplayName = "Innovation Engine Model Processor")]
    public class IEModelProcessor : Microsoft.Xna.Framework.Content.Pipeline.Processors.ModelProcessor
    {
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            ModelContent processed = base.Process(input, context);

            foreach(ModelMeshContent mesh in processed.Meshes)
                foreach (ModelMeshPartContent meshPart in mesh.MeshParts)
                {
                    string texName = Path.GetFileNameWithoutExtension(meshPart.Material.Textures[meshPart.Material.Textures.Keys.ToList<string>()[0]].Filename);

                    if (texName.Length > 4)
                    {
                        int end = texName.Length - 1;

                        if (texName[end - 1] == '_')
                            if (texName[end - 3] != '_')
                                texName = texName.Remove(end - 1, 2);
                    }

                    meshPart.Tag = texName;
                }

            return processed;
        }
    }
}