// Copyright 2013-2023, AFI.Inc. All rights reserved.

using TMPro;
using UnityEngine;

// LoadingUI는 gameObject.SetActive(true)시, loading., loading.., loading... 이런식으로 점이 찍히는 UI입니다.
public class LoadingUI : MonoBehaviour {

    [SerializeField] private TMP_Text loadingText;

    private const string originalText = "loading";
    private float currentTime = 0;
    private int addPoint = 0;
    public static LoadingUI Instance { get; private set; }
    void Awake() {
        if (Instance == null) {
            Instance = this;
        }
        
        gameObject.SetActive(false);
    }

    public void OpenUI() {
        currentTime = 0;
        addPoint = 0;
        loadingText.text = originalText;
        
        gameObject.SetActive(true);    
    }

    public void CloseUI() {
        gameObject.SetActive(false);
    }

    void Update() {
        currentTime += Time.deltaTime;
        
        // 현재 시간이 1초일 경우
        if (currentTime > 1) {
            currentTime = 0;
            loadingText.text += ".";
            
            // 찍힌 점이 3개일 경우
            if (addPoint > 3) {
                addPoint = 0;
                loadingText.text = originalText;
            } else {
                addPoint++;
            }
        }
    }
}
