using Otter;
using Puppyups.Utilities;
using System;

namespace Puppyups.objects
{ 
    class Player2Aimer : Entity
    {
        private Spritemap<string> _spriteMap;
        private double angle = Rand.Angle;
        public static bool IsActive;
        private bool _isThereAGrapple;
        private string _direction;

        public Player2Aimer(string direction)
        {
            _direction = direction;
            _spriteMap = new Spritemap<string>(Assets.PLAYERTWOAIMERS, 10, 10);
            _spriteMap.Add("main", new int[] { 0, 1, 2, 3 }, new float[] { 7f, 7f });
            _spriteMap.Play("main");
            _spriteMap.CenterOrigin();
            Graphic = _spriteMap;
            IsActive = true;
            SoundManager.PlayRequestedSound("_p2aimer");

            if (_direction == "up")
                angle = Math.PI / 2;
            if (_direction == "left")
                angle = 2 * Math.PI;
            if (_direction == "right")
                angle = Math.PI;
            if (_direction == "down")
                angle = 3 * Math.PI / 2;
        }

        public override void Update()
        {
            base.Update();
            X = Global.Player2X - 30 * (float)Math.Cos(angle);
            Y = Global.Player2Y - 30 * (float)Math.Sin(angle);
            var cn = Global.Player2Session.Controller;
            //these are the main directions:
            if (cn.Button("Left").Pressed)//left
                angle = 2 * Math.PI;//6.26
            if (cn.Button("Up").Pressed)//up
                angle = Math.PI / 2;//1.02
            if (cn.Button("Down").Pressed)//down
                angle = 3 * Math.PI / 2;//4.5
            if (cn.Button("Right").Pressed)//right
                angle = Math.PI;//3.14

            if (cn.Button("NumZero").Pressed && !_isThereAGrapple)//fire button of course
            {
                _isThereAGrapple = true;
                Scene.Add(new Grapple2(X, Y, Global.Player2X, Global.Player2Y));
                //Destroy();
            }
            if (angle > 2 * Math.PI)
                angle = 0;
            if (angle < 0)
                angle = 2 * Math.PI;
            if (!IsActive)
            {
                Destroy();
            }

        }

        public void Destroy()
        {
            IsActive = false;
            RemoveSelf();
        }
    }
}
