using Otter;
using PenniesAreGold.objects;
using PenniesAreGold.utility;
using PenniesAreGold.Utility;
using System;

namespace PenniesAreGold.player
{
    public class Player : Entity
    {
        private Spritemap<string> _spriteMap;
        //private sound death = new Sound(Assets.SFX_DEATH); //todo
        private bool _hMove = true;
        private bool _vMove = true;
        public static bool canPlayerCollide = true;
        public static bool isSpritemapChanged = true;
        private BasicMovement movement = new BasicMovement(400, 400, 400);//was 400,400,400

        public Player(float x, float y)
        {
            X = x; Y = y;
            SetHitbox(30, 20, (int)Global.Type.PLAYER);
            Collider.X = -13;
            Collider.Y = 0;
            Axis axis = Axis.CreateWASD();
            movement.Axis = axis;
            movement.Collider = Collider;
            movement.AddCollision((int)Global.Type.SOLID);//walls
            AddComponents(axis, movement);
            LoadCorrectSpritemap();
            _spriteMap.CenterOrigin();
            movement.Speed = Global.PlayerSpeed;
        }

        public override void UpdateFirst()//this is so that when the player moves the camera, the UI will update after it moves. (but the player will still appear below the elements. (it's an update thing)
        {
            base.UpdateFirst();
            #region Camera follows player
            if (X > Scene.HalfWidth && X < Global.MW - Scene.HalfWidth)//this is now universal. haha!
                Scene.CameraX = X - Scene.HalfWidth;//this does follow the player!
            else if (X <= Scene.HalfWidth)
                Scene.CameraX = 0;
            else if (X >= Global.MW - Scene.HalfWidth * 2)
                Scene.CameraX = Global.MW - Scene.HalfWidth * 2;
            if (Y > Scene.HalfHeight && Y < Global.MH - Scene.HalfHeight)
                Scene.CameraY = Y - Scene.HalfHeight;//this does follow the player!
            else if (Y <= Scene.HalfHeight)
                Scene.CameraY = 0;
            else if (Y >= Global.MH - Scene.HalfHeight * 2)
                Scene.CameraY = Global.MH - Scene.HalfHeight * 2;
            #endregion
        }

        public override void Update()
        {
            base.Update();
            var numberOfTroops = GetEntities<TroopBuilding>();
            _spriteMap.Scale = numberOfTroops.Count/9 + 1;
            #region override edges
            if (X < -8)
                X = -8;
            if (X > Global.MW)
                X = Global.MW;
            if (Y < 56)
                Y = 56;
            if (Y > Global.MH)
                Y = Global.MH;
            #endregion

            var cn = Global.PlayerSession.Controller;
            Global.PlayerX = this.X;
            Global.PlayerY = this.Y;
            if (cn.Button("R").Down)//testing!
            {
                //Console.WriteLine("x: {0} y: {1}", X, Y);
                //Scene.Add(new Fuel(Global.MW + 150, Global.MH + 200));
                //Console.WriteLine("testing! R was pressed.");
                //Scene.Add(new Penny(Scene.MouseX, Scene.MouseY));
                //Scene.Add(new ScaryGirl(Scene.MouseX, Scene.MouseY));
                //Scene.Add(new EnemyTroop(Scene.MouseX, Scene.MouseY));
            }
            if (cn.Button("E").Down)//testing!
            {
                //Global.numberOfCoins += 10;                
            }

            PressKeysForAnimation(cn, "walkLeft", "walkRight", "walkUp", "walkDown");

            #region Hmovement or Vmovement
            if (cn.Button("A").Down)//LEFT
                _hMove = true;
            else if (cn.Button("D").Down)//RIGHT
                _hMove = true;
            else
                _hMove = false;
            if (cn.Button("W").Down)//UP
                _vMove = true;
            else if (cn.Button("S").Down)//DOWN
                _vMove = true;
            else
                _vMove = false;
            #endregion
            StopWalkingAnimations();
            #region Collisions
            #region CollideWithPenny
            var collPenny = Collider.Collide(X, Y, (int)Global.Type.PENNY);
            if (collPenny != null)
            {
                    Penny p = (Penny)collPenny.Entity;
                    p.RemoveSelf();
                    SoundManager.PlayRequestedSound("_pulse", 2f);
                    Global.numberOfCoins++;
                    Global.totalNumberOfCoins++;
                    Global.Score += 10;
            //        _spriteMap.Play("pickup");
            //        SoundManager.PlayRequestedSound("_pickup");
            //        SoundManager.PlayRequestedSound("_pickupFUEL");
            //        animDone = false;
            }
            //}
            //var collGenerator = Collider.Collide(X, Y, (int)Global.Type.GENERATOR);
            //if (collGenerator != null)
            //{
            //    if (personalStorage > 0)
            //    {
            //        Generator g = (Generator)collGenerator.Entity;
            //        g._fuel++;
            //        personalStorage--;
            //        _spriteMap.Play("dropoff");
            //        SoundManager.PlayRequestedSound("_pickup");
            //        animDone = false;
            //    }
            //}
            //if (Global.PlayerHealth <= 0)
            //{
            //    Destroy();
            //    Scene.Add(new PlayerDeath(X, Y));
            //}
            #endregion
            #endregion
            #region Death... is there death?
            if (Global.PlayerHealth <= 0)
            {
                RemoveSelf();
                NoSound.music.Stop();
                Global.GAME.RemoveScene();
                Global.GAME.AddScene(new LoseScreen());
            }
            #endregion
        }//END OF UPDATE
        public void LoadCorrectSpritemap()
        {
            isSpritemapChanged = false;
            _spriteMap = null;
            _spriteMap = new Spritemap<string>(Assets.PLAYER, 45, 50);
            AddNewSprites();
            _spriteMap.Update();
            _spriteMap.Play("standDown");
            Graphic = _spriteMap;
        }
        #region AddSprites
        public void AddNewSprites()
        {
            var hMs = 5f;//horizontal animation speed
            var vMs = 5f;//vertical animation speed
            _spriteMap.Add("standRight", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standLeft", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standUp", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("standDown", new int[] { 0 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkRight", new int[] { 0, 1, 2, 3 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkLeft", new int[] { 0, 1, 2, 3 }, new float[] { hMs, hMs });
            _spriteMap.Add("walkUp", new int[] { 0, 1, 2, 3 }, new float[] { vMs, vMs });
            _spriteMap.Add("walkDown", new int[] { 0, 1, 2, 3 }, new float[] { vMs, vMs });

            _spriteMap.Add("pickup", new int[] { 16, 16, 16 }, new float[] { vMs, vMs });
            _spriteMap.Add("dropoff", new int[] { 16, 16, 16 }, new float[] { vMs, vMs });
            _spriteMap.Add("hit", new int[] { 1, 2, 6, 5 }, new float[] { vMs, vMs });
        }
        #endregion
        public void StopWalkingAnimations()
        {
            if (!_hMove && !_vMove)
            {
                switch (_spriteMap.CurrentAnim)
                {
                    case ("walkLeft"):
                        _spriteMap.Play("standLeft");
                        break;
                    case ("walkRight"):
                        _spriteMap.Play("standRight");
                        break;
                    case ("walkUp"):
                        _spriteMap.Play("standUp");
                        break;
                    case ("walkDown"):
                        _spriteMap.Play("standDown");
                        break;
                    default:
                        break;
                }
            }
        }

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

        public void PressKeysForAnimation(Controller cn, string l, string r, string u, string d)
        {
            #region pressed Keys for animation
            if (cn.Button("A").Pressed || cn.Button("Left").Pressed)//LEFT
                _spriteMap.Play(l);
            else if (cn.Button("D").Pressed || cn.Button("Right").Pressed)//RIGHT
                _spriteMap.Play(r);
            else if (cn.Button("W").Pressed || cn.Button("Up").Pressed)//UP
                _spriteMap.Play(u);
            else if (cn.Button("S").Pressed || cn.Button("Down").Pressed)//DOWN
                _spriteMap.Play(d);
            #endregion

            #region released keys for animation
            if (cn.Button("A").Released || cn.Button("Left").Released)//if i release left, updownright
            {
                if (cn.Button("W").Down || cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
                else if (cn.Button("S").Down || cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
                else if (cn.Button("D").Down || cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
            }
            else if (cn.Button("D").Released || cn.Button("Right").Released)//if i release right, updownleft
            {
                if (cn.Button("W").Down || cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
                else if (cn.Button("S").Down || cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
                else if (cn.Button("A").Down || cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(r);
            }
            else if (cn.Button("W").Released || cn.Button("Left").Released)//if i release up, rightleftdown
            {
                if (cn.Button("D").Down || cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
                else if (cn.Button("A").Down || cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(l);
                else if (cn.Button("S").Down || cn.Button("Down").Down)//and i'm holding down
                    _spriteMap.Play(d);
            }
            else if (cn.Button("S").Released || cn.Button("Down").Released)//if i release down, rightleftup
            {
                if (cn.Button("D").Down || cn.Button("Right").Down)//and i'm holding right
                    _spriteMap.Play(r);
                else if (cn.Button("A").Down || cn.Button("Left").Down)//and i'm holding left
                    _spriteMap.Play(l);
                else if (cn.Button("W").Down || cn.Button("Up").Down)//and i'm holding up
                    _spriteMap.Play(u);
            }
            #endregion
        }

        public override void Render()//mainly for testing colliders :D
        {
            base.Render();
            //Collider.Render();
        }

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