using GTA;
using GTA.Math;
using System;
using GTA.Native;
using System.IO;
using System.Windows.Forms;
// using System.Drawing;
using System.Threading;
public class HitmanMod : Script
{
    bool OnMission = false,Reached=false;
    int Reward;//,Attempt=0;
	Blip blip;
	GTA.Ped Target=null;
	Random rnd = new Random();
    Vector3 TargetLoc,TargetLoc2;
	int HMGroup = World.AddRelationshipGroup("H");

    public HitmanMod()
    {
        Tick += OnTick;
        
        KeyDown += OnKeyDown;
    }

    private void OnTick(object sender, EventArgs e)
    {
	
      if (OnMission)
        {

		 if(GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PLAYER_DEAD, Game.Player,true)
		  ||GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PLAYER_BEING_ARRESTED, Game.Player,true))
		  {
		    OnMission = false;
			Reached = false;
			// Attempt=0;
			blip.Remove();
			Game.Player.Money = Game.Player.Money - Reward/2;
            UI.Notify("Contract failed");
			if(Target.Exists()){Target.MarkAsNoLongerNeeded();}
				}
				else{
				
		if(Reached){
		
            if (Target.IsDead)
            {
                Game.Player.Money = Game.Player.Money + Reward;
                
				Target.MarkAsNoLongerNeeded();
				// Attempt=0;
				OnMission = false;
				Reached = false;
				blip.Remove();

				UI.Notify(string.Format("Target killed, {0}$ earned", Reward.ToString()));
					if(rnd.Next(0,3)==1){
				Wait(550);ArmPeds();
					}
				if(rnd.Next(0,5)==3){
					UI.Notify("Target had police connections!");
						if(Game.Player.WantedLevel<=3){Game.Player.WantedLevel=3;}
						Wait(750);ArmPeds();
				}else if(rnd.Next(0,7)==6){
				UI.Notify("Booby trap detonation in: 2");
				Wait(600);
				UI.Notify("Booby trap detonation in: 1");
				Wait(600);
				GTA.Native.Function.Call(GTA.Native.Hash.ADD_EXPLOSION, Target.Position.X, Target.Position.Y, Target.Position.Z,16,350.0,false,false,3);
				Wait(50);
				GTA.Native.Function.Call(GTA.Native.Hash.ADD_EXPLOSION, Target.Position.X, Target.Position.Y, Target.Position.Z+2,23,350.0,false,false,3);
				Wait(50);
				GTA.Native.Function.Call(GTA.Native.Hash.ADD_EXPLOSION, Target.Position.X, Target.Position.Y, Target.Position.Z+4,31,350.0,false,false,3);
				}
			
            }else if(World.GetDistance(Game.Player.Character.Position,Target.Position)>380){
		    OnMission = false;
			Reached = false;
			// Attempt=0;
			blip.Remove();
			Game.Player.Money = Game.Player.Money - Reward/2;
            UI.Notify("Target got away");
            Target.MarkAsNoLongerNeeded();

       }
	   // else{
	   // if(rnd.Next(0,29)==1){
	   // Target.Task.EnterVehicle();UI.Notify("Target entering a car");}
	   // }

		}else
		if(World.GetDistance(Game.Player.Character.Position,TargetLoc)<95
		||World.GetDistance(Game.Player.Character.Position,TargetLoc2)<95
		){

			Ped[] nearByPeople = World.GetNearbyPeds(Game.Player.Character, 1000);
            for (int i = 0; i < nearByPeople.Length; i++)
            {
			if(GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_HUMAN, nearByPeople[i],true)
			// && GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_ANY_VEHICLE, nearByPeople[i])
			 && World.GetDistance(Game.Player.Character.Position,nearByPeople[i].Position)>45
			 && World.GetDistance(Game.Player.Character.Position,nearByPeople[i].Position)<105)
			{
		UI.Notify("Target located");
		blip.Remove();
		Target=nearByPeople[i];
		Target.RelationshipGroup=HMGroup;
		Target.IsPersistent=true;
		// GTA.Native.Function.Call(GTA.Native.Hash.SET_PED_FLEE_ATTRIBUTES, Target,0, 0);
		// GTA.Native.Function.Call(GTA.Native.Hash.SET_PED_COMBAT_ATTRIBUTES, Target,46, 1);
		blip = Target.AddBlip();
		blip.IsFriendly=false;
		if(!GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_IN_ANY_VEHICLE, nearByPeople[i])){
		Target.Task.WanderAround();Target.AlwaysKeepTask=true;}

		// Target.AlwaysDiesOnLowHealth=true;
		Target.IsEnemy=true;
		// Target.Health=rnd.Next(350,450);
		Target.Money=rnd.Next(120,500);
		
		if(rnd.Next(0,9)!=1){ArmTarget(Target);}
		Reached=true;

		break;
		}
	 }
	 
	  if(Reached==true){
		  
		if(rnd.Next(0,7)==5){UI.Notify("The target was expecting you!");Wait(250);
		ArmPeds();
		
		if(Target.Exists()){Target.Task.FightAgainst(Game.Player.Character);}
		
		  }
		 }
	    }
	   }
      }
	 }

    public Vector3 getRandomPos()
    {
        TargetLoc = new Vector3(0,0,220);
		TargetLoc2 = new Vector3(0,0,60);

		int RandX = rnd.Next(-1305, 1100);
		int RandY = rnd.Next(-2251,473);
			TargetLoc.X = RandX;
			TargetLoc.Y = RandY;
			TargetLoc2.X = RandX;
			TargetLoc2.Y = RandY;
			// TargetLoc.Z = GTA.World.GetGroundHeight (TargetLoc);TargetLoc.Z+=5;
			// Vector3 NewLoc = GTA.World.GetNextPositionOnStreet(TargetLoc);
			
        return TargetLoc;
    }

	public void ArmTarget (Ped target)
		{
		// GTA.Native.Function.Call(GTA.Native.Hash.SET_PED_FLEE_ATTRIBUTES, target,0, 0);
		// GTA.Native.Function.Call(GTA.Native.Hash.SET_PED_COMBAT_ATTRIBUTES, target,46, 1);
		
		// target.Health=rnd.Next(350,450);
		int Weap=rnd.Next(0,5);
		
		// UI.Notify("Target is Armed! " + Weap.ToString());
		switch(Weap){
		case 0 : target.Weapons.Give(WeaponHash.Pistol50, 9920, false, true);break;
		case 1 : target.Weapons.Give(WeaponHash.CombatPistol, 9920, false, true);break;
		case 2 : target.Weapons.Give(WeaponHash.APPistol, 9920, false, true);break;
		case 3 : target.Weapons.Give(WeaponHash.HeavyPistol, 9920, false, true);break;
		case 4 : target.Weapons.Give(WeaponHash.SMG, 9920, false, true);break;
		}
		Weap=rnd.Next(3,22);
		// UI.Notify("target is Armed! " + Weap.ToString());
		switch(Weap){

		case 3 : target.Weapons.Give(WeaponHash.AssaultRifle, 9920, false, true);break;
		case 4 : target.Weapons.Give(WeaponHash.CarbineRifle, 9920, false, true);break;
		case 5 : target.Weapons.Give(WeaponHash.AdvancedRifle, 9920, false, true);break;
		case 6 : target.Weapons.Give(WeaponHash.MG, 9920, false, true);break;
		case 7 : target.Weapons.Give(WeaponHash.AssaultShotgun, 9920, false, true);break;
		case 8 : target.Weapons.Give(WeaponHash.BullpupShotgun, 9920, false, true);break;
		case 9 : target.Weapons.Give(WeaponHash.HeavySniper, 9920, false, true);break;
		case 10 : target.Weapons.Give(WeaponHash.GrenadeLauncher, 9920, false, true);break;
		case 12 : target.Weapons.Give(WeaponHash.RPG, 9920, false, true);break;
		case 13 : target.Weapons.Give(WeaponHash.AssaultSMG, 9920, false, true);break;

		case 14 : target.Weapons.Give(WeaponHash.BullpupRifle, 9920, false, true);break;
		case 15 : target.Weapons.Give(WeaponHash.HomingLauncher, 9920, false, true);break;
		case 16 : target.Weapons.Give(WeaponHash.Gusenberg, 9920, false, true);break;
		case 17 : target.Weapons.Give(WeaponHash.MarksmanRifle, 9920, false, true);break;
		case 18 : target.Weapons.Give(WeaponHash.HeavyShotgun, 9920, false, true);break;
		case 19 : target.Weapons.Give(WeaponHash.Railgun, 9920, false, true);break;
		case 20 : target.Weapons.Give(WeaponHash.CombatMG, 9920, false, true);break;
		case 21 : target.Weapons.Give(WeaponHash.SniperRifle, 9920, false, true);break;
		}
		}
	public void ArmPeds()
		{
		
		Ped[] nearByPeople = World.GetNearbyPeds(Game.Player.Character, 1000);
            for (int i = 0; i < nearByPeople.Length; i++)
            {
			if(nearByPeople[i].Exists()
			// &&nearByPeople[i]!=Game.Player.Character
			&&GTA.Native.Function.Call<bool>(GTA.Native.Hash.IS_PED_HUMAN, nearByPeople[i],true)){
			
			nearByPeople[i].IsEnemy=true;
			ArmTarget(nearByPeople[i]);
			nearByPeople[i].Task.FightAgainst(Game.Player.Character);
			nearByPeople[i].AlwaysKeepTask=true;
			nearByPeople[i].RelationshipGroup=HMGroup;
			nearByPeople[i].Money=rnd.Next(120,400);
			Wait(0);
			}
			
		}
	
	}

    void OnKeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.B

		&&GTA.Native.Function.Call<bool>(GTA.Native.Hash.CAN_PLAYER_START_MISSION,Game.Player)) { 
		Game.Player.Character.Task.UseMobilePhone(1000);
		Wait(1400);
		
		if (OnMission)
        {
            OnMission = false;
			Reached = false;
			// Attempt=0;
			blip.Remove();
            UI.Notify("Hitman contract rejected");
            Target.MarkAsNoLongerNeeded();
			Game.Player.Money = Game.Player.Money - Reward/2;

        }
        else if(Game.Player.WantedLevel<1)
	        {
            OnMission = true;

			blip = World.CreateBlip(getRandomPos());
			blip.ShowRoute=true;
			blip.Sprite=GTA.BlipSprite.Crosshair2;
			Reward = rnd.Next(330, 470)*10;
            UI.Notify("New contract available (Reward : " + Reward.ToString() + "$)");

            Wait(1000);
			}else{UI.Notify("Loose the cops first");}
		}
    }
}