using GTA; // This is a reference that is needed! do not edit this
using GTA.Native; // This is a reference that is needed! do not edit this
using System; // This is a reference that is needed! do not edit this
using System.Windows.Forms; // This is a reference that is needed! do not edit this
using System.Collections.Generic;

namespace TruenoHeadlights
{
    public class TruenoHeadlights : Script // declare Modname as a script
    {
        //Automatic Pop-up Headlights for various add-on vehicles 1.8.1


        //The following lines are the only lines you need to edit. I HIGHLY recommend using Notepad++ to edit.

        static bool PedVehiclesAlso = true;
        //If true, this mod will also open the pop-up headlights of pedestrian vehicles at night. If false, this mod will only work for your vehicle.

        //The following lines are used for editing which cars will be affected by this mod. Each List name (Ex: BackLeftDoorVehicles, BackRightDoorVehicles) refers to the door that is set as each vehicle's headlights.
        //For example, the vehicle "ae86" has headlights that are set as the left rear door, so that is why "ae86" is written inside the BackLeftDoorVehicles list.


        List<string> BackLeftDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Back Left Door, write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "ae86","trueno","rx7","tofu","rx7rb","TSUP3","miata3","diablo","nsxtypr","f40","ntypex"
    };

        List<string> BackRightDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Back Right Door, write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "EXAMPLE","EXAMPLE"
    };

        List<string> HoodDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Hood, write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "nsx2","EXAMPLE"
    };

        List<string> TrunkDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Trunk, write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "EXAMPLE","EXAMPLE"
    };

        List<string> Trunk2DoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the second Trunk, write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "EXAMPLE","EXAMPLE"
    };

        List<string> FrontLefttDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Front Left Door (Driver), write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "EXAMPLE","EXAMPLE"
    };

        List<string> FrontRightDoorVehicles = new List<string>
    {
		//For vehicles with pop-up headlights set as the Front Right Door (Passenger), write their spawn name in the following line. You must separate the vehicle names with a comma, and write the name inside quotation marks (" ").
		
        "EXAMPLE","EXAMPLE"
    };
        //Do not touch anything below this line!!



        VehicleDoor Headlights;
        VehicleDoor CivHeadlights;
        int CloseDoorTimer;
        Vehicle veh;
        Vehicle[] allVeh;

        public TruenoHeadlights() // main function
        {
            Tick += OnTick;
            Interval = 10;
        }

        private void OnTick(object sender, EventArgs e) // This is where most of your script goes
        {

            Ped player = Game.Player.Character; // player variable
            veh = player.CurrentVehicle; // vehicle variable
            allVeh = World.GetNearbyVehicles(player.Position, 30f);

            if (Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, player, false))
            {
                PersonalVehicleSupport(veh);

                if (CloseDoorTimer > 1 && CloseDoorTimer < 5)
                {
                    veh.CloseDoor(Headlights, false);
                }
            }

            if (PedVehiclesAlso)
            {
                if (IsNightTime() == true)
                {
                    for (int i = 0; i < allVeh.Length; i++)
                    {
                        if (allVeh[i] != veh)
                        {
                            if (allVeh[i].EngineRunning == true)
                            {
                                CivilianVehicleSupport(allVeh[i]);
                            }
                        }
                    }
                }
            }
        }

        bool IsAnyLightOn()
        {
            return ((Game.Player.Character.CurrentVehicle.LightsOn == true) || (Game.Player.Character.CurrentVehicle.HighBeamsOn == true));
        }

        public void PersonalVehicleSupport(Vehicle v)
        {
            foreach (string BLD in BackLeftDoorVehicles)
            {
                if (v.Model == BLD)
                {
                    Headlights = GTA.VehicleDoor.BackLeftDoor;
                    OpenOrClose();
                }
            }

            foreach (string HD in HoodDoorVehicles)
            {
                if (v.Model == HD)
                {
                    Headlights = GTA.VehicleDoor.Hood;
                    OpenOrClose();
                }
            }

            foreach (string BRD in BackRightDoorVehicles)
            {
                if (v.Model == BRD)
                {
                    Headlights = GTA.VehicleDoor.BackRightDoor;
                    OpenOrClose();
                }
            }

            foreach (string FLD in FrontLefttDoorVehicles)
            {
                if (v.Model == FLD)
                {
                    Headlights = GTA.VehicleDoor.FrontLeftDoor;
                    OpenOrClose();
                }
            }

            foreach (string FRD in FrontRightDoorVehicles)
            {
                if (v.Model == FRD)
                {
                    Headlights = GTA.VehicleDoor.FrontRightDoor;
                    OpenOrClose();
                }
            }

            foreach (string TD1 in TrunkDoorVehicles)
            {
                if (v.Model == TD1)
                {
                    Headlights = GTA.VehicleDoor.Trunk;
                    OpenOrClose();
                }
            }

            if (v.Model == "240sxbn")
            {
                Headlights = GTA.VehicleDoor.BackLeftDoor;
                OpenOrClose240sxbn();
            }

            //foreach (string TD2 in Trunk2DoorVehicles)
            //{
            //    if (v.Model == TD2)
            //    {
            //        Headlights = GTA.VehicleDoor.Trunk2;
            //        OpenOrClose();
            //    }
            //}
        }

        public void CivilianVehicleSupport(Vehicle c)
        {
            foreach (string BLD in BackLeftDoorVehicles)
            {
                if (c.Model == BLD)
                {
                    CivHeadlights = GTA.VehicleDoor.BackLeftDoor;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            foreach (string HD in HoodDoorVehicles)
            {
                if (c.Model == HD)
                {
                    CivHeadlights = GTA.VehicleDoor.Hood;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            foreach (string BRD in BackRightDoorVehicles)
            {
                if (c.Model == BRD)
                {
                    CivHeadlights = GTA.VehicleDoor.BackRightDoor;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            foreach (string FLD in FrontLefttDoorVehicles)
            {
                if (c.Model == FLD)
                {
                    CivHeadlights = GTA.VehicleDoor.FrontLeftDoor;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            foreach (string FRD in FrontRightDoorVehicles)
            {
                if (c.Model == FRD)
                {
                    CivHeadlights = GTA.VehicleDoor.FrontRightDoor;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            foreach (string TD1 in TrunkDoorVehicles)
            {
                if (c.Model == TD1)
                {
                    CivHeadlights = GTA.VehicleDoor.Trunk;
                    c.OpenDoor(CivHeadlights, true, false);
                }
            }

            //foreach (string TD2 in Trunk2DoorVehicles)
            //{
            //    if (c.Model == TD2)
            //    {
            //        CivHeadlights = GTA.VehicleDoor.Trunk2;
            //        c.OpenDoor(CivHeadlights, true, false);
            //    }
            //}
        }

        public void OpenOrClose()
        {
            if (IsAnyLightOn())
            {
                Game.Player.Character.CurrentVehicle.OpenDoor(Headlights, true, false);
                CloseDoorTimer = 0;
            }
            else
            {
                if (CloseDoorTimer < 5)
                    CloseDoorTimer++;
            }
        }

        public static bool IsNightTime() //credits to Eddlm for this method.
        {
            int hour = Function.Call<int>(Hash.GET_CLOCK_HOURS);
            return (hour > 20 || hour < 7);
        }

        public void OpenOrClose240sxbn()
        {
            if (!IsAnyLightOn())
            {
                Game.Player.Character.CurrentVehicle.OpenDoor(Headlights, true, false);
                CloseDoorTimer = 0;
            }
            else
            {
                if (CloseDoorTimer < 5)
                    CloseDoorTimer++;
            }
        }
    }
}