using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PArena
{
    public enum Pony
    {
        Twily,
        Pinkie,
        RD,
        Flutty,
        Rarity,
        AJ
    } 

    public class Player: GameObject
    {

        
        static float timeToFire = 0;
        public bool canFire { get { return timeToFire == 0; } }
        Type BulletType;
        //Pony enumpony;
        public float hitpoints;
        public float maxHitpoints = 100;
        public bool isDead;

        public float damageMul = 1;
        public float rof = 1;
        float deadAnimationTime = 2.0f;
        int nSpot = 20;
        float spotTime;
        public Pony pony;


        public Player(Vector2 pos, Vector2 dir, Pony pony, float speed = 300)
            : base(pos, dir, null, speed)
        {
            hitpoints = maxHitpoints;
            //enumpony = Pony.AJ;
            switch (pony)
            {
                case Pony.Pinkie:
                    break;
                case Pony.RD:
                    rof = 0.9f;
                    timeToFire = 0;
                    BulletType = typeof(Bullet_Rail);
                    sprite = Cnt.game.playerTextures[4];
                    speed = 350;
                    break;
                case Pony.Flutty:
                    rof = 0.17f;
                    timeToFire = 0;
                    BulletType = typeof(Bullet_Gun);
                    sprite = Cnt.game.playerTextures[1];
                    break;
                case Pony.Rarity:
                    rof = 0.35f;
                    timeToFire = 0;
                    BulletType = typeof(Bullet_Rifle);
                    sprite = Cnt.game.playerTextures[3];
                    break;
                case Pony.AJ:
                    rof = 0.9f;
                    timeToFire = 0;
                    BulletType = typeof(Bullet_Shotgun);
                    sprite = Cnt.game.playerTextures[0];
                    break;
                case Pony.Twily:
                    rof =0.5f;
                    timeToFire = 0;
                    BulletType = typeof(Bullet_Guide);
                    sprite = Cnt.game.playerTextures[5];
                    break;
                default:
                    break;
            }
            this.pony = pony;
            Height = sprite.Height;
            Width = sprite.Width;
            origin = new Vector2(Width / 2, Height / 2);
        }
        /*
        public override void Draw(SpriteBatch sb float offX = 0, float offY = 0)
        {            
            base.Draw(sb);
        }
        */
        public override void Update(GameTime gt)
        {
            float elapsed = (float)gt.ElapsedGameTime.TotalSeconds;
            Vector2 newPos = Pos + Dir * speed * elapsed;
            if (newPos.X < 0 || newPos.X > Game1.screenWidth) Dir.X = 0;
            if (newPos.Y < 0 || newPos.Y > Game1.screenHeight) Dir.Y = 0;

            base.Update(gt);
        }
        
        public void Update(GameTime gt, KeyboardState kstate)
        {

            Dir = Vector2.Zero;
            if (kstate.IsKeyDown(Keys.A)) Dir.X--;
            if (kstate.IsKeyDown(Keys.D)) Dir.X++;
            if (kstate.IsKeyDown(Keys.W)) Dir.Y--;
            if (kstate.IsKeyDown(Keys.S)) Dir.Y++;


            timeToFire = MathHelper.Clamp(timeToFire - (float)gt.ElapsedGameTime.TotalSeconds, 0, rof);
            Update(gt);
            //Pos += Vel * (float)gt.ElapsedGameTime.TotalSeconds;
            if (isDead)
            {
                spotTime += (float)gt.ElapsedGameTime.TotalSeconds;
                if (spotTime > deadAnimationTime / nSpot)
                {
                    spotTime = 0;
                    Vector2 randomPos = new Vector2(Pos.X + (float)Game1.rnd.NextDouble() * 300 - 150, Pos.Y + (float)Game1.rnd.NextDouble() * 300 - 150);
                    //Vector2 randomPos = Pos;
                    int randomTex = Game1.rnd.Next(Cnt.game.spotCount);
                    Spot sp = new Spot { rect = new Rectangle(randomTex * 80, 0, 80, 80), pos = randomPos, color = Color.DarkRed };
                    Cnt.game.spots.Add(sp);
                }
            }

        }


        public ShakeParam Shoot(float mx, float my)
        {
            ShakeParam sh=new ShakeParam();
            if (BulletType == typeof(Bullet_Shotgun))
            {
                for (int i = 0; i < 13; i++)
                {
                    Bullet bullet = (Bullet)Activator.CreateInstance(BulletType, new object[] { Pos, new Vector2(mx, my) - Pos, Cnt.game.bulletTex, damageMul });
                    Cnt.game.Bullets.Add(bullet);
                    sh = bullet.getShakeParam;
                    
                }
                Bullet tmpb = (Bullet)Activator.CreateInstance(BulletType, new object[] { Pos, new Vector2(mx, my) - Pos, Cnt.game.bulletTex, damageMul });
                tmpb.soundfx.Play();
            }
            else
            {
                Bullet bullet = (Bullet)Activator.CreateInstance(BulletType, new object[] { Pos, new Vector2(mx, my) - Pos, Cnt.game.bulletTex, damageMul });
                Cnt.game.Bullets.Add(bullet);
                sh = bullet.getShakeParam;
                bullet.soundfx.Play();
            }
            timeToFire = rof;

            return sh;
        }

        public void Die()
        {
            //isDead = true;
        }
    }
}
