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

public class GridHighlighter : MonoBehaviour
{
    public static GridHighlighter instance;
    public GameObject[] gridPieces;
    public static string grabbedObjectName;
    public static bool DISABLED;

    private float distanceToHighlight = 0.7f;

    private void Awake()
    {
        Transform player = GameObject.FindGameObjectWithTag("Player").transform;
        instance = this;
    }

    private void Update()
    {
        if (GrabManager.heldItem)
            DetermineDistance();
        else
            DisableHighlights();
    }

    public void DisableHighlights()
    {
        for (int i = 0; i < gridPieces.Length; i++)
        {
            if (gridPieces[i].activeSelf)
                gridPieces[i].SetActive(false);
        }
    }

    private GameObject GetGridPiece()
    {
        for (int i = 0; i < gridPieces.Length; i++)
        {
            if (gridPieces[i].name == grabbedObjectName)
                return gridPieces[i];
        }

        Debug.LogError("NULL!");
        return null;
    }

    private void DetermineDistance()
    {
        if (DISABLED)
            return;

        GameObject relevantPiece = GetGridPiece();

        float distance = GetDistanceToFocus(relevantPiece.transform);

        bool enabled = distance < distanceToHighlight && CanHighlightPiece(relevantPiece.transform);
        relevantPiece.SetActive(enabled);
    }

    private bool PieceActive(int number)
    {
        return VasePiece.placedPieces.Contains(gridPieces[number - 1].name);
    }

    public bool CanHighlightPiece(Transform piece)
    {
        if (DISABLED)
            return false;

        bool highlight = false;
        string name = piece.name;

        switch (name)
        {
            case "Piece 1": // ALWAYS
                highlight = true;
                break;
            case "Piece 2": // ALWAYS
                highlight = true;
                break;
            case "Piece 3":  // IF PIECE 2 ACTIVE
                highlight = PieceActive(2);
                break;
            case "Piece 4": // IF PIECE 1 and 3 ACTIVE
                highlight = PieceActive(1) && PieceActive(3);
                break;
            case "Piece 5": // IF PIECE 1 and 2 ACTIVE
                highlight = PieceActive(1) && PieceActive(2);
                break;
            case "Piece 6": // IF PIECE 3 ACTIVE
                highlight = PieceActive(3);
                break;
            case "Piece 7": // IF PIECE 6 ACTIVE
                highlight = PieceActive(6);
                break;
            case "Piece 8": // IF PIECE 4 ACTIVE
                highlight = PieceActive(4);
                break;
            case "Piece 9": // IF PIECE 5 ACTIVE
                highlight = PieceActive(5);
                break;
            case "Piece 10": // IF PIECE 9 ACTIVE
                highlight = PieceActive(9);
                break;
            case "Piece 11": // IF PIECE 6 & 4 ACTIVE
                highlight = PieceActive(4) && PieceActive(6);
                break;
        }

        GameObject gridPiece = GetGridPiece();
        return highlight && GetDistanceToFocus(gridPiece.transform) < distanceToHighlight;
    }

    private float GetDistanceToFocus(Transform gridPiece)
    {
        if (VasePiece.grabbedTarget == null)
        {
            print("NULL!");
            return 1000f;
        }

        return Vector3.Distance(VasePiece.grabbedTarget.transform.position, gridPiece.position);
    }
}