using UnityEngine;

[System.Serializable]
public class EquippableItemPrefabAssetData : PrefabAssetModelData
{
    public EquippableItemData EquippableItemData;

    public EquippableItemPrefabAssetData(EquippableItemPrefabAsset assetModel) : base(assetModel)
    {
        EquippableItemData = MapModelToData(assetModel.EquippableItem);
    }
    
    /// <summary>
    /// Function to convert equitable item (current object partial variable) object to data format
    /// </summary>
    /// <param name="equippableItem"></param>
    /// <returns></returns>
    public EquippableItemData MapModelToData(EquippableItem equippableItem)
    {
        return new EquippableItemData(equippableItem);
    }
    
    /// <summary>
    /// Function to convert EquippableItem Asset format to data format
    /// </summary>
    /// <param name="assetModel"></param>
    /// <returns></returns>
    public override PrefabAssetModelData PrefabAssetModelToData(PrefabAssetModel assetModel)
    {
        return new EquippableItemPrefabAssetData((EquippableItemPrefabAsset)assetModel);
    }

    public override PrefabAssetModel MapDataToPrefabAssetModel()
    {
        EquippableItemPrefabAsset equippableItemPrefabAsset = new EquippableItemPrefabAsset(
            Name,
            PrefabAssetName,
            new Vector3(Position[0], Position[1], Position[2])
        );

        // if we forcefully cast on EquippableItem by creating new clss variable - we lost data about Type
        // because constructo think we pass Item not EquippableItem (because MapDataToObject return Item type)
        var item = EquippableItemData.MapDataToObject(equippableItemPrefabAsset.PrefabAssetName) as EquippableItem;

        if (item != null)
        {
            // successfully cast -  it was oryginally EquippableItem
            equippableItemPrefabAsset.EquippableItem = item;
        }
        else
        {
            // cast failed - it never were EquippableItem it plain Item
            equippableItemPrefabAsset.EquippableItem = new EquippableItem(
                EquippableItemData.MapDataToObject(equippableItemPrefabAsset.PrefabAssetName)
            );
        }

        return equippableItemPrefabAsset;
    }
}