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

public class CoinBehavior : MonoBehaviour
{
    public int m_ScoreValue = 1; //how many points a single coin is worth at the end of the game
    public float m_ChanceOfDisease; //chance of getting diease from 0-100
    private float m_CoinTimer;
    private GameObject m_CoinSpawnerRef;

    public AudioSource CoinSource;

    // Use this for initialization
    void Start ()
	{
        m_CoinSpawnerRef = GameObject.FindWithTag("CoinSpawner");
        m_CoinTimer = GameObject.FindWithTag("CoinSpawner").GetComponent<CoinSpawner>().m_CoinLifespan;
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if(col.gameObject.CompareTag("Player"))
        {
            if (!col.gameObject.GetComponent<PlayerGameplay>().m_IsDead)
            {
                col.gameObject.GetComponent<PlayerGameplay>().SetCurrentScore(m_ScoreValue);
                if (GameObject.FindWithTag("PlayerManager").GetComponent<PlayerManager>().CanTransmitDisease())
                {
                    ChanceOfCatchingDisease(col.gameObject);
                }

				CoinSource.Play();
                Object.Destroy(this.gameObject);
                m_CoinSpawnerRef.GetComponent<CoinSpawner>().CurrentCoinsCount--;
               
            }
        }
    }
   
    void ChanceOfCatchingDisease(GameObject playerObject)
    {
        if (Random.Range(0, 100) < m_ChanceOfDisease)
        {
            playerObject.GetComponent<PlayerGameplay>().SetIsDiseased(true);
        }
    }

    void Update()
    {
        if(m_CoinTimer > 0)
        {
            m_CoinTimer -= Time.deltaTime;
        }
        else
        {
            m_CoinSpawnerRef.GetComponent<CoinSpawner>().CurrentCoinsCount--;
            Object.Destroy(this.gameObject);
        }
    }

}
