using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using MikuMikuDance.Motion;

using TImport = MikuMikuDance.Motion.Motion2.MMDMotion2;

namespace MikuMikuDance.XNA.Motion
{
    /// <summary>
    /// MikuMikuDanceのモーションデータをインポートするためのインポータ
    /// </summary>
    [ContentImporter(".vmd", DisplayName = "MikuMikuDance Motion : MikuMikuDance for XNA", CacheImportedData = false, DefaultProcessor = "MMDMotionProcessor")]
    public class MMDMotionImporter : ContentImporter<TImport>
    {
        /// <summary>
        /// インポート処理
        /// </summary>
        /// <param name="filename">ファイル名</param>
        /// <param name="context">コンテントインポータ</param>
        /// <returns>MMDモーションデータ</returns>
        /// <remarks>XNAのインポータから呼び出される</remarks>
        public override TImport Import(string filename, ContentImporterContext context)
        {
            //Identityを設定(コンテンツのロード元の判別等に必要)
            ContentIdentity Identity = new ContentIdentity(filename);
            
            filename = Path.GetFullPath(filename);
            TImport result = MotionManager.Read(filename, CoordinateType.LeftHanded) as TImport;
            if (result == null)
                throw new InvalidContentException();
            if (result.Motions == null && result.LightMotions == null && result.FaceMotions == null && result.CameraMotions == null)
                throw new InvalidContentException();
            return result;
        }
    }
}
