using UnityEngine;

namespace Northbound
{
    /// <summary>
    /// Simple test script to verify ghost materials work.
    /// Attach to any GameObject with a Renderer to see the ghost effect.
    /// </summary>
    public class GhostMaterialTest : MonoBehaviour
    {
        [Header("Test Settings")]
        public Color ghostColor = new Color(0, 1, 0, 0.5f);
        public bool applyOnStart = true;

        private void Start()
        {
            if (applyOnStart)
            {
                ApplyGhostMaterial();
            }
        }

        [ContextMenu("Apply Ghost Material")]
        public void ApplyGhostMaterial()
        {
            Material ghostMat = CreateGhostMaterial(ghostColor);

            Renderer[] renderers = GetComponentsInChildren<Renderer>();
            foreach (var renderer in renderers)
            {
                Material[] mats = new Material[renderer.materials.Length];
                for (int i = 0; i < mats.Length; i++)
                {
                    mats[i] = ghostMat;
                }
                renderer.materials = mats;
                renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                renderer.receiveShadows = false;
            }

            Debug.Log($"Applied ghost material to {renderers.Length} renderers");
        }

        private Material CreateGhostMaterial(Color color)
        {
            // Try to find appropriate shader for current render pipeline
            Shader shader = Shader.Find("Universal Render Pipeline/Lit");
            if (shader == null)
                shader = Shader.Find("Standard");
            if (shader == null)
                shader = Shader.Find("Diffuse");

            Material mat = new Material(shader);

            // Set base color
            if (mat.HasProperty("_BaseColor"))
                mat.SetColor("_BaseColor", color); // URP
            else if (mat.HasProperty("_Color"))
                mat.SetColor("_Color", color); // Standard

            // Enable transparency
            if (mat.HasProperty("_Surface"))
            {
                // URP Transparency
                mat.SetFloat("_Surface", 1); // Transparent
                mat.SetFloat("_Blend", 0); // Alpha
                mat.SetFloat("_AlphaClip", 0);
                mat.SetFloat("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                mat.SetFloat("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                mat.SetFloat("_ZWrite", 0);
                mat.renderQueue = 3000;
                mat.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
                mat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
            }
            else
            {
                // Standard RP Transparency
                mat.SetFloat("_Mode", 3); // Transparent mode
                mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                mat.SetInt("_ZWrite", 0);
                mat.DisableKeyword("_ALPHATEST_ON");
                mat.EnableKeyword("_ALPHABLEND_ON");
                mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                mat.renderQueue = 3000;
            }

            Debug.Log($"Created ghost material with shader: {shader.name}");
            return mat;
        }
    }
}
