using Cirno.Scripts.Interactables;
using Cirno.Scripts.Resources.ItemEffects;
using Godot;

namespace Cirno.Scripts.Resources;

[GlobalClass]
[Tool]
public partial class LootItem : Resource
{
    [Export] public StringName ItemName { get; set; }
    [Export] public StringName ShortName { get; set; }
    [Export] public StringName ItemDescription { get; set; }
    [Export] public StringName ItemKey { get; set; }
    [Export] public ItemTypes Item;
    [Export] public int Tier { get; set; } = 0;
    [Export] public int Price { get; set; }
    [Export] public ItemEffectResource ItemEffect { get; private set; }
    [Export] public WeaponResource WeaponData { get; set; }
    [Export] public int Amount;
    [Export] public int Max;
    [Export] public bool PickupIfMaxed;
    [Export] public bool ConsumeOnUse;

    [Export(PropertyHint.Flags, "Icon,Count,Ammo,Energy")]
    public UiItemType UiType { get; set; } = 0;
    [Export] public bool Selectable;
    [Export] public bool AutoPickup { get; private set; } = false;
    [Export] public Texture2D InventorySprite;
    //[Export] public SpriteFrames WorldSprite;
    //[Export] public PackedScene HudItemScene;
    [Export(PropertyHint.File)] public StringName DropScenePath { get; private set; } // Has to be a string path to avoid recursion issues

    public ItemPickup Spawn(Node2D sibling, bool dropAsChild = false)
    {
        if (string.IsNullOrWhiteSpace(DropScenePath)) return null;
        var itemScene = GD.Load<PackedScene>(DropScenePath);
        var spawnedItem = dropAsChild ? sibling.CreateChild<ItemPickup>(itemScene) : sibling.CreateSibling<ItemPickup>(itemScene);
        
        spawnedItem.Name = this.ItemKey;
        
        spawnedItem.LootTable.Add(this);
        spawnedItem.SetSprite(InventorySprite);

        return spawnedItem;
    }
}