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;

    private bool holdingWand;

    public AudioSource noEssenceAudio;

    void Start()
    {
        player = gameObject;
    }

    public void PerformAction(string action)
    {
        Debug.LogWarning(action);

        if (player.GetComponent<PlayerInfo>().GetRightHand() != null) 
        {
            if (player.GetComponent<PlayerInfo>().GetRightHand().name.Equals("wand")) holdingWand = true;
        }
        else holdingWand = false;
        

        switch (action)
        {
            case "HorizontalLine":
                if (holdingWand)
                {
                    Debug.LogWarning("CAST BOLT");
                    ShootProjectile();
                    InstantiateIceBolt(objects[0]);
                }
                else
                {
                    Debug.LogWarning("WOODCUTTING ACTION");
                }
                break;
            case "VerticalLine":
                if (holdingWand)
                {
                    Debug.LogWarning("WAND VERTICAL");
                    if (PlayerInfo.Instance.AddEssenceBasic(-1))
                    {
                        Vector3 spawnPoint = transform.position + playerCamera.transform.forward;
                        spawnPoint = new Vector3(spawnPoint.x, spawnPoint.y + 1, spawnPoint.z);
                        GameObject shield = Instantiate(objects[1], spawnPoint, Quaternion.Euler(-90, playerCamera.transform.eulerAngles.y - 180, 180));
                        Destroy(shield, 5);
                    }
                    else
                    {
                        noEssenceAudio.Play();
                    }
                }
                else
                {
                    Debug.LogWarning("VERTICAL");

                }
                break;
            case "Circle":
                if (holdingWand)
                {
                    Debug.LogWarning("Arcing fireball");

                }
                else
                {
                    GameObject minigame = FindMinigame();
                    if (minigame != null)
                    {
                        float distance = Vector3.Distance(transform.position, minigame.transform.position);
                        if(distance < 4f) minigame.GetComponent<WellController>().StartMinigame();
                    }

                }
                break;
            case "Triangle":
                if (holdingWand)
                {
                    Debug.LogWarning("WAND TRIANGLE");
                }
                else
                {
                    Debug.LogWarning("WOODCUTTING ACTION");
                }
                break;
        }

        /*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 MinigameEnable(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 FindMinigame()
    {
        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) 
    {
        Debug.LogWarning("INSTANTIATE BOLT");
        var projectileObj = Instantiate(item, rightHandTransform.position, playerCamera.transform.rotation) as GameObject;
        projectileObj.GetComponent<Rigidbody>().velocity = (playerCamera.transform.forward).normalized * projectileSpeed;
    }
}