using System.Collections;
using UnityEngine;
using VContainer;

public class Player : MonoBehaviour, IPlayer
{
    public float jumpforce = 4f;
    public Rigidbody BallRb;
    public bool isDead {  get; private set; }

    public GameObject splashObject;

    [Inject] ScoreService scoreService;
    [Inject] InputReader inputReader;
    [Inject] DeathScreenController deathScreenController;

    void Start()
    {
       
        if (BallRb == null)
        {
            BallRb = GetComponent<Rigidbody>();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.CompareTag("Platform") && !isDead)
        {
            ContactPoint contact  = collision.contacts[0];
            Vector3 contactOffset = new Vector3(0, 0.1f, 0);
            GameObject currentSplash = Instantiate(splashObject, contact.point+contactOffset,splashObject.transform.rotation);
            BallRb.linearVelocity = new Vector3(0, jumpforce, 0);
            Destroy(currentSplash,0.2f);
        }
        else if (collision.collider.CompareTag("Death"))
        {
            Die();
        }
    }

    void Die()
    {
        Debug.Log("Player is Dead");
        isDead = true;
        StartCoroutine(DieRoutine());
    }

    IEnumerator DieRoutine()
    {
        inputReader.isBlocked = true;
        yield return new WaitForSeconds(1f);
        Time.timeScale = 0;
        deathScreenController.ShowDeathScreen(scoreService.score, scoreService.GetHighscore());
    }


    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("ScoreTrigger"))
        {
            scoreService.AddScore();
        }
    }
}
