using System.Collections.Generic;
using Decimation.Lib.Util;
using Terraria;
using Terraria.Audio;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;

namespace Decimation.Lib.Items
{
    public abstract class DecimationItem : ModItem
    {
        protected abstract string ItemName { get; }
        protected virtual string ItemTooltip { get; } = "";
        protected virtual DrawAnimation Animation { get; } = null;

        public sealed override void SetStaticDefaults()
        {
            this.DisplayName.SetDefault(this.ItemName);
            this.Tooltip.SetDefault(this.ItemTooltip);

            if (this.Animation != null) Main.RegisterItemAnimation(this.item.type, this.Animation);
        }

        public sealed override void SetDefaults()
        {
            this.item.height = 16;
            this.item.width = 16;
            this.item.useAnimation = 20;
            this.item.useTime = 20;
            this.item.useStyle = 1;
            this.item.maxStack = 999;
            this.item.noMelee = true;
            this.item.UseSound = SoundID.Item1;

            Init();
        }

        public sealed override void AddRecipes()
        {
            List<ModRecipe> recipes = GetRecipes();
            recipes.Add(GetRecipe());

            foreach (ModRecipe recipe in recipes) recipe?.AddRecipe();
        }

        protected abstract void Init();

        protected virtual ModRecipe GetRecipe()
        {
            return null;
        }

        protected virtual List<ModRecipe> GetRecipes()
        {
            return new List<ModRecipe>();
        }

        protected static ModRecipe GetNewModRecipe(DecimationItem result, int quantity, int tile,
            bool anyIronBar = false)
        {
            return GetNewModRecipe(result, quantity, new List<int> {tile}, anyIronBar);
        }

        protected static ModRecipe GetNewModRecipe(DecimationItem result, int quantity, List<int> tiles,
            bool anyIronBar = false)
        {
            ModRecipe recipe = new ModRecipe(References.mod) {anyIronBar = anyIronBar};

            recipe.SetResult(result, quantity);

            foreach (int tile in tiles) recipe.AddTile(tile);

            return recipe;
        }
    }
}