using UnityEngine;

namespace Mod
{
    public class Mod
    {
        public static void Main()
        {
            // Register the cure liquid first
            ModAPI.RegisterLiquid(ZombieCureSyringe.CureSerum.ID, new ZombieCureSyringe.CureSerum());
            
            // Register the zombie cure syringe
            ModAPI.Register(
                new Modification()
                {
                    OriginalItem = ModAPI.FindSpawnable("Zombie Syringe"), // Base it on zombie syringe
                    NameOverride = "Zombie Cure Syringe",
                    DescriptionOverride = "A revolutionary antidote that reverses zombie infection and restores humanity.",
                    CategoryOverride = ModAPI.FindCategory("Medical"),
                    ThumbnailOverride = ModAPI.LoadSprite("sprites/cure_syringe_view.png"),
                    AfterSpawn = (Instance) =>
                    {
                        // Remove the original zombie syringe behavior
                        UnityEngine.Object.Destroy(Instance.GetComponent<SyringeBehaviour>());
                        
                        // Add our custom cure behavior
                        Instance.GetOrAddComponent<ZombieCureSyringe>();
                        
                        // Customize the appearance
                        Instance.GetComponent<SpriteRenderer>().sprite = ModAPI.LoadSprite("sprites/cure_syringe.png");
                        
                        // Add a healing glow effect
                        var glow = Instance.GetComponent<SpriteRenderer>();
                        if (glow != null)
                        {
                            glow.color = new Color(0.3f, 1f, 0.3f, 0.8f); // Green healing glow
                        }
                        
                        // Add spawn sound effect
                        var audioSource = Instance.GetComponent<AudioSource>();
                        if (audioSource == null)
                        {
                            audioSource = Instance.AddComponent<AudioSource>();
                        }
                        
                        var spawnSound = ModAPI.LoadSound("sfx/cure_spawn.wav");
                        if (spawnSound != null && audioSource != null)
                        {
                            audioSource.PlayOneShot(spawnSound);
                        }
                        
                        Instance.FixColliders();
                    }
                }
            );
        }
    }

    public class ZombieCureSyringe : SyringeBehaviour
    {
        public override string GetLiquidID() => CureSerum.ID;

        public class CureSerum : Liquid
        {
            public const string ID = "ZOMBIE CURE SERUM";

            public CureSerum()
            {
                // Bright green color for the cure liquid
                Color = new Color(0.2f, 1f, 0.2f, 0.9f);
            }

            public override void OnEnterLimb(LimbBehaviour limb)
            {
                // Cure zombie infection by removing zombie-related effects
                var person = limb.Person;
                if (person != null)
                {
                    // Remove zombie poison if present
                    var zombiePoison = person.GetComponent<CirculationBehaviour>()?.GetLiquid("REANIMATION AGENT");
                    if (zombiePoison != null)
                    {
                        person.GetComponent<CirculationBehaviour>().RemoveLiquid(zombiePoison);
                    }
                    
                    // Restore human appearance and behavior
                    if (limb.gameObject.name.Contains("Head"))
                    {
                        // Restore normal skin color
                        var spriteRenderer = limb.GetComponent<SpriteRenderer>();
                        if (spriteRenderer != null)
                        {
                            spriteRenderer.color = new Color(1f, 0.85f, 0.7f); // Normal skin tone
                        }
                    }
                    
                    // Heal any zombie-related damage
                    limb.WoundIntensity = 0f;
                    limb.BruiseIntensity = 0f;
                    limb.BurnIntensity = 0f;
                    
                    // Restore normal physical properties
                    limb.BreakingThreshold = limb.OriginalBreakingThreshold;
                    limb.RegenerationSpeed = limb.OriginalRegenerationSpeed;
                }
                
                // Apply healing effect
                limb.HealDamage(5f);
            }

            public override void OnUpdate(BloodContainer container)
            {
                // Continuous healing effect while cure is in bloodstream
                if (container is LimbBehaviour limb)
                {
                    limb.HealDamage(0.1f);
                    
                    // Prevent re-infection
                    var zombiePoison = container.GetLiquid("REANIMATION AGENT");
                    if (zombiePoison != null)
                    {
                        container.RemoveLiquid(zombiePoison);
                    }
                }
            }

            public override void OnEnterContainer(BloodContainer container)
            {
                // Healing aura effect
                if (container is LimbBehaviour limb)
                {
                    var person = limb.Person;
                    if (person != null)
                    {
                        // Apply gentle healing to all limbs
                        foreach (var personLimb in person.Limbs)
                        {
                            personLimb.HealDamage(0.5f);
                        }
                    }
                }
            }

            public override void OnExitContainer(BloodContainer container)
            {
                // Final healing burst when cure leaves the system
                if (container is LimbBehaviour limb)
                {
                    limb.HealDamage(2f);
                }
            }
        }
    }
}