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

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

namespace BansheeGz.BGDatabase
{
    public abstract class BGBDCellA : BGBDRowA
    {
        public SharedString fieldName;
        
        private BGField field;

        public BGField Field
        {
            get
            {
                var realFieldName = this.fieldName.Value;
                if (field != null && !field.IsDeleted && field.Meta != null && string.Equals(field.Name, realFieldName, StringComparison.Ordinal)) return field;
                var meta = Meta;
                if (meta == null) return null;
                field = null;
                if (string.IsNullOrEmpty(realFieldName)) throw new Exception($"Can not retrieve a field, cause field name is empty!");
                field = meta.GetField(realFieldName, false);
                if (field == null) throw new Exception($"Can not retrieve a field, cause there is no field with specified name={realFieldName}!");
                return field;
            }
        }
    }
}