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

public class PlayerCatcher : MonoBehaviour
{
    public Vector3 rightSideMax;
    public Vector3 rightSideMin;

    public Vector3 leftSideMax;
    public Vector3 leftSideMin;

    public float speed; 

    private GameObject rightSide;
    private GameObject leftSide;

    private void Start()
    {
        rightSide = GetChildWithName("RightSide");
        leftSide = GetChildWithName("LeftSide");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Player") 
        {
            Debug.Log("test");
            CloseWalls();
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            Debug.Log("test");
            OpenWalls();
        }
    }

    private void OpenWalls()
    {
        rightSide.transform.localPosition = Vector3.MoveTowards(rightSide.transform.localPosition, rightSideMax, speed * Time.deltaTime);
        leftSide.transform.localPosition = Vector3.MoveTowards(leftSide.transform.localPosition, leftSideMin, speed * Time.deltaTime);
    }

    private void CloseWalls() 
    {
        rightSide.transform.localPosition = Vector3.MoveTowards(rightSide.transform.localPosition, rightSideMin, speed * Time.deltaTime);
        leftSide.transform.localPosition = Vector3.MoveTowards(leftSide.transform.localPosition, leftSideMax, speed * Time.deltaTime);
    }

    private GameObject GetChildWithName(string name) 
    {
        Transform child = transform.Find(name);
        return child.gameObject;
    }
}
