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

using System;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine;

namespace BansheeGz.BGDatabase
{
    [BGPlugin(Version = "1.0")]
    public abstract class BGBDTableA : BGBDActionA
    {
        public SharedString tableName;

        private BGMetaEntity meta;

        public BGMetaEntity Meta
        {
            get
            {
                var realTableName = tableName.Value;
                if (meta != null && !meta.IsDeleted && string.Equals(meta.Name, realTableName, StringComparison.Ordinal)) return meta;
                meta = null;
                if (string.IsNullOrEmpty(realTableName)) throw new Exception($"Can not retrieve a meta, cause meta name is not provided!");
                meta = BGRepo.I.GetMeta(realTableName);
                if (meta == null) throw new Exception($"Can not retrieve a meta, cause there is no meta with specified name={realTableName}!");
                return meta;
            }
        }
    }
}