using GTA;
using GTA.Native;
using GTA.Math;
using System.Windows.Forms;

public class HornsProp : Script
{
    private bool hornsAdded = false;
    private Prop hornsProp;
    private int fireEffectLeft;
    private int fireEffectRight;
    private Prop beachFireProp;

    public HornsProp()
    {
        KeyDown += OnKeyDown;
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.H)
        {
            if (!hornsAdded)
            {
                AddHorns();
                hornsAdded = true;
            }
            else
            {
                RemoveHorns();
                hornsAdded = false;
            }
        }
    }

    private void AddHorns()
    {
        var player = Game.Player.Character;

        PlayPlayerAnimation(player);

        ShowMessageToAll("Power is awaking!! Haaha by kunalbot");

        Script.Wait(4000); 

        Script.Wait(1000);

        LoadAndAttachHorns(player);
        AddFireEffects();

        ShowMessageToAll("Power Is Awakened!!");
    }

    private void RemoveHorns()
    {
        var player = Game.Player.Character;

        PlayPlayerAnimation(player);

        ShowMessageToAll("Power is Going back to sleep!! Haaha"); 

        Script.Wait(4000);  

        Script.Wait(1000);

        if (hornsProp != null && hornsProp.Exists())
        {
            hornsProp.Delete();
            hornsProp = null;
        }

        if (fireEffectLeft != 0)
        {
            Function.Call(Hash.REMOVE_PARTICLE_FX, fireEffectLeft, true);
            fireEffectLeft = 0;
        }

        if (fireEffectRight != 0)
        {
            Function.Call(Hash.REMOVE_PARTICLE_FX, fireEffectRight, true);
            fireEffectRight = 0;
        }

        if (beachFireProp != null && beachFireProp.Exists())
        {
            beachFireProp.Delete();
            beachFireProp = null;
        }

        ShowMessageToAll("Power Has Gone Back to sleep! by kunalbot");
    }

    private void ShowMessageToAll(string message)
    {
        foreach (var player in World.GetAllPeds())
        {
            float distance = player.Position.DistanceTo(Game.Player.Character.Position);

            if (distance <= 50f) 
            {
                GTA.UI.Screen.ShowSubtitle(message, 3000);  
            }
        }
    }

    private void LoadAndAttachHorns(Ped player)
    {
        Model hornsModel = new Model("redpowerhorns");
        hornsModel.Request(250);

        while (!hornsModel.IsLoaded)
        {
            Script.Wait(50);
        }

        hornsProp = World.CreateProp(hornsModel, player.Position, false, false);

        int headBoneIndex = Function.Call<int>(Hash.GET_PED_BONE_INDEX, player.Handle, 31086);

        Function.Call(Hash.ATTACH_ENTITY_TO_ENTITY, 
                      hornsProp.Handle,               
                      player.Handle,                 
                      headBoneIndex,                
                      0.0004f, 0.0100f, -0.0020f,    
                      0f, 0f, 0f,                     
                      false,                        
                      true,                           
                      false,                         
                      false,                         
                      2,                            
                      true                            
        );

        Model beachFireModel = new Model("prop_beach_fire");
        beachFireModel.Request(250);

        while (!beachFireModel.IsLoaded)
        {
            Script.Wait(50);
        }

        beachFireProp = World.CreateProp(beachFireModel, new Vector3(0.3995f, -0.0460f, -0.0064f), false, false);

        beachFireProp.Rotation = new Vector3(0f, 90f, 0f);

        beachFireProp.IsCollisionEnabled = false;
        beachFireProp.Opacity = 0;

        Function.Call(Hash.ATTACH_ENTITY_TO_ENTITY, 
                      beachFireProp.Handle,         
                      player.Handle,                
                      headBoneIndex,                
                      0.3995f, -0.0460f, -0.0064f,  
                      0f, 90f, 0f,                  
                      false,                       
                      true,                         
                      false,                       
                      false,                      
                      2,                           
                      true                          
        );
    }

    private void AddFireEffects()
    {
        var player = Game.Player.Character;

        fireEffectLeft = Function.Call<int>(Hash.START_PARTICLE_FX_NON_LOOPED_AT_COORD, 
                                            "core",               
                                            player.Position.X - 1.0f, player.Position.Y - 0.5f, player.Position.Z + 1.5f, 
                                            0f, 0f, 0f,          
                                            1f,              
                                            0,                  
                                            0,                    
                                            0);                 
        
        fireEffectRight = Function.Call<int>(Hash.START_PARTICLE_FX_NON_LOOPED_AT_COORD, 
                                             "core",            
                                             player.Position.X + 1.0f, player.Position.Y - 0.5f, player.Position.Z + 1.5f, 
                                             0f, 0f, 0f,         
                                             1f,                   
                                             0,                  
                                             0,                 
                                             0);                  
    }

    private void PlayPlayerAnimation(Ped playerPed)
    {
        string dict = "special_ped@jessie@michael_1@michael_1b"; 
        string anim = "jessie_ig_2_yeahyeahyeah_1"; 

        if (!Function.Call<bool>(Hash.HAS_ANIM_DICT_LOADED, dict))
        {
            Function.Call(Hash.REQUEST_ANIM_DICT, dict); 
            while (!Function.Call<bool>(Hash.HAS_ANIM_DICT_LOADED, dict))
            {
                Script.Wait(10);
            }
        }

        Function.Call(Hash.TASK_PLAY_ANIM, playerPed.Handle, dict, anim, 8.0f, -8.0f, -1, 0, 0, false, false, false);
    }
}
