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

using System;
using UnityEngine;

namespace BansheeGz.BGDatabase
{
    [Serializable]
    public abstract class BGDBUiElementBinderRowBasedA : BGDBUiElementBinderA
    {
        [SerializeField] protected string metaIdString;
        [SerializeField] protected string entityIdString;

        protected BGMetaEntity meta;
        protected BGEntity entity;

        public string MetaIdString => metaIdString;

        public string EntityIdString => entityIdString;

        public BGMetaEntity Meta
        {
            get
            {
                if (meta != null && !meta.IsDeleted) return meta;
                meta = null;
                if (!IsMetaSet) return null;
                if (!BGId.TryParse(metaIdString, out var metaId)) return null;
                return meta = BGRepo.I.GetMeta(metaId);
            }
            set
            {
                if (value == null)
                {
                    entityIdString = null;
                    metaIdString = null;
                    meta = null;
                    entity = null;
                }
                else
                {
                    var valueId = value.Id.ToString();
                    if (valueId == metaIdString) return;
                    metaIdString = valueId;
                    entityIdString = null;
                    meta = value;
                    entity = null;
                }
                MetaChanged();
            }
        }

        protected bool IsMetaSet => !string.IsNullOrEmpty(metaIdString);

        protected bool IsEntitySet => !string.IsNullOrEmpty(entityIdString);

        public BGEntity Entity
        {
            get
            {
                if (entity?.Meta != null && !entity.Meta.IsDeleted) return entity;
                entity = null;
                if (!IsEntitySet) return null;
                var m = Meta;
                if (m == null) return null;
                if (!BGId.TryParse(entityIdString, out var entityId)) return null;
                return entity = m.GetEntity(entityId);
            }
            set
            {
                if (value == null)
                {
                    entityIdString = null;
                    entity = null;
                }
                else
                {
                    var valueId = value.Id.ToString();
                    if (valueId == entityIdString) return;
                    entityIdString = valueId;
                    if (value.MetaId.ToString() != metaIdString)
                    {
                        metaIdString = value.MetaId.ToString();
                        meta = value.Meta;
                        MetaChanged();
                    }

                    entity = value;
                }
            }
        }

        protected virtual void MetaChanged()
        {
           
        }

        public virtual bool IsMetaSupported(BGMetaEntity meta) => true;
    }
}