using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace EduNetworkBuilder
{
    //Holds a little bit of information pertaining to the current animation.
    [Serializable]
    public class AnimationClass
    {
        Point ImageStartPoint = new Point();
        Rectangle Where;
        Size HowBig; //how big is each animation.
        public DateTime NextAnimation;
        int AnimationCount = 0;
        int AnimationSpeed = 150; //ms between animations
        int maxAnim = 12;
        bool erased = true;

        public bool AnimationDone { get { if (AnimationCount > 0) return false; return true; } }
        public bool FinalErase = false;

        public AnimationClass(AnimationName What, Rectangle where)
        {
            Where = where;
            switch(What)
            {
                case AnimationName.Fire1:
                    ImageStartPoint = new Point(0,0);
                    HowBig = new Size(100, 100);
                    AnimationCount = 12;
                    maxAnim = 12;
                    AnimationSpeed = 100;
                    break;
                case AnimationName.Explo1:
                    ImageStartPoint = new Point(0, 400);
                    HowBig = new Size(100, 100);
                    AnimationCount = 12;
                    maxAnim = 12;
                    AnimationSpeed = 100;
                    break;
                case AnimationName.Smoke1:
                    ImageStartPoint = new Point(0, 200);
                    HowBig = new Size(100, 100);
                    AnimationCount = 6;
                    maxAnim = 6;
                    break;
                case AnimationName.Spark1:
                    ImageStartPoint = new Point(0, 300);
                    HowBig = new Size(100, 100);
                    AnimationCount = 6;
                    maxAnim = 6;
                    break;
                case AnimationName.Lightning1:
                    ImageStartPoint = new Point(0, 600);
                    HowBig = new Size(100, 100);
                    AnimationCount = 6;
                    maxAnim = 6;
                    break;
            }
            NextAnimation = DateTime.UtcNow.AddMilliseconds(AnimationSpeed);
        }

        public void DrawAnimation(Image TheNetworkImage, PictureBox myPB)
        {
            if (AnimationCount < 0) return;
            if (!erased) return;
            int sx = (maxAnim - AnimationCount) * HowBig.Width;
            int sy = ImageStartPoint.Y;
            if(sx >= Properties.Resources.Animations.Width)
            {
                sy = sy + HowBig.Height;
                sx = sx - Properties.Resources.Animations.Width;
            }
            //Console.WriteLine("Anim: x:" + sx + "  y:" + sy);
            Rectangle AniminSnip = new Rectangle(sx, sy, HowBig.Width, HowBig.Height);
            Graphics.FromImage(TheNetworkImage).DrawImage(Properties.Resources.Animations, Where, AniminSnip, GraphicsUnit.Pixel);
            myPB.Invalidate(Where);
        }

        public void EraseAnimation(Image TheNetworkImage, PictureBox myPB, Image TheNetworkImageBackground)
        {
            if (DateTime.UtcNow >= NextAnimation)
            {
                //Console.WriteLine("\nThis happened at: " + DateTime.UtcNow.ToString("mm:ss.fff tt"));
                if (TheNetworkImageBackground == null) return;//do nothing if no animation set
                Graphics.FromImage(TheNetworkImage).DrawImage(TheNetworkImageBackground, Where, Where, GraphicsUnit.Pixel);
                myPB.Invalidate(Where);
                erased = true;
                NextAnimation = DateTime.UtcNow.AddMilliseconds(AnimationSpeed);
                //Console.WriteLine("next: "+ NextAnimation.ToString("mm:ss.fff tt"));
                AnimationCount--;
            }
        }
    }
}
