using System.Collections.Generic;

namespace Oxide.Plugins
{
    [Info("Vehicle Locks", "MrPoundsign", "1.0.6")]
    [Description("Vehicle ownership and locking")]
    class VehicleLocks : RustPlugin
    {
        const string chinookPilotSeat = "assets/prefabs/vehicle/seats/pilotseat.prefab";
        const string sedanDriverSeat = "assets/prefabs/vehicle/seats/driverseat.prefab";
        const string smallBoatDriverSeat = "assets/prefabs/vehicle/seats/smallboatdriver.prefab";
        const string miniHeliSeat = "assets/prefabs/vehicle/seats/miniheliseat.prefab";
        const string rhibDriverSeat = "assets/prefabs/vehicle/seats/standingdriver.prefab";
        const string saddlePrefab = "assets/prefabs/vehicle/seats/horsesaddle.prefab";
        const string scrapTransportSeat = "assets/prefabs/vehicle/seats/transporthelipilot.prefab";

        protected override void LoadDefaultMessages()
        {
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["vehicle.locked"] = "You don't have the keys for this vehicle",
                ["vehicle.owned"] = "You now own this vehicle",
            }, this);
        }

        object CanMountEntity(BasePlayer player, BaseMountable entity)
        {
            if (player.IsNpc) return null;

            // Only restrict to driver's seats
            if (entity.name != chinookPilotSeat &&
                entity.name != sedanDriverSeat &&
                entity.name != smallBoatDriverSeat &&
                entity.name != miniHeliSeat &&
                entity.name != rhibDriverSeat &&
                entity.name != saddlePrefab &&
                entity.name != scrapTransportSeat) return null;

            var vehicle = entity.VehicleParent();

            // If the player already owns the vehicle
            if (vehicle == null || vehicle.OwnerID == player.userID) return null;

            // If the player is in a safe zone, they can always take any vehicle
            // Additionaly, if it's unowned, it can be taken. This sets ownership
            // to the player taking the vehicle.
            if (vehicle.OwnerID == 0 || (!player.InSafeZone() && player.CanBuild()))
            {
                player.ChatMessage(lang.GetMessage("vehicle.owned", this, player.UserIDString));
                vehicle.OwnerID = player.userID;
                return null;
            }

            // Players who are standing in a place they are authorized to build,
            // with or without TC coverage, they can take a vehicle.
            var bp = player.GetBuildingPrivilege();
            if (bp != null)
            {
                // At this point we need to determine if the owner is authorized to the
                // tool cupboard providing the build privileges. This is so if someone
                // parks their vehicle at an enemy base, anyone can take it.
                foreach (ulong playerID in bp.authorizedPlayers)
                {
                    if (playerID != vehicle.OwnerID) continue;
                    player.ChatMessage(lang.GetMessage("vehicle.locked", this, player.UserIDString));
                    return true;
                }
            }

            // All other checks have failed, the vehicle is theirs to take.
            player.ChatMessage(lang.GetMessage("vehicle.owned", this, player.UserIDString));
            vehicle.OwnerID = player.userID;
            return null;
        }
    }
}
