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

public class InteractableHandler
{
    Camera cam;

    Image tooltipBackground;
    Text tooltip;

    public RaycastHit interactableCheck;
    
    public bool rightClickMenu = false;
    Image rightClickMenuBackground;
    MenuHandler menu;

    public InteractableHandler(Camera cam, Image tooltipBackground, Image rightClickMenuBackground, MenuHandler menu)
    {
        this.cam = cam;
        this.tooltipBackground = tooltipBackground;
        this.tooltip = tooltipBackground.transform.GetChild(0).gameObject.GetComponent<Text>();
        this.rightClickMenuBackground = rightClickMenuBackground;
        this.menu = menu;
    }

    private String CleanName(String name)
    {
        int spaceindex = 0;
        int underscoreindex = 0;

        for (int i = 0; i < name.Length; i++)
        {
            if (name[i] == ' ')
                spaceindex = i;
            if (name[i] == '_')
                underscoreindex = i;
        }

        if (spaceindex == 0)
        {
            if (underscoreindex != 0)
                name = name.Substring(0, underscoreindex) + " " + name.Substring(underscoreindex + 1);
        }
        else
        {
            if (underscoreindex == 0)
                name = name.Substring(0, spaceindex);
            else
                name = name.Substring(0, underscoreindex) + " " + name.Substring(underscoreindex + 1, spaceindex - underscoreindex);
        }

        return name;
    }

    public bool CheckIfInteractable(Ray raycast, float distance)
    {
        if (Physics.Raycast(raycast, out interactableCheck, distance))
        {
            GameObject interactable = interactableCheck.collider.gameObject;
            if (interactable.tag == "Interactable")
            {
                return true;
            }
        }

        return false;
    }

    public void PlaceRightClickMenu()
    {
        rightClickMenuBackground.enabled = true;
        rightClickMenuBackground.transform.GetChild(0).gameObject.SetActive(true);
        rightClickMenuBackground.transform.GetChild(1).gameObject.SetActive(true);
        rightClickMenuBackground.transform.GetChild(2).gameObject.SetActive(true);

        tooltipBackground.enabled = false;
        tooltip.enabled = false;

        menu.SetMenu(CleanName(interactableCheck.collider.gameObject.transform.parent.name));
        
        rightClickMenuBackground.transform.position = Input.mousePosition + new Vector3(rightClickMenuBackground.rectTransform.rect.width / 2, -rightClickMenuBackground.rectTransform.rect.height / 2);
    }

    public void Update()
    {
        if (!rightClickMenu)
        {
            if (CheckIfInteractable(cam.ScreenPointToRay(Input.mousePosition), Mathf.Infinity))
            {
                tooltipBackground.enabled = true;
                tooltip.enabled = true;
                tooltip.text = CleanName(interactableCheck.collider.gameObject.transform.parent.name);
            }
            else
            {
                tooltipBackground.enabled = false;
                tooltip.enabled = false;
            }

            rightClickMenuBackground.enabled = false;
            rightClickMenuBackground.transform.GetChild(0).gameObject.SetActive(false);
            rightClickMenuBackground.transform.GetChild(1).gameObject.SetActive(false);
            rightClickMenuBackground.transform.GetChild(2).gameObject.SetActive(false);

            tooltipBackground.transform.position = Input.mousePosition + new Vector3(60, -35);
        }
        else
        {
        }
    }

}
