using System;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Gameplay.Prototyping
{
    [ExecuteAlways]
    public abstract class ModelPrototype : MonoBehaviour
    {
        [GroupDebug, SerializeField] private bool displayDebug = true;
        [GroupInjections, SerializeField] private PrototypeModel prototypeModel;

        protected PrototypeModel PrototypeModel => prototypeModel;
        
        // protected virtual void OnEnable()
        // {
        //     ConfigElements();
        //     if (PrototypeModel.IsNull()) return;
        //     Generate();
        //     PrototypeModel.DebugRenderer.enabled = displayDebug;
        //     SetupVisibility();
        //     PrototypeModel.SetupDebugVisibility();
        // }

        protected virtual void OnValidate()
        {
            ConfigElements();
            if (PrototypeModel.IsNull()) return;
            Generate();
            PrototypeModel.DebugRenderer.enabled = displayDebug;
            SetupVisibility();
            PrototypeModel.SetupDebugVisibility();
        }
        
        private void Awake()
        {
            ConfigElements();
            if (PrototypeModel.IsNull()) return;
            PrototypeModel.DebugRenderer.enabled = displayDebug;
            SetupVisibility();
            PrototypeModel.SetupDebugVisibility();
        }

        [GroupDebug, Button(ButtonSizes.Medium, ButtonStyle.FoldoutButton)]
        protected void SizeFitting()
        {
            DebugResize();
            if (PrototypeModel.IsNull()) return;
            Generate();
        }
        
        protected virtual void ConfigElements()
        {
        }
        
        protected virtual void SetupVisibility()
        {
        }
        
        protected abstract void DebugResize();
        protected abstract void Generate();
    }
}