/*
<copyright file="BGUNGetRelatedIndex.cs" company="BansheeGz">
    Copyright (c) 2018-2021 All Rights Reserved
</copyright>
*/

using System;
using MaxyGames.uNode;

namespace BansheeGz.BGDatabase
{
    [NodeMenu("BansheeGz", "DB: Get related index")]
    public class BGUNGetRelatedIndex : BGUNCellA, IDataNode<int> 
    {
        object IDataNode.GetValue(object graph)
        {
            return GetValue(graph);
        }

        public Type ReturnType()
        {
            return typeof(int);
        }

        public int GetValue(object graph)
        {
            var field = Field;
            if (!(field is BGFieldRelationSingle))
                throw new Exception($"Can not read value from the database: " +
                                    $"field '{field.FullName}' is not single relation field!");
            var entity = Entity;
            var relatedEntity = ((BGFieldRelationSingle) field)[entity.Index];
            return relatedEntity?.Index ?? -1;
        }

    }
}