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

using System;
using System.Reflection;
using UnityEngine.UIElements;

namespace BansheeGz.BGDatabase
{
    [Serializable]
    public class BGDBUiElementBinderLocalized : BGDBUiElementBinderRowBasedA
    {
        private const string AddonTypeName = "BansheeGz.BGDatabase.BGAddonLocalization";
        private const string CurrentLocalePropertyName = "CurrentLocale";

        private static Type localizationAddonType;
        private static PropertyInfo currentLocaleProperty;

        private Type LocalizationAddonType
        {
            get
            {
                if (localizationAddonType != null) return localizationAddonType;
                localizationAddonType = BGUtil.GetType(AddonTypeName);
                if (localizationAddonType == null) return localizationAddonType;
                return localizationAddonType;
            }
        }


        private PropertyInfo CurrentLocaleProperty
        {
            get
            {
                if (currentLocaleProperty != null) return currentLocaleProperty;
                var addonType = LocalizationAddonType;
                if (addonType == null) return null;
                currentLocaleProperty = addonType.GetProperty(CurrentLocalePropertyName);
                return currentLocaleProperty;
            }
        }

        private BGAddon Addon
        {
            get
            {
                var addonType = LocalizationAddonType;
                if (addonType == null) return null;
                return BGRepo.I.Addons.Get(addonType);
            }
        }

        public override object Value
        {
            get
            {
                var e = Entity;
                if (e == null) throw new Exception(IsEntitySet ? $"Can not find an entity using metaId={metaIdString} and entityId={entityIdString}" : "Entity is not set");
                return Field.GetValue(e.Index);
            }
        }

        public BGField Field
        {
            get
            {
                var m = Meta;
                if (m == null) throw new Exception(IsMetaSet ? $"Can not find a meta using metaId={metaIdString}" : "Meta is not set");
                var localizationAddon = Addon;
                if (localizationAddon == null) throw new Exception("Localization addon is not enabled");
                var localeProperty = CurrentLocaleProperty;
                if (localeProperty == null) throw new Exception($"Can not find localization current locale property {CurrentLocalePropertyName}");
                var fieldName = localeProperty.GetValue(localizationAddon) as string;
                if (fieldName == null) throw new Exception("Can not retrieve current locale value");
                var field = m.GetField(fieldName);
                if (field == null) throw new Exception($"Can not retrieve field with name {fieldName}");
                return field;
            }
        }

        public override Type ValueType
        {
            get
            {
                try
                {
                    return Field?.ValueType;
                }
                catch
                {
                    return null;
                }
            }
        }

        public override bool Enabled => CurrentLocaleProperty != null && Addon != null;
        public override string Title => "Localized";

        public override void ReverseBind(UIDocument doc)
        {
            //not supported for now
        }

        public override bool IsMetaSupported(BGMetaEntity meta) => meta.GetType().FullName.Equals("BansheeGz.BGDatabase.BGMetaLocalizationSingleValue");
    }
}