/*
<copyright file="BGNCGetValue.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 value")]
    [Description("Get cell value from database and store result in varToAssign variable")]
    public class BGNCGetValue<T> : BGNCCellA
    {
        public BBParameter<T> varToAssign;

        protected override void OnExecute()
        {
            var field = Field;
            if (!(typeof(T).IsAssignableFrom(field.ValueType)))
                throw new Exception($"Can not read value from the database: " +
                                    $"field '{field.FullName}' values (type={field.ValueType.FullName}) can not be cast to {typeof(T).FullName} type!");
            var entity = Entity;
            T value;
            if (field is BGField<T>)
            {
                value = ((BGField<T>) field)[entity.Index];
            }
            else
            {
                value = (T) field.GetValue(entity.Index);
            }

            varToAssign.value = value;
            EndAction(true);
        }
    }
}