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

public class AdsPlaceholder : MonoBehaviour {
	public Text Timer;
	public Button SkipButton;
	int adSkipTime = 4;
	// Use this for initialization
	void OnEnable () {
		if (Timer && SkipButton)
		{
			ResetAdPlaceholder(string.Empty, false);
			StartCoroutine(StartCountdown(adSkipTime + 1));
		}
	}
		
	void ResetAdPlaceholder(string timer, bool enableSkipButton)
	{
		SkipButton.enabled = enableSkipButton;
		SkipButton.gameObject.GetComponent<Image>().enabled = enableSkipButton;
		Timer.text = timer;
	}
	IEnumerator StartCountdown(int timer)
	{
		while (timer > 1)
		{
			timer --;
			Timer.text = "Skip ad in " + timer + " seconds";
			yield return WaitForUnscaledSeconds(1f); //note: there is no "new" keyword.
		}

		ResetAdPlaceholder(string.Empty, true);
		// Time.timeScale = 1f;
	}

	IEnumerator WaitForUnscaledSeconds(float dur)
	{
		var cur = 0f;
		while (cur < dur)
		{		
			yield return null;
			cur += Time.unscaledDeltaTime;
		}
	}
}
