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

using System;
using NodeCanvas.Framework;
using ParadoxNotion.Design;

namespace BansheeGz.BGDatabase
{
    [Category("BansheeGz")]
    [Name("BGDatabase: Get related index")]
    [Description("Get related entity index from database if any, or -1 if no value, relationSingle fields are supported")]
    public class BGNCGetRelatedIndex : BGNCCellA
    {
        [RequiredField] public BBParameter<int> targetVar;

        protected override void OnExecute()
        {
            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];
            targetVar.value = relatedEntity?.Index ?? -1;
            EndAction(true);
        }
    }
}