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

using System;
using Unity.VisualScripting;

namespace BansheeGz.BGDatabase
{
    [UnitCategory("BansheeGz")]
    public class BGDatabaseCountEntities : Unit
    {
        [DoNotSerialize] public ValueInput metaName;
        [DoNotSerialize] public ValueOutput count;

        protected override void Definition()
        {
            metaName = ValueInput<string>("Meta name", "");
            count = ValueOutput<int>("count", GetCount);
        }

        private int GetCount(Flow flow)
        {
            var tableName = flow.GetValue<string>(metaName);
            if (string.IsNullOrEmpty(tableName)) throw new Exception("Meta name is not set!");
            var meta = BGRepo.I[tableName];
            if (meta == null) throw new Exception("Can not find meta with name " + tableName + "!");
            return meta.CountEntities;
        }
    }
}