using UnityEngine;
using UnityEngine.SceneManagement;

namespace GGJam21.Core
{
    public class PlayerMovement : MonoBehaviour
    {
        public KeyCode key1;
        public KeyCode key2;
        public Vector3 movement;

        public float timeTransition = 3f;
        public ParticleSystem particleConfetti;
        public bool isObstacle;

        Rigidbody rb;

        void Start()
        {
            rb = GetComponent<Rigidbody>();
            particleConfetti.Stop();
        }

        void FixedUpdate()
        {
            if (Input.GetKey(key1)) // W
                rb.velocity += movement;
            if (Input.GetKey(key2)) // S
                rb.velocity -= movement;
            if(Input.GetKey(KeyCode.R))
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        void OnTriggerEnter(Collider other)
        {
            if (!isObstacle)
            {
                if (CompareTag("Player") && other.CompareTag("Finish"))
                {
                    particleConfetti.Play();
                    Invoke("SetLoadScene", timeTransition);
                }
            }
        }

        void SetLoadScene()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }
}