using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GTA;
using GTA.Native;
using Menu = GTA.Menu;
using MenuItem = GTA.MenuItem;

namespace SpawnAnything
{
    public class SpawnAnything : Script
    {
        private bool _isMenuOpen;
        private Viewport _viewport;
        private List<MenuItem> _menuItems;
        private int _selectedItem;
        private bool _infiniteHealth;
        private bool _neverWanted;
        private int _branch;
        public SpawnAnything()
        {
            Tick += TestMod_Tick;
            KeyUp += TestMod_KeyUp;
            KeyDown += TestMod_KeyDown;
        }

        void TestMod_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
        }

        private void TestMod_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.H)
            {
                if (!_isMenuOpen)
                {
                    MainMenu();
                }
                else
                {
                    _isMenuOpen = false;
                }
                return;
            }
            if (_isMenuOpen)
            {
                switch (e.KeyCode)
                {
                    case Keys.NumPad5:
                        _menuItems[_selectedItem].Activate();
                        break;
                    case Keys.NumPad2:
                        if (_selectedItem + 1 <= _menuItems.Count - 1)
                        {
                            _menuItems[_selectedItem].Deselect();
                            _menuItems[++_selectedItem].Select();
                        }
                        break;
                    case Keys.NumPad8:
                        if (_selectedItem - 1 >= 0)
                        {
                            _menuItems[_selectedItem].Deselect();
                            _menuItems[--_selectedItem].Select();
                        }
                        break;
                    case Keys.Back:
                        PreviousMenu();
                        break;
                }
            }
            //else if (e.KeyCode == Keys.NumPad5)
            //{
            //    this._menuItems[this.selectedButton].Activate();
            //    Game.PlaySound("CANCEL", "HUD_FRONTEND_CLOTHESSHOP_SOUNDSET");
            //}
            //else if (e.KeyCode == Keys.NumPad2)
            //{
            //    this.NextButton();
            //}
            //else
            //{
            //    if (e.KeyCode != Keys.NumPad8)
            //        return;
            //    this.PreviousButton();
            //}
        }

        private void PreviousMenu()
        {
            switch (_branch)
            {
                case 1:
                    _isMenuOpen = false;
                    break;
                case 2:
                    MainMenu();
                    break;
            }
        }

        void TestMod_Tick(object sender, EventArgs e)
        {
            if (_isMenuOpen)
            {
                _viewport.Draw();
            }
            if (_neverWanted)
            {
                Game.Player.WantedLevel = 0;
            }
            if (_infiniteHealth)
            {
                int handle = Game.Player.Character.Handle;
                Function.Call(Hash.SET_ENTITY_HEALTH, new InputArgument(handle), new InputArgument(100 + 100));
                Function.Call(Hash.SET_PED_ARMOUR, new InputArgument(handle), new InputArgument(100));
            }
        }

        private void SpawnAircraftAndHelicopter(VehicleHash vehicleHash)
        {
            Vehicle vehicle = World.CreateVehicle(vehicleHash, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 6.0f, Game.Player.Character.Heading + 90);
            vehicle.PlaceOnGround();
            _isMenuOpen = false;
        }

        private void SpawnMilitary(VehicleHash vehicleHash)
        {
            Vehicle vehicle = World.CreateVehicle(vehicleHash, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 6.0f, Game.Player.Character.Heading + 90);
            vehicle.PlaceOnGround();
            _isMenuOpen = false;
        }

        private void SpawnSuperFastCar(VehicleHash vehicleHash)
        {
            Vehicle vehicle = World.CreateVehicle(vehicleHash, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 4.0f, Game.Player.Character.Heading + 90);
            vehicle.NumberPlate = "Spawn";
            vehicle.EngineRunning = true;
            vehicle.PrimaryColor = VehicleColor.MetallicOrange;
            vehicle.SecondaryColor = VehicleColor.MetallicWhite;
            Function.Call(Hash.SET_VEHICLE_MOD_KIT, vehicle.Handle, 0);
            if (vehicleHash == VehicleHash.Zentorno)
            {

                vehicle.SetMod(VehicleMod.Spoilers, 6, true);
                vehicle.SetMod(VehicleMod.Exhaust, 3, true);
                vehicle.SetMod(VehicleMod.SideSkirt, 2, true);
                vehicle.WheelType = VehicleWheelType.HighEnd;
                vehicle.SetMod(VehicleMod.BackWheels, 18, true);
                vehicle.SetMod(VehicleMod.FrontWheels, 18, true);

            }

            vehicle.WindowTint = VehicleWindowTint.Limo;
            vehicle.CanTiresBurst = false;
            vehicle.SetMod(VehicleMod.Armor, 4, true);
            vehicle.SetMod(VehicleMod.Brakes, 2, true);
            vehicle.SetMod(VehicleMod.Engine, 3, true);
            vehicle.SetMod(VehicleMod.Transmission, 2, true);

            vehicle.SetNeonLightsOn(VehicleNeonLight.Back, true);
            vehicle.SetNeonLightsOn(VehicleNeonLight.Front, true);
            vehicle.SetNeonLightsOn(VehicleNeonLight.Left, true);
            vehicle.SetNeonLightsOn(VehicleNeonLight.Right, true);
            vehicle.NeonLightsColor = Color.White;

            vehicle.PlaceOnGround();
            _isMenuOpen = false;
        }

        private void SpawnMotorcycles(VehicleHash vehicleHash)
        {
            Vehicle vehicle = World.CreateVehicle(vehicleHash, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 3.0f, Game.Player.Character.Heading + 90);
            vehicle.NumberPlate = "Spawn";
            vehicle.EngineRunning = true;
            //vehicle.PrimaryColor = VehicleColor.MetallicOrange;
            //vehicle.SecondaryColor = VehicleColor.MetallicWhite;
            Function.Call(Hash.SET_VEHICLE_MOD_KIT, vehicle.Handle, 0);



            vehicle.PlaceOnGround();
            _isMenuOpen = false;
        }

        private void SpawnBoat(VehicleHash vehicleHash)
        {
            Vehicle vehicle = World.CreateVehicle(vehicleHash, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 4.0f, Game.Player.Character.Heading + 90);
            vehicle.NumberPlate = "Spawn";
            vehicle.EngineRunning = true;
            vehicle.PlaceOnGround();
            _isMenuOpen = false;
        }



        private void ModifyWantedLevel(int level)
        {
            if (level == -1)
            {
                _neverWanted = !_neverWanted;
                if (_neverWanted)
                {
                    Game.Player.WantedLevel = 0;
                    UI.ShowSubtitle("Never Wanted mode : On");
                }
                else
                {
                    UI.ShowSubtitle("Never Wanted mode : Off");
                }
            }
            else
            {
                Game.Player.WantedLevel = level;
            }
            _isMenuOpen = false;
        }

        private void AddMoney(int amount)
        {
            Game.Player.Money += amount;
            _isMenuOpen = false;

        }

        private void ModifyHealthAndArmor(bool? infinite)
        {
            if (infinite.HasValue)
            {
                _infiniteHealth = !_infiniteHealth;
                UI.ShowSubtitle(_infiniteHealth ? "Infinite Health and Armor mode : On" : "Infinite Health and Armor : Off");
            }
            else
            {
                int handle = Game.Player.Character.Handle;
                Function.Call(Hash.SET_ENTITY_HEALTH, new InputArgument(handle), new InputArgument(100 + 100));
                Function.Call(Hash.SET_PED_ARMOUR, new InputArgument(handle), new InputArgument(100));
                UI.ShowSubtitle("Health and Armor are full now.");
            }
            _isMenuOpen = false;
        }

        private void PrepareMenuPhase1()
        {
            _menuItems.Clear();
            _selectedItem = 0;
        }

        private void PrepareMenuPhase2(string caption)
        {
            Menu menu = new Menu(String.Format("Spawn Anything - {0}", caption), _menuItems.ToArray()) { Width = 400 };
            _viewport = new Viewport();
            _viewport.AddMenu(menu);
            _branch = 2;
        }

        private void ShowAircrafts()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Blimp),
                new VehicleDes(VehicleHash.CargoPlane, "Cargo Plane"),
                new VehicleDes(VehicleHash.Cuban800, "Cuban 800"),
                new VehicleDes(VehicleHash.Dodo),
                new VehicleDes(VehicleHash.Duster),
                new VehicleDes(VehicleHash.Luxor),
                new VehicleDes(VehicleHash.Mammatus),
                new VehicleDes(VehicleHash.Miljet),
                new VehicleDes(VehicleHash.Titan),
                new VehicleDes(VehicleHash.Velum),
                new VehicleDes(VehicleHash.Vestra)
            };
            _menuItems.AddRange(items.Select(s => new MenuButton(s.Name, () => SpawnAircraftAndHelicopter(s.VehicleHash))));
            PrepareMenuPhase2("Aircrafts");
        }

        private void ShowSuperFastCars()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Adder),
                new VehicleDes(VehicleHash.Bullet),
                new VehicleDes(VehicleHash.Cheetah),
                new VehicleDes(VehicleHash.EntityXF),
                new VehicleDes(VehicleHash.Infernus),
                new VehicleDes(VehicleHash.Turismor, "Turismo R"),
                new VehicleDes(VehicleHash.Vacca),
                new VehicleDes(VehicleHash.Voltic),
                new VehicleDes(VehicleHash.Zentorno),
            };
            _menuItems.AddRange(items.Select(s => new MenuButton(s.Name, () => SpawnSuperFastCar(s.VehicleHash))));
            PrepareMenuPhase2("Super Fast Cars");
        }

        private void ShowBoats()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Dinghy),
                new VehicleDes(VehicleHash.Dinghy2, "Dinghy 2"),
                new VehicleDes(VehicleHash.Speeder),
                new VehicleDes(VehicleHash.Predator),
                new VehicleDes(VehicleHash.Jetmax),
                new VehicleDes(VehicleHash.Marquis),
                new VehicleDes(VehicleHash.Squalo),
                new VehicleDes(VehicleHash.Tropic),
                new VehicleDes(VehicleHash.Suntrap)
            };
            _menuItems.AddRange(items.OrderBy(o => o.Name).Select(s => new MenuButton(s.Name, () => SpawnBoat(s.VehicleHash))));
            PrepareMenuPhase2("Boats");
        }


        private void ShowWantedLevel()
        {
            PrepareMenuPhase1();
            _menuItems.Add(new MenuButton("No Wanted Level Now", () => ModifyWantedLevel(0)));
            _menuItems.Add(new MenuButton("1 Star", () => ModifyWantedLevel(1)));
            _menuItems.Add(new MenuButton("2 Stars", () => ModifyWantedLevel(2)));
            _menuItems.Add(new MenuButton("3 Stars", () => ModifyWantedLevel(3)));
            _menuItems.Add(new MenuButton("4 Stars", () => ModifyWantedLevel(4)));
            _menuItems.Add(new MenuButton("5 Stars", () => ModifyWantedLevel(5)));
            _menuItems.Add(!_neverWanted ? new MenuButton("Enable Never Wanted", () => ModifyWantedLevel(-1)) : new MenuButton("Disable Never Wanted", () => ModifyWantedLevel(-1)));
            PrepareMenuPhase2("Wanted Level");
        }

        private void ShowMoney()
        {
            PrepareMenuPhase1();
            _menuItems.Add(new MenuButton("100$", () => AddMoney(100)));
            _menuItems.Add(new MenuButton("500$", () => AddMoney(500)));
            _menuItems.Add(new MenuButton("1000$", () => AddMoney(1000)));
            _menuItems.Add(new MenuButton("5000$", () => AddMoney(5000)));
            _menuItems.Add(new MenuButton("10000$", () => AddMoney(10000)));
            _menuItems.Add(new MenuButton("50000$", () => AddMoney(50000)));
            _menuItems.Add(new MenuButton("100000$", () => AddMoney(100000)));
            _menuItems.Add(new MenuButton("500000$", () => AddMoney(500000)));
            _menuItems.Add(new MenuButton("1000000$", () => AddMoney(1000000)));
            _menuItems.Add(new MenuButton("10000000$", () => AddMoney(10000000)));
            _menuItems.Add(new MenuButton("30000000$", () => AddMoney(30000000)));
            PrepareMenuPhase2("Money");
        }

        private void ShowHelicopters()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Annihilator),
                new VehicleDes(VehicleHash.Buzzard2, "Buzzard"),
                new VehicleDes(VehicleHash.Frogger),
                new VehicleDes(VehicleHash.Frogger2, "Frogger 2"),
                new VehicleDes(VehicleHash.Maverick),
                new VehicleDes(VehicleHash.Savage),
                new VehicleDes(VehicleHash.Skylift),
                new VehicleDes(VehicleHash.Valkyrie)
            };
            _menuItems.AddRange(items.Select(s => new MenuButton(s.Name, () => SpawnAircraftAndHelicopter(s.VehicleHash))));
            PrepareMenuPhase2("Helicoptes");
        }

        private void ShowMotorcycles()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Lectro),
                new VehicleDes(VehicleHash.Akuma),
                new VehicleDes(VehicleHash.Hakuchou),
                new VehicleDes(VehicleHash.Bati, "Bati 800"),
                new VehicleDes(VehicleHash.Bati2, "Bati Custom"),
                new VehicleDes(VehicleHash.Daemon),
                new VehicleDes(VehicleHash.CarbonRS, "Carbon RS"),
                new VehicleDes(VehicleHash.Double, "Double T"),
                new VehicleDes(VehicleHash.Bagger),
                new VehicleDes(VehicleHash.PCJ, "PCJ-600"),
                new VehicleDes(VehicleHash.Sanchez),
                new VehicleDes(VehicleHash.Sanchez2, "Sanchez 2"),
                new VehicleDes(VehicleHash.Thrust),
                new VehicleDes(VehicleHash.Innovation),
                new VehicleDes(VehicleHash.Nemesis),
                new VehicleDes(VehicleHash.Sovereign),
                new VehicleDes(VehicleHash.Faggio2, "Faggio"),
                new VehicleDes(VehicleHash.Enduro),
                new VehicleDes(VehicleHash.Hexer),
                new VehicleDes(VehicleHash.Vader),
                new VehicleDes(VehicleHash.Ruffian)
            };
            _menuItems.AddRange(items.OrderBy(o => o.Name).Select(s => new MenuButton(s.Name, () => SpawnMotorcycles(s.VehicleHash))));
            PrepareMenuPhase2("Motorcycles");
        }

        private void ShowMilitary()
        {
            PrepareMenuPhase1();
            List<VehicleDes> items = new List<VehicleDes>
            {
                new VehicleDes(VehicleHash.Hydra),
                new VehicleDes(VehicleHash.Lazer),
                new VehicleDes(VehicleHash.Rhino),
                new VehicleDes(VehicleHash.Buzzard, "Buzzard Attack"),
                new VehicleDes(VehicleHash.Crusader),
                new VehicleDes(VehicleHash.Barracks),
                new VehicleDes(VehicleHash.Barracks2, "Barracks 2"),
                new VehicleDes(VehicleHash.Dubsta),
                new VehicleDes(VehicleHash.Dubsta2, "Dubsta 2"),
                new VehicleDes(VehicleHash.Dubsta3, "Dubsta 6*6"),
            };
            _menuItems.AddRange(items.OrderBy(o => o.Name).Select(s => new MenuButton(s.Name, () => SpawnMilitary(s.VehicleHash))));
            PrepareMenuPhase2("Military");
        }

        private void ShowHealthAndArmor()
        {
            PrepareMenuPhase1();
            _menuItems.Add(_infiniteHealth ? new MenuButton("Disable Infinite Health and Armor (Except Explosions)", () => ModifyHealthAndArmor(true)) : new MenuButton("Enable Infinite Health and Armor (Except Explosions)", () => ModifyHealthAndArmor(true)));
            _menuItems.Add(new MenuButton("Full Health and Armor", () => ModifyHealthAndArmor(null)));
            PrepareMenuPhase2("Health");
        }

        private void MainMenu()
        {
            _selectedItem = 0;
            _menuItems = new List<MenuItem>
            {
                new MenuButton("Aircrafts", ShowAircrafts),
                new MenuButton("Helicopters", ShowHelicopters),
                new MenuButton("Super Fast Cars", ShowSuperFastCars),
                new MenuButton("Motorcycles", ShowMotorcycles),
                new MenuButton("Boats", ShowBoats),
                new MenuButton("Military", ShowMilitary),
                new MenuButton("Wanted Level", ShowWantedLevel),
                new MenuButton("Money", ShowMoney),
                new MenuButton("Health & Armor", ShowHealthAndArmor)
            };
            _branch = 1;
            Menu menu = new Menu("SPAWN ANYTHING", _menuItems.ToArray()) { Width = 300 };
            _viewport = new Viewport();
            _viewport.AddMenu(menu);
            _isMenuOpen = true;
        }

    }
}
