using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using Facepunch;
using Network;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Configuration;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using Oxide.Game.Rust;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

namespace Oxide.Plugins
{
    [Info("PortalToRaidableBase", "Marte6", "1.0.0")]
    [Description("PortalToRaidableBase")]
    class PortalToRaidableBase : RustPlugin
    {
        private const string PortalPrefab = "assets/prefabs/missions/portal/halloweenportalentry.prefab";
        private BasePortal entryPortal;
        private BasePortal exitPortal;

        private void OnRaidableBaseStarted(Vector3 raidPos, int mode, bool allowPVP)
        {
            Puts("Criar portal em: " + raidPos);
        }

        [ChatCommand("entrar")]
        private void CreateEntryPortalCommand(BasePlayer player, string command, string[] args)
        {
            if (entryPortal != null)
            {
                player.ChatMessage("Um portal de entrada já existe. Use /removeportals para removê-los.");
                return;
            }

            Vector3 position = GetPlayerFrontPosition(player, 3);
            entryPortal = CreatePortal(position, player.transform.rotation);
            player.ChatMessage("Portal de entrada criado.");
        }

        [ChatCommand("sair")]
        private void CreateExitPortalCommand(BasePlayer player, string command, string[] args)
        {
            if (exitPortal != null)
            {
                player.ChatMessage("Um portal de saída já existe. Use /removeportals para removê-los.");
                return;
            }

            Vector3 position = GetPlayerFrontPosition(player, 3);
            exitPortal = CreatePortal(position, player.transform.rotation);
            player.ChatMessage("Portal de saída criado.");

            // Link portals if both have been created
            if (entryPortal != null)
            {
                LinkPortals();
            }
        }

        [ChatCommand("removeportals")]
        private void RemovePortals(BasePlayer player, string command, string[] args)
        {
            DestroyPortals();
            player.ChatMessage("Portais removidos.");
        }

        private void LinkPortals()
        {
            if (entryPortal != null && exitPortal != null)
            {
                entryPortal.targetPortal = exitPortal;
                exitPortal.targetPortal = entryPortal;

                entryPortal.LinkPortal();
                exitPortal.LinkPortal();
            }
        }

        private BasePortal CreatePortal(Vector3 position, Quaternion rotation)
        {
            var entity = GameManager.server.CreateEntity(PortalPrefab, position) as BasePortal;
            if (entity != null)
            {
                entity.transform.rotation = rotation;
                entity.Spawn();
            }
            return entity;
        }

        private Vector3 GetPlayerFrontPosition(BasePlayer player, float distance)
        {
            return player.transform.position + (player.eyes.BodyForward() * distance);
        }

        private void DestroyPortals()
        {
            entryPortal?.Kill();
            exitPortal?.Kill();
            entryPortal = null;
            exitPortal = null;
        }

        private void Unload()
        {
            DestroyPortals();
        }

        private object OnPortalUseSSS(BasePlayer player, BasePortal portal)
        {
            Puts("OnPortalUse");
            //portal?.Kill();
            return null;
        }
    }
}