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

public class PlaceableSpot : MonoBehaviour
{

    [SerializeField] GameObject[] acceptedObjects;
    [SerializeField] private GameEvent correctEvent;
    [SerializeField] private GameEvent incorrectEvent;
    private Transform snapPosition;

    private GameObject objectInSpot;

    public GameObject ObjectInSpot => objectInSpot;

    public bool HasObjectInSpot
    {
        get { return objectInSpot != null; }
    }

    // Start is called before the first frame update
    void Start()
    {
        snapPosition = transform.Find("Snap Point");
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private bool putObjectInSpot(GameObject obj)
    {
        if (this.HasObjectInSpot)
        {
            return false;
        }

        if (acceptedObjects.Contains(obj))
        {
            // snap to position
            obj.GetComponent<Rigidbody>().isKinematic = true;
            obj.transform.position = snapPosition.position;
            obj.transform.rotation = snapPosition.rotation;
            objectInSpot = obj;
            correctEvent.Raise();
            return true;
        }
        else
        {
            incorrectEvent.Raise();
            return false;
        }
    }

    private void removeObjectFromSpot(GameObject obj)
    {
        obj.GetComponent<Rigidbody>().isKinematic = false;
        objectInSpot = null;
    }

    void OnTriggerEnter(Collider collider)
    {
        
    }
}
