using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MikuMikuDance.XNA.Stages
{
    class LightMotionTrack
    {
        public MMDStageMotion motion;
        Stopwatch timer = new Stopwatch();
        decimal frame = 0;
        decimal beforeMS = 0;
        decimal LossTime = 0;
        bool isEmpty = true;
        bool isLoopPlay;

        public bool IsEmpty { get { return isEmpty; } set { isEmpty = value; } }
        public bool IsLoopPlay { get { return isLoopPlay; } set { isLoopPlay = value; } }
        public void Clear() { motion = null; timer.Reset(); isLoopPlay = false; isEmpty = true; }
        public decimal NowFrame { get; set; }
        public long MaxFrame { get { return motion.MaxFrame; } }

        public void Stop()
        {
            if (!timer.IsRunning)
                return;
            timer.Stop();
            LossTime = (decimal)timer.Elapsed.TotalMilliseconds - beforeMS;//ロスタイムを計測
        }

        public void Start()
        {
            if (timer.IsRunning)
                return;
            timer.Start();
            beforeMS = (decimal)timer.Elapsed.TotalMilliseconds - LossTime;
        }
        public void Reset()
        {
            timer.Reset();
            beforeMS = 0;
            LossTime = 0;
            frame = 0;
            NowFrame = 0;
        }
        public bool IsPlay
        {
            get
            {
                return timer.IsRunning;
            }
        }
        public void UpdateFrame(decimal FramePerSecond)
        {
            //現在時刻の取得
            decimal nowMS = (decimal)timer.Elapsed.TotalMilliseconds;
            //前回コール時からの経過フレーム数を取得
            decimal dframe = (nowMS - beforeMS) * FramePerSecond / 1000.0m;
            //フレーム数の更新
            frame += dframe;
            beforeMS = nowMS;

            if (frame > motion.MaxFrame && !isLoopPlay)
            {
                Stop();
                NowFrame = (decimal)motion.MaxFrame;
            }
            NowFrame = frame % (decimal)(motion.MaxFrame + 1);
        }

        public void ApplyMotion(IMMDLightManager mmdLight)
        {
            Vector3 v;
            Color color;
            motion.GetLightData(NowFrame, out v, out color);
            mmdLight.KeyLight = new MMDLight() { Direction = v, Type = mmdLight.KeyLight.Type };
            mmdLight.AmbientLight = color;
        }
    }
}
