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

public class CollectiblesTracker : MonoBehaviour
{
    public int foundEasy;
    public int totalEasy;
    public int foundMedium;
    public int totalMedium;
    public int foundHard;
    public int totalHard;
    public ISoundEffect soundCoin;

    private float gameStartTimeStamp;
    private bool gameEnd;
    private float gameEndTimeStamp;
    private bool gameCompletion;
    private float gameCompletionTimeStamp;

    public void OnCollectibleAwakes(string type) {
        if(type == "Easy") {
            totalEasy++;
        }
        if(type == "Medium") {
            totalMedium++;
        }
        if(type == "Hard") {
            totalHard++;
        }
    }
    public void OnCollectibleCollected(string type) {
        if(type == "Easy") {
            foundEasy++;
        }
        if(type == "Medium") {
            foundMedium++;
        }
        if(type == "Hard") {
            foundHard++;
        }
        soundCoin.Trigger();

        // Check for completion
        CheckForCompletion();
    }

    public void StartGameTimer() {
        gameStartTimeStamp = Time.timeSinceLevelLoad;
    }

    public void StopEndGameTimer() {
        if(!gameEnd) {
            gameEnd = true;
            gameEndTimeStamp = Time.timeSinceLevelLoad;

            // Check for completion
            CheckForCompletion();
        }
    }

    private void CheckForCompletion() {
        if(gameCompletion) {
            return;
        }
        if(gameEnd && foundEasy == totalEasy && foundMedium == totalMedium && foundHard == totalHard) {
            gameCompletion = true;
            gameCompletionTimeStamp = Time.timeSinceLevelLoad;
        }
    }

    private string FormatTime(float secs) {
        TimeSpan t = TimeSpan.FromSeconds( secs );

        return string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", 
                t.Hours, 
                t.Minutes, 
                t.Seconds, 
                t.Milliseconds);
    }

    public string GetCompleteTime() {
        if(gameEnd) {
            float gameTime = gameEndTimeStamp - gameStartTimeStamp;
            return FormatTime(gameTime);
        } else {
            return "N.A.";
        }
    }

    public string GetFullCompleteTime() {
        if(gameCompletion) {
            float gameTime = gameCompletionTimeStamp - gameStartTimeStamp;
            return FormatTime(gameTime);
        } else {
            return "N.A.";
        }
    }
}
