using Otter;
using Puppyups.Utilities;
using System;

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

        public Player1Aimer(string direction)
        {
            _direction = direction;
            _spriteMap = new Spritemap<string>(Assets.PLAYERONEAIMERS, 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("_p1aimer");

            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.PlayerX - 30 * (float)Math.Cos(angle);
            Y = Global.PlayerY - 30 * (float)Math.Sin(angle);
            var cn = Global.PlayerSession.Controller;
            //these are the main directions:
            if (cn.Button("A").Pressed)//left
                angle = 2 * Math.PI;//6.26
            if (cn.Button("W").Pressed)//up
                angle = Math.PI / 2;//1.02
            if (cn.Button("S").Pressed)//down
                angle = 3 * Math.PI / 2;//4.5
            if (cn.Button("D").Pressed)//right
                angle = Math.PI;//3.14
            //these are the focus directions after selection
            /*if (cn.Button("A").Down)//Left   SURprisingly Working
            {
                if (angle >= Math.PI / 4 && !_pastZero)
                {
                    anglePlus = false;
                }
                if (anglePlus && angle == 2 * Math.PI)
                    _pastZero = false;
                if (!anglePlus && angle == 2 * Math.PI)
                    _pastZero = true;
                if (angle <= 7 * Math.PI / 4 && _pastZero)
                {
                    anglePlus = true;
                }
            }
            if (cn.Button("W").Down)//Up      WORKING
            {
                if (angle >= 3 * Math.PI / 4)
                    anglePlus = false;
                if (angle <=  Math.PI / 4)
                    anglePlus = true;
            }
            if (cn.Button("D").Down)//Right       WORKING
            {
                if (angle >= 5 * Math.PI / 4)
                    anglePlus = false;
                if (angle <= 3 * Math.PI / 4)
                    anglePlus = true;
            }
            if (cn.Button("S").Down)//Down       WORKING
            {
                if (angle >= 7 * Math.PI / 4)
                    anglePlus = false;
                if (angle <= 5 * Math.PI / 4)
                    anglePlus = true;
            }*/
            if (cn.Button("J").Pressed && !_isThereAGrapple)//fire button of course
            {
                _isThereAGrapple = true;
                Scene.Add(new Grapple(X, Y, Global.PlayerX, Global.PlayerY));
                //Destroy();
            }
            //this is direction it goes.
            /*if (cn.Button("W").Down || cn.Button("A").Down || cn.Button("S").Down || cn.Button("D").Down)//if holding the button. move!
            {
                if (anglePlus)
                    angle += .05f;
                else
                    angle -= .05f;
            }*/
            //Console.WriteLine("angle: {0}", angle);
            //this keeps it managable. Can't get anything over 2PI
            if (angle > 2 * Math.PI)
                angle = 0;
            if (angle < 0)
                angle = 2 * Math.PI;
            if (!IsActive)
            {
                Destroy();
            }

        }

        public void Destroy()
        {
            RemoveSelf();
        }
    }
}
