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

namespace GTA5M
{
    public class TheBird : Script
    {
        Ped fightingPed;
        Blip fightingPedBlip;

        public TheBird()
        {
            Interval = 100;
            Tick += OnTick;
        }

        private void OnTick(object o, EventArgs e)
        {
            // If not currently fighting a ped
            if (fightingPed == null)
            {
                // If the player is alive and available
                if (Game.Player.IsAlive && Game.Player.CanStartMission && Game.IsKeyPressed(Keys.J))
                {
                    // Play middle finger animation
                    Game.Player.Character.Task.PlayAnimation("amb@code_human_in_car_mp_actions@finger@std@ps@base", "idle_a", 2, 750, true, 1);

                    // Is player aiming at ped
                    Entity aimedEntity = Game.Player.GetTargetedEntity();
                    if (Function.Call<bool>(Hash.IS_ENTITY_A_PED, aimedEntity))
                    {
                        // Give them some time to react...
                        Wait(500);

                        // Prep the ped
                        fightingPed = (Ped)aimedEntity;
                        fightingPed.Task.ClearAllImmediately();
                        fightingPed.IsPersistent = true;
                        fightingPed.AlwaysKeepTask = true;

                        // Use a random number generator to arm the ped
                        Random randomNumberGenerated = new Random();
                        int randomNumber = randomNumberGenerated.Next(3);
                        if (randomNumber == 2)
                        {
                            fightingPed.Weapons.Give(WeaponHash.Bat, 1, true, true);
                        } else if (randomNumber == 1)
                        {
                            fightingPed.Weapons.Give(WeaponHash.Bottle, 1, true, true);
                        }

                        // Set the ped to fight you
                        fightingPed.Task.FightAgainst(Game.Player.Character);

                        // Add a blip for the ped to the map
                        fightingPedBlip = fightingPed.AddBlip();
                    }
                }
            } else
            {
                // If ped is dead or you moved to far away from them
                if (!Game.Player.IsAlive || !Game.Player.CanStartMission || !fightingPed.IsAlive || !fightingPed.IsNearEntity(Game.Player.Character, new GTA.Math.Vector3(100, 100, 100)))
                {
                    // Destroy ped
                    fightingPed.MarkAsNoLongerNeeded();
                    fightingPed.IsPersistent = false;
                    fightingPed.AlwaysKeepTask = false;
                    fightingPed = null;

                    // Destroy blip
                    if (fightingPedBlip != null)
                    {
                        fightingPedBlip.Remove();
                        fightingPedBlip = null;
                    }
                }
            }
        }
    }
}