using Decimation.Lib.Items;
using Terraria;
using Terraria.ModLoader;

namespace Decimation.Content.Projectiles
{
    public abstract class DecimationProjectile : ModProjectile
    {
        private static readonly int FramesPerSeconds = 60;
        
        protected virtual int Damages { get; set; }
        protected virtual DecimationWeapon.DamageType DamageType { get; set; } = DecimationWeapon.DamageType.Melee;
        protected virtual int AnimationFrames { get; } = -1;
        protected virtual int AnimationFps { get; } = 5;

        private int _frameTime;

        protected abstract void Init();

        public override void SetStaticDefaults()
        {
            if (AnimationFrames >= 0) Main.projFrames[projectile.type] = AnimationFrames;
        }

        public sealed override void SetDefaults()
        {
            _frameTime = FramesPerSeconds / AnimationFps;
            
            this.projectile.aiStyle = 1;
            this.projectile.height = 16;
            this.projectile.width = 16;
            this.projectile.hostile = false;
            this.projectile.friendly = !this.projectile.hostile;
            this.projectile.ignoreWater = false;
            this.projectile.light = 0f;
            this.projectile.penetrate = 0;
            this.projectile.tileCollide = true;
            this.projectile.timeLeft = 180;

            Init();

            this.projectile.damage = Damages;

            switch (DamageType)
            {
                case DecimationWeapon.DamageType.Melee:
                    this.projectile.melee = true;
                    break;
                case DecimationWeapon.DamageType.Magic:
                    this.projectile.magic = true;
                    break;
                case DecimationWeapon.DamageType.Ranged:
                    this.projectile.ranged = true;
                    break;
                case DecimationWeapon.DamageType.Throw:
                    this.projectile.thrown = true;
                    break;
                default:
                    this.projectile.melee = true;
                    break;
            }
        }

        public override bool PreAI()
        {
            if (++projectile.frameCounter >= _frameTime)
            {
                projectile.frameCounter = 0;

                if (++projectile.frame >= AnimationFrames) projectile.frame = 0;
            }

            return base.PreAI();
        }
    }
}