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
    {
        VehicleDoor Headlights;
        int CloseDoorTimer;
        
        //Automatic Pop-up Headlights for various add-on vehicles 1.7

        //The following lines are the only lines you need to edit. I HIGHLY recommend using Notepad++ to edit.
        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"
    };

        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!!

        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
            Vehicle veh = player.CurrentVehicle; // vehicle variable

            if (Function.Call<bool>(Hash.IS_PED_IN_ANY_VEHICLE, player, false))
            {
                foreach (string BLD in BackLeftDoorVehicles)
                {
                    if (player.CurrentVehicle.Model == BLD)
                    {
                        Headlights = GTA.VehicleDoor.BackLeftDoor;
                        OpenOrClose();
                    }
                }

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

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

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

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

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

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

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

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

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