using UnityEngine;
namespace Passer {
    using Humanoid;
    /// The teleporter is a simple tool to teleport transforms
    [HelpURLAttribute("https://passervr.com/documentation/humanoid-control/tools/teleport/")]
    public class Teleporter : InteractionPointer {
        ///  Determines how the Transform is moved to the Target Point.
        public enum TransportType {
            Teleport,   //< Direct placement on the target point
            Dash        //< A quick movement in a short time from the originating point to the target point
        }
        /// The TransportType to use when teleporting.
        public TransportType transportType = TransportType.Teleport;
        /// The transform which will be teleported
        public Transform transformToTeleport;
        protected HumanoidControl humanoid;
        protected override void Awake() {
            base.Awake();
            if (transformToTeleport == null)
                transformToTeleport = this.transform;
            humanoid = transformToTeleport.GetComponent();
            if (humanoid == null)
                humanoid = transformToTeleport.GetComponentInParent();
            if (humanoid != null)
                transformToTeleport = humanoid.transform;
        }
        /// Teleport the transform
        public virtual void TeleportTransform() {
            if (transformToTeleport == null)
                transformToTeleport = this.transform;
            if (humanoid == null)
                transformToTeleport.Teleport(focusPointObj.transform.position);
            else {
                Vector3 interactionPointerPosition = humanoid.GetHumanoidPosition() - transformToTeleport.position;
                switch (transportType) {
                    case TransportType.Teleport:
                        transformToTeleport.Teleport(focusPointObj.transform.position - interactionPointerPosition);
                        break;
                    case TransportType.Dash:
                        StartCoroutine(TransformMovements.DashCoroutine(transformToTeleport, focusPointObj.transform.position - interactionPointerPosition));
                        break;
                    default:
                        break;
                }
            }
        }
        /// Adds a default Teleporter to the transform
        /// The transform to which the Teleporter will be added
        /// The interaction pointer type for the Teleporter
        public static new Teleporter Add(Transform parentTransform, PointerType pointerType = PointerType.Ray) {
            GameObject pointerObj = new GameObject("Teleporter");
            pointerObj.transform.SetParent(parentTransform, false);
            GameObject destinationObj = new GameObject("Destination");
            destinationObj.transform.SetParent(pointerObj.transform);
            destinationObj.transform.localPosition = Vector3.zero;
            destinationObj.transform.localRotation = Quaternion.identity;
            if (pointerType == PointerType.FocusPoint) {
                GameObject focusPointSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                focusPointSphere.transform.SetParent(destinationObj.transform);
                focusPointSphere.transform.localPosition = Vector3.zero;
                focusPointSphere.transform.localRotation = Quaternion.identity;
                focusPointSphere.transform.localScale = Vector3.one * 0.1F;
            }
            else {
                LineRenderer pointerRay = destinationObj.AddComponent();
                pointerRay.startWidth = 0.01F;
                pointerRay.endWidth = 0.01F;
                pointerRay.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                pointerRay.receiveShadows = false;
                pointerRay.useWorldSpace = false;
            }
            Teleporter teleporter = pointerObj.AddComponent();
            teleporter.focusPointObj = destinationObj;
            teleporter.rayType = RayType.Bezier;
            return teleporter;
        }
        public override void Click(bool clicking) {
            if (clicking)
                TeleportTransform();
        }
    }
}