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

namespace Yumon.Template.Lite
{
    /// <summary>
    ///     Example of how to implement the <see cref="YumonGameplay" /> abstract class.
    ///     This is a sample gameplay script that updates the score in a linear way from 0 to 100 and back to 0
    /// </summary>
    public class ClickTimingGameplay : YumonGameplay
    {
        [SerializeField] TextMeshProUGUI scoreText;
        [SerializeField] Image fillImage;
        [SerializeField] float timeBetweenScoreUpdates = 0.025f;
        [SerializeField] int scoreIncrement = 1;
        
        bool _isLevelStarted;


        public static int Score { get; private set; }

        public override void OnStartLevel()
        {
            _isLevelStarted = true;
            StartCoroutine(UpdateScore());
        }

        public override void OnEndLevel() => _isLevelStarted = false;

        IEnumerator UpdateScore()
        {
            while (_isLevelStarted)
            {
                Score += scoreIncrement;
                if (Score >= 100)
                    Score = 0;

                scoreText.text = $"Score: {Score}";
                fillImage.fillAmount = Score/100f;
                yield return new WaitForSeconds(timeBetweenScoreUpdates);
            }
        }
    }
}
