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

public class InteractSphere : MonoBehaviour, IInteractable {
    [SerializeField] private List<Material> materials;
    [SerializeField] private MeshRenderer meshRenderer;
    [SerializeField] private bool isGreen;
    private Action onInteractionComplete;
    private float timer;
    private bool IsActive { get; set; }
    private GridPosition GridPosition { get; set; }

    private bool IsGreen {
        get => isGreen;
        set {
            isGreen = value;
            meshRenderer.material = materials[IsGreen ? 0 : 1];
        }
    }

    private void Start() {
        GridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
        LevelGrid.Instance.SetInteractableAtGridPosition(GridPosition, this);
        IsGreen = false;
    }

    private void Update() {
        if (!IsActive) return;
        timer -= Time.deltaTime;
        if (timer > 0f) return;
        IsActive = false;
        onInteractionComplete();
    }

    public void Interact(Action newOnInteractionComplete) {
        onInteractionComplete = newOnInteractionComplete;
        IsActive = true;
        timer = .5f;
        IsGreen = !IsGreen;
    }
}