using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wordinator;

namespace Bombing_Math
{
    public class classFallingPrize
    {
        public static List<classFallingPrize> lst = new List<classFallingPrize>();

        public Point vel = new Point();
        public Point pt = new Point();
        public bool bolParachuteDeployed = false;
        public bool bolDrawTitle = false;

        bool DrawTitle
        {
            get { return bolDrawTitle; }
            set 
            {
                if (bolDrawTitle != value)
                {
                    bolDrawTitle = value;
                    if (bolDrawTitle)
                    {
                        cEventTicket_ShowBonus.Reschedule();
                    }
                }
            }
        }
       public static void DrawList(ref Graphics g)
        {
            for (int intFallingPrizeCounter = 0; intFallingPrizeCounter < lst.Count; intFallingPrizeCounter ++)
            {
                classFallingPrize cFallingPrize = lst[intFallingPrizeCounter];
                cFallingPrize.Draw(ref g);
                


            }
        }
        public static void MoveList()
        {

            if (formBombingMath.GameIsOver) return;
            for (int intFallingPrizeCounter = 0; intFallingPrizeCounter < lst.Count; intFallingPrizeCounter ++)
            {
                classFallingPrize cFallingPrize = lst[intFallingPrizeCounter];
                cFallingPrize.Move();
            }
        }


        public classEventController.classEventTicket cEventTicket_ShowBonus = null;
        static Random rnd = new Random();
        public classFallingPrize(ref classMathProblem cProblem)
        {
            cEventTicket_ShowBonus = formBombingMath.cEC.Ticket("fallingPrize_ShowBonus", 3000, tmrShowBonus_Tick);
            cEventTicket_ShowBonus.quit();
            pt = cProblem.pt;
            eToolType = cProblem.cBonus.eTool;
            vel = new Point(10 - rnd.Next(20), -(10 + rnd.Next(20)));
            lst.Add(this);
        }
        public classFallingPrize()
        {
            cEventTicket_ShowBonus = formBombingMath.cEC.Ticket("fallingPrize_ShowBonus", 3000, tmrShowBonus_Tick);
            cEventTicket_ShowBonus.quit();
            vel = new Point(10 - rnd.Next(20), -(10 + rnd.Next(20)));
            lst.Add(this);
        }



        public enuTools eToolType = enuTools.SpyGlass;
        static Size szImage
        {
            get 
            {
                return formBombingMath.szTool;
            }
        }

        static Bitmap _bmpCrate = null;
        static Bitmap bmpCrate
        {
            get
            {
                if (_bmpCrate ==null)
                {
                    _bmpCrate = new Bitmap(Properties.Resources.crate);
                    _bmpCrate.MakeTransparent(_bmpCrate.GetPixel(0, 0));
                }
                return _bmpCrate;
            }
        }
        static Bitmap _bmpParachute = null;
        static Bitmap bmpParachute
        {
            get
            {
                if (_bmpParachute ==null)
                {
                    _bmpParachute = new Bitmap(Properties.Resources.parachute);
                    _bmpParachute.MakeTransparent(_bmpParachute.GetPixel(0, 0));
                }
                return _bmpParachute;
            }
        }


        static Size szScreen
        {
            get { return formBombingMath.bmpWorking.Size; }
        }

        void tmrShowBonus_Tick(object sender, EventArgs e)
        {
            formBombingMath.arrTools_Inventory[(int)eToolType]++;

            die();
        }

        void Draw(ref Graphics g)
        {
            Bitmap bmpImage = DrawTitle
                                   ? formBombingMath.arrImages_Tools[(int)eToolType]
                                   : bmpCrate;

            Rectangle recSource = new Rectangle(0,0, bmpImage.Width, bmpImage.Height);
            Rectangle recDest = new Rectangle(pt, formBombingMath.szTool);
            g.DrawImage(bmpImage, recDest, recSource, GraphicsUnit.Pixel);
            if (bolParachuteDeployed && !DrawTitle)
            {
                Point ptDrawParachute = new Point(pt.X + bmpImage.Width / 2 - bmpParachute.Width / 2,
                                                  pt.Y - bmpParachute.Height);
                g.DrawImage(bmpParachute, ptDrawParachute);
            }

        }

        void Move()
        {
            if (bolParachuteDeployed)
            {
                int intMaxSpeed = 5;
                if (vel.Y < intMaxSpeed)
                    vel.Y = intMaxSpeed;
                else
                    vel.Y -= 3;
            }
            else
            {
                vel.Y += 5;
                if (vel.Y > 15)
                    bolParachuteDeployed = true;
            }

            vel.X = (int)(vel.X * .8);


            pt.X += vel.X;
            pt.Y += vel.Y;

            if (pt.X < 0)
            {
                pt.X = 0;
                vel.X *= -1;
            }

            if (pt.X + szImage.Width > szScreen.Width)
            {
                pt.X = szScreen.Width - szImage.Width;
                vel.X *= -1;    
            }

            if (pt.Y +szImage.Height > szScreen.Height)
            {
                pt.Y = szScreen.Height - szImage.Height;
                DrawTitle = true;
            }

        }

        public void die()
        {
            lst.Remove(this);
        }



    }
}
