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

public class UITime : MonoBehaviour
{
    float minX = -478;
    float maxX = 6;


    public Image fillImage; // 填充的Image组件
    public float countdownTime; // 总倒计时秒数


    float timeClip;

    void Start()
    {

    }

    void Update()
    {

    }

    // 设置新的倒计时时间
    public void SetCountdownTime(int newTime)
    {
        StopAllCoroutines();
        countdownTime = newTime;
        timeClip = (maxX - minX) / (float)newTime;
    }

    void RefreshUI(int second)
    {
        this.fillImage.rectTransform.localPosition = new Vector2(maxX - timeClip * second, fillImage.rectTransform.localPosition.y);
    }

    public IEnumerator StartCountTime(int seconds)
    {
        Debug.Log("Timer started.");
        while (seconds > 0)
        {

            yield return new WaitForSeconds(1f); // 等待指定时间
            RefreshUI(60 - seconds);
            seconds--;
        }

    }

    internal void CountTime(int seconds)
    {
        SetCountdownTime(seconds);
        StartCoroutine(StartCountTime(seconds));
    }
}