﻿#region Namespaces
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using UnityEngine.UI;
using System;
#endregion Namespaces
/// <summary>
/// Script from Reimajo, purchased at https://reimajo.booth.pm/
/// Join my Discord Server to receive update notifications & support for this asset: https://discord.gg/SWkNA394Mm
/// There is a version of this tool available with advanced protection, see my discord for more details. 
/// If you have any issues, please contact me on Discord or Booth or Twitter https://twitter.com/ReimajoChan
/// </summary>
namespace ReimajoBoothAssets
{
    /// <summary>
    /// Mods can disable pickups (which is not an issue or harmful in general), so we need to put the real logic somewhere else. This
    /// also allows easier Quest build sync & the pickup itself doesn't need to be included in the quest build if not needed.
    /// 
    /// Class name, public and serialized variable names, as well as exposed events on this panel have a name that is not
    /// easy to understand. This is to provide a minimal layer of security on top so that
    /// people with mods that can display those don't understand what they are doing and have a harder time messing around with it.
    /// 
    /// This script can be a single time in the world.
    /// You should keep "Synchronize Position" enabled for this script (should be default on unless you disable it manually).
    /// You should NOT have "Transfer Ownership on Collision" enabled for this script.
    /// </summary>
    public class AdminPanel : UdonSharpBehaviour
    {
        /// <summary> 
        /// The real admin panel, name is obfuscated since it can be read out via mods (in theory), although most mods just read out the GameObject name instead
        /// </summary>
        [SerializeField, Tooltip("The real admin panel, name is obfuscated since it can be read out via mods (in theory), although most mods just read out the GameObject name instead.")]
        private PickupSync _pickupTracker;
        /// <summary>
        /// Internal copy which reads the serialized field at start before that one is nulled to protect it
        /// </summary>
        private PickupSync _pickupTrackerCopy;
        private VRC_Pickup _vrcPickup;
        private void Start()
        {
            if(!Utilities.IsValid(_pickupTracker))
            {
                Debug.LogError($"[Analytics] There is no PickupSync script assigned to the Panel Pickup.");
                this.gameObject.SetActive(false);
                return;
            }
            _vrcPickup = (VRC_Pickup)GetComponent(typeof(VRC_Pickup));
            _pickupTrackerCopy = _pickupTracker;
            _pickupTracker = null; //let's not expose the admin panel script
        }
        #region PickupEvents
        /// <summary>
        /// Called when the panel is picked up
        /// </summary>
        public override void OnPickup()
        {
            _pickupTrackerCopy._OnRelayedPickup(isHeldWithLeftHand: _vrcPickup.currentHand == VRC_Pickup.PickupHand.Left);
        }
        /// <summary>
        /// Called when the panel is dropped
        /// </summary>
        public override void OnDrop()
        {
            _pickupTrackerCopy._OnRelayedDrop();
        }
        #endregion PickupEvents
        #region HoneyPots
        [HideInInspector]
        public bool _isAdmin = false; //<- does absolutely nothing, only there to confuse hackers
        /// <summary>
        /// Those are traps. All those stupid script kiddies with their malicious mods tend to randomly call public methods
        /// on a script to see what happens, if they call this one they will be disconnected by VRChat for "unusual client behaviour".
        /// This method starts with an underscore so it cannot be called on the network which is very important.
        /// </summary>
        public void _EnableAdminTools()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _MakeLocalPlayerAdmin()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _BanSelectedPlayer()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _SelectNextPlayer()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _BanAllOtherPlayersExceptMe()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _EnableFlightMode()
        {
            _pickupTrackerCopy._EnableForLocalPlayer(); //<- it's a trap!
        }
        public void _Pressed_NextPlayer()
        {
            _pickupTrackerCopy._RegisterPickup(1); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_PreviousPlayer()
        {
            _pickupTrackerCopy._RegisterPickup(2); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_ArmBanButton()
        {
            _pickupTrackerCopy._RegisterPickup(3); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_ArmUnbanButton()
        {
            _pickupTrackerCopy._RegisterPickup(4); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_ArmUnbanAllButton()
        {
            _pickupTrackerCopy._RegisterPickup(5); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_UnbanButton()
        {
            _pickupTrackerCopy._RegisterPickup(6); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_UnbanAll()
        {
            _pickupTrackerCopy._RegisterPickup(7); //<- it's a trap for non-admins, but works fine for admins
        }
        public void _Pressed_BanButton()
        {
            _pickupTrackerCopy._RegisterPickup(8); //<- it's a trap for non-admins, but works fine for admins
        }
        #endregion HoneyPots
    }
}