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

using GameCreator.Core;
using GameCreator.Variables;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace BansheeGz.BGDatabase
{
    /// <summary>
    /// abstract database setter
    /// </summary>
    public abstract class BGGCSetA : BGGCCellA
    {
        [SerializeField] [HideInInspector] private bool useVarForValue;

        public bool UseVarForValue => useVarForValue;

        public override bool InstantExecute(GameObject target, IAction[] actions, int index)
        {
            var cell = GetCell(target);
            if (cell == null)
            {
                Debug.Log("WARNING! BGDatabase: can not access a target cell!");
                return true;
            }
            var cellValue = cell.Value;
            
            if (useVarForValue)
            {
                AssignUsingVar(target, cellValue);
            }
            else
            {
                AssignUsingValue(target, cellValue);
            }
            return true;
        }

        protected abstract void AssignUsingValue(GameObject target, BGGCCell cell);

        protected abstract void AssignUsingVar(GameObject target, BGGCCell cell);

        protected bool IsValid(VariableProperty prop)
        {
            if (prop != null) return true;
            Debug.Log("WARNING! BGDatabase: can not store value to variable, cause it is null!");
            return false;
        }

        // +--------------------------------------------------------------------------------------+
        // | EDITOR                                                                               |
        // +--------------------------------------------------------------------------------------+

#if UNITY_EDITOR
        public override void EditorGui()
        {
            base.EditorGui();
            EditorGUILayout.PropertyField(Prop(nameof(useVarForValue)), new GUIContent("Use variable for value"));
        }
#endif

    }
}