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

using System.Collections.Generic;
using GameCreator.Core;
using GameCreator.Variables;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;

#endif

namespace BansheeGz.BGDatabase
{
    public class BGGCSetRelatedList : BGGCSetA
    {
        public HelperListVariable listVariables = new HelperListVariable();

        [SerializeField] private List<int> value;

        public override bool IsSupported(BGField field)
        {
            return field is BGFieldRelationMultiple;
        }

        protected override void AssignUsingVar(GameObject target, BGGCCell cell)
        {
            if (listVariables == null)
            {
                Debug.Log("WARNING! BGDatabase: can not access listVariables variable!");
                return;
            }

            var listVar = listVariables.GetListVariables(target);
            if (listVar == null)
            {
                Debug.Log("WARNING! BGDatabase: listVar variable is null!");
                return;
            }

            if (listVar.type != Variable.DataType.Number)
            {
                Debug.Log("WARNING! BGDatabase: can not use list variable, cause list var type is not number!");
                return;
            }

            List<int> val = null;
            if (listVar.variables != null && listVar.variables.Count > 0)
            {
                val = new List<int>(listVar.variables.Count);
                foreach (var item in listVar.variables) val.Add(Mathf.RoundToInt((float)item.Get()));
            }

            Assign(cell, val);
        }

        protected override void AssignUsingValue(GameObject target, BGGCCell cell)
        {
            Assign(cell, value);
        }

        private static void Assign(BGGCCell cell, List<int> value)
        {
            var relatedMeta = ((BGFieldRelationMultiple)cell.field).RelatedMeta;

            List<BGEntity> val = null;
            if (value != null && value.Count > 0)
            {
                val = new List<BGEntity>();
                foreach (var v in value)
                {
                    if (v < 0 || v >= relatedMeta.CountEntities)
                    {
                        Debug.Log("WARNING! BGDatabase: can not assign multiple relation value! One of the values is in invalid range! valid range=[0,"
                                  + (relatedMeta.CountEntities - 1) + "], invalid value=" + v);
                        return;
                    }

                    val.Add(relatedMeta.GetEntity(v));
                }
            }

            cell.Value = val;
        }
        // +--------------------------------------------------------------------------------------+
        // | EDITOR                                                                               |
        // +--------------------------------------------------------------------------------------+

#if UNITY_EDITOR
        public static new string NAME = "BansheeGz/Set related list";
        protected override string Title => "BGDatabase: Set related list";

        public override void EditorGui()
        {
            base.EditorGui();
            if (UseVarForValue) EditorGUILayout.PropertyField(Prop(nameof(listVariables)));
            else
            {
                EditorGUILayout.PropertyField(Prop(nameof(value)), true);
            }
        }
#endif
    }
}