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

public class ScoreCounter : MonoBehaviour {

	[SerializeField] private float m_MinTimeLow = 2f;
	[SerializeField] private float m_MinTimeMid = 10f;
	[SerializeField] private float m_MinTimeHigh = 30f;

	[SerializeField] private int m_KillScoreLow = 5;
	[SerializeField] private int m_KillScoreMid = 10;
	[SerializeField] private int m_KillScoreHigh = 20;

	[SerializeField] private int m_DeathPenalty = 5;

	public Text m_ScoreText;
	public Text m_TimerText;

	private int m_Score;

	private float m_ActiveTime;

	public int GetScore() {
		return m_Score;
	}

	public void OnKilled() {
		m_Score = Mathf.Max (0, m_Score - m_DeathPenalty);
		m_ActiveTime = 0f;


		if(m_ScoreText!=null)
			m_ScoreText.text = "" + m_Score;

		if(m_TimerText!=null)
			m_TimerText.text = "" + 0;
	}

	public void OnKill(GameObject killed) {
		var otherComp = killed.GetComponent<ScoreCounter> ();
		if (otherComp != null) {
			if (otherComp.m_ActiveTime >= m_MinTimeHigh) {
				m_Score += m_KillScoreHigh;
			} else if (otherComp.m_ActiveTime >= m_MinTimeMid) {
				m_Score += m_KillScoreMid;
			} else if (otherComp.m_ActiveTime >= m_MinTimeLow) {
				m_Score += m_KillScoreLow;
			}
		}
	}

	// Update is called once per frame
	void Update () {
		m_ActiveTime += Time.deltaTime;

		if(m_ScoreText!=null)
			m_ScoreText.text = "" + m_Score;

		if(m_TimerText!=null)
			m_TimerText.text = "" + (int)m_ActiveTime;
	}
}
