using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActionGestureInteraction : MonoBehaviour
{
    public List<GameObject> objects;
    public Camera playerCamera;
    private Vector3 destination;

    public Transform rightHandTransform;
    private GameObject player;
    public float projectileSpeed = 30;

    void Start()
    {
        player = gameObject;
    }

    public void PerformAction(string action)
    {
        Debug.Log(action);
        if (action == "Portal")
        {
            // Raycast to find portal were looking at.
            var nearestPortal = FindPortalInFront();
            Debug.Log(nearestPortal);
            EnableDisablePortal(nearestPortal);
        }
        else 
        {
            foreach (var item in objects)
            {
                if (item.name == action && action == "IceBolt")
                {
                    ShootProjectile();
                    InstantiateIceBolt(item);
                }
                if (item.name == action && action == "IceWall")
                {
                    // Make ice wall appear from below to block incoming projectiles
                }
            }
        }  
    }
    void EnableDisablePortal(GameObject portal)
    {
        // Did the raycast catch a portal in front of us?
        if (portal != null)
        {
            var distance = Vector3.Distance(portal.transform.position, this.transform.position);
            Debug.Log(distance);
            // Is the portal within a reasonable distance?
            if (distance <= 10.0f)
            {
                var portalVFX = portal.transform.Find("PortalFX");
                var portalTrigger = portal.transform.Find("Portal");
                // if the nearest portal is already enabled, then disable, else enable.
                if (portalVFX.gameObject.activeInHierarchy)
                {
                    portalVFX.gameObject.SetActive(false);
                    portalTrigger.gameObject.GetComponent<PortalTeleporter>().enabled = false;
                }
                else
                {
                    portalVFX.gameObject.SetActive(true);
                    portalTrigger.gameObject.GetComponent<PortalTeleporter>().enabled =true;
                }
            }
        }
    }

    GameObject FindPortalInFront() 
    {
        Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;
        LayerMask playerLayerMask = LayerMask.GetMask("Player");
        if (Physics.Raycast(ray, out hit, playerLayerMask))
        {

            if (hit.transform.gameObject.transform.root.CompareTag("Portal"))
            {
                return hit.transform.gameObject.transform.root.gameObject;
            }
        }
        return null;
    }

    void ShootProjectile() 
    {
        Ray ray = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            destination = hit.point;
        }
        else
        {
            destination = ray.GetPoint(1000);
        }

    }

    void InstantiateIceBolt(GameObject item) 
    {
        var projectileObj = Instantiate(item, rightHandTransform.position, Quaternion.identity) as GameObject;
        projectileObj.GetComponent<Rigidbody>().velocity = (destination - rightHandTransform.position).normalized * projectileSpeed;
    }
}