using Otter;
using System;

namespace PenniesAreGold.objects
{
    class Penny : Entity
    {
        private Spritemap<string> _spriteMap;
        private Rectangle rect;

        private double angleInRadians = 1;
        private double reverseAngleInRadians = 1;
        private int firstTimer = 100;
        private int randomTimer = Rand.Int(1, 50);
        private bool _amICollidingWithCoins = false;

        public Penny(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(3, 3, (int)Global.Type.PENNY);
            Collider.X = -2;
            Collider.Y = 0;
            rect = new Rectangle((int)X, (int)Y, 10, 10);
            _spriteMap = new Spritemap<string>(Assets.PENNY, 10, 10);
            _spriteMap.Add("spin", new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }, new float[] { 5f, 5f });
            _spriteMap.Add("roll", new int[] { 7, 6, 5, 4, 3, 2, 1, 0 }, new float[] { 7f, 7f });
            _spriteMap.Play("spin");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
            angleInRadians = GetAngle(Rand.Float(0f, 359f));
        }

        public override void Update()
        {
            base.Update();
            //Console.WriteLine("currentAnimatino: {0}", _spriteMap.CurrentAnim);
            if (randomTimer <= 0)
            {
                randomTimer = Rand.Int(300, 1000);
                _amICollidingWithCoins = AmICollidingWithCoins(Collider);
            }
            if (_amICollidingWithCoins)
            {
                var speed = .2f;
                X -= speed * (float)Math.Cos(angleInRadians);
                Y -= speed * (float)Math.Sin(angleInRadians);
                ChangeAnimTo("roll");
            }
            //collide with anything else, reverse direction.
            var colSolid = Collider.Collide(X, Y, (int)Global.Type.SOLID);

            if (colSolid != null)
            {
                var speed = 10f;
                X -= speed * (float)Math.Cos(reverseAngleInRadians);
                Y -= speed * (float)Math.Sin(reverseAngleInRadians);
                ChangeAnimTo("roll");
            }

            if (firstTimer > 0)
                firstTimer--;
            if (firstTimer <= 0 && _amICollidingWithCoins)
            {//get a new angle, because you are still colliding.
                angleInRadians = GetAngle(Rand.Float(0f, 359f));
                firstTimer = Rand.Int(3, 40);
            }
            if (randomTimer > 0) randomTimer--;
            //for pesky borders
            if (X < 16)
                X = 16;
            else if (X > Global.MW - 16)
                X = Global.MW - 16;
            if (Y <= 16)
                Y = 16;
            else if (Y > Global.MH - 16)
                Y = Global.MH - 16;

            //end of update
        }
        public bool AmICollidingWithCoins(Collider c)
        {
            var collB = c.Collide(X, Y, (int) Global.Type.PENNY);
            if (collB != null)
                return true;
            else
                return false;
        }

        public double GetAngle(float angle)
        {
            var tempAngle = MathHelper.ToRadians(angle);
            reverseAngleInRadians = MathHelper.ToRadians(angle + 180);
            return tempAngle;
        }

        public void ChangeAnimTo(string desiredAnimation)
        {
            if (_spriteMap.CurrentAnim != desiredAnimation)
                _spriteMap.Play(desiredAnimation);
        }

        public void Destroy()
        {
            RemoveSelf();
        }

        public override void Render()
        {
            base.Render();
            //Collider.Render(Color.Orange);
        }
    }
}

