using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using GTA;

namespace TestScriptCS
{
   public class EquipGun : Script
   {
       GTA.Object sm1 = null; //32TBOGT_AssaultSMG 
       GTA.Object sg1 = null; //11
       GTA.Object sg2 = null; //30TBOGT_ExplosiveShotgun
       GTA.Object sg3 = null; //31TBOGT_NormalShotgun
       GTA.Object sg4 = null; //22 TLAD_AssaultShotgun 
       GTA.Object sg5 = null; //26TLAD_SawedOffShotgun 
       GTA.Object ar1 = null; //14
       GTA.Object ar2 = null; //15
       GTA.Object ar3 = null; //34TBOGT_AdvancedMG 
       GTA.Object sn1 = null; //16
       GTA.Object sn2 = null; //17
       GTA.Object sn3 = null; //35TBOGT_AdvancedSniper
       GTA.Object gr1 = null; //21TBOGT_GrenadeLauncher  
       GTA.Object gr2 = null; //21TLAD_GrenadeLauncher
       int episodes = 0;
       int currentGun = 0;
       int EqSlot_1 = 0;
       int EqSlot_2 = 0;

       public EquipGun()
      {
          Wait(2000);
          this.Tick += new EventHandler(this.EquipGun_Tick);
          sg1 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
          ar1 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
          ar2 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
          sn1 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
          sn2 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
          Wait(1000);
          while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg1)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar1)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar2)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn1)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn2))
          {
              Wait(0);
          }
          sg1.Delete();
          ar1.Delete();
          ar2.Delete();
          sn1.Delete();
          sn2.Delete();
          episodes = (int)Game.CurrentEpisode;
          if (episodes == 1)
          {
              gr2 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              sg4 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              sg5 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              Wait(1000);
              while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr2)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg4)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg5))
              {
                  Wait(0);
              }
              gr2.Delete();
              sg4.Delete();
              sg5.Delete();
          }
          if (episodes == 2)
          {
              sm1 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              sg2 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              sg3 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              ar3 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              sn3 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              gr1 = World.CreateObject("bm_char_fag_f", Player.Character.Position.Around(99.0F));
              Wait(1000);
              while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sm1)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg2)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg3)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar3)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn3)
              || !GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr1))
              {
                  Wait(0);
              }
              sm1.Delete();
              sg2.Delete();
              sg3.Delete();
              ar3.Delete();
              sn3.Delete();
              gr1.Delete();
          }
          //GTA.Native.Function.Call("GIVE_WEAPON_TO_CHAR", Player.Character, 35, 1, 0);
          //Player.Character.Health = 5;
      }

       private void EquipGun_Tick(object sender, EventArgs e)
      {
          Wait(0);
          if (GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "upset_in_bed_idle")
             || GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "lie_on_bed_l")
             || GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "lie_on_bed_r")
              || GTA.Native.Function.Call<bool>("IS_PAUSE_MENU_ACTIVE"))
          {
              while (GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "upset_in_bed_idle")
             || GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "lie_on_bed_l")
             || GTA.Native.Function.Call<bool>("IS_CHAR_PLAYING_ANIM", Player.Character, "amb@savegame", "lie_on_bed_r")
             || GTA.Native.Function.Call<bool>("IS_PAUSE_MENU_ACTIVE"))
              {
                  Wait(0);
              }
              
          }
          GTA.Native.Pointer currentGun = new GTA.Native.Pointer(typeof(int));
          GTA.Native.Function.Call("GET_CURRENT_CHAR_WEAPON", Player.Character, currentGun);

          if (!GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, EqSlot_1))
          {
              EqSlot_1 = 0;
          }
          if (!GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, EqSlot_2))
          {
              EqSlot_2 = 0;
          }

          if (currentGun != EqSlot_2 && EqSlot_1 != EqSlot_2)
          {
              EqSlot_1 = EqSlot_2;
          }

          if (currentGun == EqSlot_1 || EqSlot_1 == 0)
          {
              EqSlot_1 = 0;
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 21) && currentGun != 21)
              {
                  EqSlot_1 = 21;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 11) && currentGun != 11)
              {
                  EqSlot_1 = 11;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 30) && currentGun != 30)
              {
                  EqSlot_1 = 30;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 31) && currentGun != 31)
              {
                  EqSlot_1 = 31;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 22) && currentGun != 22)
              {
                  EqSlot_1 = 22;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 26) && currentGun != 26)
              {
                  EqSlot_1 = 26;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 32) && currentGun != 32)
              {
                  EqSlot_1 = 32;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 16) && currentGun != 16)
              {
                  EqSlot_1 = 16;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 17) && currentGun != 17)
              {
                  EqSlot_1 = 17;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 35) && currentGun != 35)
              {
                  EqSlot_1 = 35;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 14) && currentGun != 14)
              {
                  EqSlot_1 = 14;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 15) && currentGun != 15)
              {
                  EqSlot_1 = 15;
              }
              if (GTA.Native.Function.Call<bool>("HAS_CHAR_GOT_WEAPON", Player.Character, 34) && currentGun != 34)
              {
                  EqSlot_1 = 34;
              }
          }
          if (currentGun == 21)
          {
              EqSlot_2 = 21;
          }

          if (currentGun == 11)
          {
              EqSlot_2 = 11;
          }
          if (currentGun == 30)
          {
              EqSlot_2 = 30;
          }
          if (currentGun == 31)
          {
              EqSlot_2 = 31;
          }
          if (currentGun == 22)
          {
              EqSlot_2 = 22;
          }
          if (currentGun == 26)
          {
              EqSlot_2 = 26;
          }

          if (currentGun == 32)
          {
              EqSlot_2 = 32;
          }

          if (currentGun == 16)
          {
              EqSlot_2 = 16;
          }
          if (currentGun == 17)
          {
              EqSlot_2 = 17;
          }
          if (currentGun == 35)
          {
              EqSlot_2 = 35;
          }

          if (currentGun == 14)
          {
              EqSlot_2 = 14;
          }
          if (currentGun == 15)
          {
              EqSlot_2 = 15;
          }
          if (currentGun == 34)
          {
              EqSlot_2 = 34;
          }
          episodes = (int)Game.CurrentEpisode;
          if (EqSlot_1 == 0)
          {
              if (GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg1) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar1)
                  || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar2) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn1)
                  || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn2))
              {
                  Delete_obj();
              }
              if (episodes == 1)
              {
                 if (GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr2) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg4)
                     || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg5))
                 {
                     Delete_obj();
                 }
              }
              if (episodes == 2)
              {
                  if (GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sm1) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg2)
                      || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg3) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar3)
                      || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn3) || GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr1))
                  {
                      Delete_obj();
                  }
              }
          }
          if (EqSlot_1 == 11)
          {
              if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg1) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sg1))
              {
                  Delete_obj();
                  sg1 = World.CreateObject("w_pumpshot", Player.Character.Position.Around(99.0f));
                  while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg1))
                  {
                      Wait(0);
                  }
                  GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sg1, Player.Character, 1223, 0.07f, 0.13f, 0.1f, 210.1f, 30.1f, 0.0, 0);
                  GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sg1);
              }
          }
          if (EqSlot_1 == 14)
          {
              if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar1) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", ar1))
              {
                  Delete_obj();
                  ar1 = World.CreateObject("w_ak47", Player.Character.Position.Around(99.0f));
                  while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar1))
                  {
                      Wait(0);
                  }
                  GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", ar1, Player.Character, 1223, 0.04f, 0.16f, 0.29f, 210.1f, 30.1f, 0.0, 0);
                  GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", ar1);
              }
          }
          if (EqSlot_1 == 15)
          {
              if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar2) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", ar2))
              {
                  Delete_obj();
                  ar2 = World.CreateObject("w_m4", Player.Character.Position.Around(99.0f));
                  while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar2))
                  {
                      Wait(0);
                  }
                  GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", ar2, Player.Character, 1223, 0.03f, 0.16f, 0.25f, 210.1f, 30.1f, 0.0, 0);
                  GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", ar2);
              }
          }
          if (EqSlot_1 == 16)
          {
              if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn1) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sn1))
              {
                  Delete_obj();
                  sn1 = World.CreateObject("w_psg1", Player.Character.Position.Around(99.0f));
                  while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn1))
                  {
                      Wait(0);
                  }
                  GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sn1, Player.Character, 1223, 0.07f, 0.15f, 0.2f, 210.1f, 30.1f, 0.0, 0);
                  GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sn1);
              }
          }
          if (EqSlot_1 == 17)
          {
              if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn2) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sn2))
              {
                  Delete_obj();
                  sn2 = World.CreateObject("w_rifle", Player.Character.Position.Around(99.0f));
                  while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn2))
                  {
                      Wait(0);
                  }
                  GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sn2, Player.Character, 1223, 0.1f, 0.15f, 0.3f, 210.1f, 30.1f, 0.0, 0);
                  GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sn2);
              }
          }
           
           if (episodes == 1)
           {
               if (EqSlot_1 == 22)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg4) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sg4))
                   {
                       Delete_obj();
                       sg4 = World.CreateObject("w_e1_sweeper", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg4))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sg4, Player.Character, 1223, -0.01f, 0.17f, -0.15f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sg4);
                   }
               }
               if (EqSlot_1 == 26)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg5) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sg5))
                   {
                       Delete_obj();
                       sg5 = World.CreateObject("w_e1_sawnoff", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg5))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sg5, Player.Character, 1223, -0.01f, 0.18f, -0.15f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sg5);
                   }
               }
               if (EqSlot_1 == 21)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr2) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", gr2))
                   {
                       Delete_obj();
                       gr2 = World.CreateObject("w_e1_glauncher", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr2))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", gr2, Player.Character, 1223, 0.0f, 0.15f, 0.05f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", gr2);
                   }
               }
           }
           if (episodes == 2)
           {
               if (EqSlot_1 == 32)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sm1) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sm1))
                   {
                       Delete_obj();
                       sm1 = World.CreateObject("w_e2_p90", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sm1))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sm1, Player.Character, 1223, 0.05f, 0.13f, 0.05f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sm1);
                   }
               }
               if (EqSlot_1 == 30)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg2) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sg2))
                   {
                       Delete_obj();
                       sg2 = World.CreateObject("w_e2_aa12_exp", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg2))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sg2, Player.Character, 1223, 0.02f, 0.15f, 0.1f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sg2);
                   }
               }
               if (EqSlot_1 == 31)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg3) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sg3))
                   {
                       Delete_obj();
                       sg3 = World.CreateObject("w_e2_aa12", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sg3))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sg3, Player.Character, 1223, 0.02f, 0.15f, 0.1f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sg3);
                   }
               }
               if (EqSlot_1 == 34)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar3) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", ar3))
                   {
                       Delete_obj();
                       ar3 = World.CreateObject("w_e2_m249", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", ar3))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", ar3, Player.Character, 1223, 0.07f, 0.15f, 0.1f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", ar3);
                   }
               }
               if (EqSlot_1 == 35)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn3) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", sn3))
                   {
                       Delete_obj();
                       sn3 = World.CreateObject("w_e2_dsr1", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", sn3))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", sn3, Player.Character, 1223, 0.03f, 0.15f, 0.3f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", sn3);
                   }
               }
               if (EqSlot_1 == 21)
               {
                   if (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr1) || !GTA.Native.Function.Call<bool>("IS_OBJECT_ATTACHED", gr1))
                   {
                       Delete_obj();
                       gr1 = World.CreateObject("w_e2_glauncher", Player.Character.Position.Around(99.0f));
                       while (!GTA.Native.Function.Call<bool>("DOES_OBJECT_EXIST", gr1))
                       {
                           Wait(0);
                       }
                       GTA.Native.Function.Call("ATTACH_OBJECT_TO_PED", gr1, Player.Character, 1223, 0.0f, 0.15f, 0.05f, 210.1f, 30.1f, 0.0, 0);
                       GTA.Native.Function.Call("MARK_OBJECT_AS_NO_LONGER_NEEDED", gr1);
                   }
               }
           }

       }

       private void Delete_obj()
       {
           sg1.Delete();
           ar1.Delete();
           ar2.Delete();
           sn1.Delete();
           sn2.Delete();

           episodes = (int)Game.CurrentEpisode;
           if (episodes == 1)
           {
               gr2.Delete();
               sg4.Delete();
               sg5.Delete();
           }
           if (episodes == 2)
           {
               sm1.Delete();
               sg2.Delete();
               sg3.Delete();
               ar3.Delete();
               sn3.Delete();
               gr1.Delete();
           }
       }


   }
}