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



//scorboard with countdown and score keeper
// SCORE - use trigger colliders in your prefab assetbundle to call IncrimentScore()
// COUNTDOWN - in the inspector set the time in seconds (120 = 2 minutes etc.)
// Add TextMeshPro Component to 2 GameObjects- one for score - one for countdown
// To enable reseting the scorboard add an event trigger to call StartTimer() 
//   and/or choose Auto Start to begin countdown when the experience begins.
// 





/*************************************************************************
* 
* ARConnex
* __________________
* 
*  [2017] - [2020] ARConnex Incorporated 
*  All Rights Reserved.
* 
* NOTICE:  All information contained herein is, and remains
* the property of ARConnex Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to ARConnex Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from ARConnex Incorporated or if used for display in the ARConnex Reality Browser.
*/




public class TimerScore : MonoBehaviour
{
    public float timeRemaining;
    bool timerIsRunning;   
    public TextMeshPro timeText;
    public TextMeshPro scoreText;
    float score;
    public AudioSource endSound;
    float originaltime;
    public bool autoStart;

    private void Start()
    {
      originaltime = timeRemaining;
      DisplayTime(originaltime);
        
    }

    void Update()
    {
        if (autoStart) { StartTimer(); autoStart = false; }

        if (timerIsRunning)
        {
            if (timeRemaining > 0)
            {
                timeRemaining -= Time.deltaTime;
                DisplayTime(timeRemaining);
            }
            else
            {                
                timeRemaining = 0;
                timerIsRunning = false;
                timeRemaining = originaltime;
                DisplayTime(timeRemaining);
                endSound.Play();
            }
        }
    }

    void DisplayTime(float timeToDisplay)
    {
        float minutes = Mathf.FloorToInt(timeToDisplay / 60);
        float seconds = Mathf.FloorToInt(timeToDisplay % 60);
        timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }

    public void StartTimer()
    {
        score = 0f;
        timeRemaining = originaltime;
        timerIsRunning = true;
        DisplayTime(timeRemaining);
        scoreText.text = score.ToString();
    }

    public void IncrimentScore()
    {
        if (timerIsRunning)
        {
            score = score + 1.0f;
            scoreText.text = score.ToString();
        }            
    }

}