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

using System;
using BackEnd;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class LoadingUI : MonoBehaviour {

    public static LoadingUI Instance { get; private set; } = null;
    
    private TMP_Text        _loadingText;
    private const float     _addTime = 0.2f;
    private float           _time = 0;
    private int             _addCount = 0;
    private const string    _loadingPhrase = "로딩중";
    
    public void Initialize() {
        if (Instance == null) {
            Instance = this;
        }
        
        _loadingText = GetComponentInChildren<TMP_Text>();
        _loadingText.text =_loadingPhrase;
    }
    
    // 로딩 UI 활성화 시, 0.2초마다 .을 추가하는 로직
    void Update() {

        _time += Time.deltaTime;

        if (_time >_addTime) {

            _loadingText.text += ".";
            _time = 0;
            _addCount++;

            if (_addCount > 3) {
                _loadingText.text = _loadingPhrase;
                _addCount = 0;
            }
        }
    }

    public void OpenUI() {
        gameObject.SetActive(true);
    }

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