using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightGame
{
    public class AnimationData
    {
        public string[] frames = null;
        public double fps = 0;
        public int width = 0;
        public int height = 0;

        public AnimationData(string[] frames, double fps)
        {
            this.frames = frames;
            this.fps = fps;
        }
    }

    /*
        A game object with an animated bitmap image
    */
    public class AnimatedGameObject : GameObject
    {
        protected double timeSinceLastFrame = 0;
        protected int currentFrame = 0;
        protected AnimationData animationData = null;

        public AnimatedGameObject startupAnimatedGameObject(Point dimensions, AnimationData animationData, int zLayer)
        {
            // initialise variables
            this.animationData = animationData;
            currentFrame = 0;
            timeSinceLastFrame = 0;

            base.startupGameObject(dimensions, animationData.frames[currentFrame], zLayer);

            return this;
        }

        public override void shutdown()
        {
            (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children.Remove(rect);
            rect = null;
            base.shutdown();
        }

        public override void enterFrame(double dt)
        {
            base.enterFrame(dt);

            timeSinceLastFrame += dt;

            if (timeSinceLastFrame >= 1 / animationData.fps)
            {
                // we don't get a render event if the page is not displayed. Using this while
                // loop means that if we come back to the game after browsing another page
                // the animations don't go crazy
                while (timeSinceLastFrame >= 1 / animationData.fps)
                {
                    timeSinceLastFrame -= 1 / animationData.fps;
                    ++currentFrame;
                    currentFrame %= animationData.frames.Length;
                }

                prepareImage(animationData.frames[currentFrame]);
            }
        }

        override protected void prepareImage(string image)
        {
            // get the embedded image and display it on the rectangle
            imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = ResourceHelper.GetBitmap(image);
            rect.Fill = imageBrush;
        }
    }


}