﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Onward.CustomContent.Assets
{
    [ExecuteInEditMode]
    public class SetPhysicMaterial : Entity
    {
        public enum EPhysicMaterialType
        {
            Concrete = 0,
            Glass = 1,
            GlassUnpenetratable = 8,
            GlassVfx = 9,
            Metal = 2,
            MetalUnpenetratable = 3,
            Wood = 4,
            WoodUnpenetratable = 5,
            Dirt = 6,
            Grass = 7,
            Cloth = 10,
            ClothUnpenetratable = 11,
            ClothVfx = 12,
            Water = 13,
            Snow = 14,
            SnowUnpenetratable = 15,
            Ice = 16,
            IceUnpenetrable = 17,
        }

        public EPhysicMaterialType MaterialType = EPhysicMaterialType.Concrete;

        [Header("Can AI opponents see through this material?")]
        public bool AISeeThrough = false;

        [Header("Apply same settings to children, too?")]
        public bool IncludeChildren = false;

#if UNITY_EDITOR
        private void OnEnable()
        {
            if (GetComponents<SetPhysicMaterial>().Length > 1)
            {
                Debug.LogError($"Only one instance of {nameof(SetPhysicMaterial)} per GameObject allowed.", gameObject);

                DestroyImmediate(this);
            }
        }
#endif

        public override void Validate()
        {
            base.Validate();

            Collider[] allColliders = GetComponentsInChildren<Collider>(true);

            if (allColliders.Length <= 0)
            {
                Debug.LogWarning($"{gameObject.name} uses {nameof(SetPhysicMaterial)} but is lacking colliders.");
            }
        }

#if UNITY_EDITOR
        [UnityEditor.MenuItem("Component/ONWARD/Physic material")]
        static void AddToGameObject()
        {
            if (UnityEditor.Selection.activeGameObject != null)
                UnityEditor.Selection.activeGameObject.AddComponent<SetPhysicMaterial>();
        }
#endif
    }
}