using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckUpdates : MonoBehaviour
{
    public static int ClientVersion = 5;
    public static int ServerVersion;

    public float interval = 30;
    
    float t;

    void Awake(){
        DontDestroyOnLoad(gameObject);
        CheckUpdate();
    }

    void Update()
    {
        t+=Time.deltaTime;

        if(t > interval){ t=0; CheckUpdate();}
    }

    public void CheckUpdate(){
        StartCoroutine(checkUpdate());
    }


    IEnumerator checkUpdate(){
        WWW req= new WWW(DataManager.API_ENDPOINT + "get_version.php");
        yield return req;

        Debug.Log("Server version: " + req.text);

        ServerVersion = int.Parse(req.text);
        if(ServerVersion <= 0){
            LoadingScreen.LoadLevel("maintaince");
        }else if(ServerVersion > ClientVersion){
            LoadingScreen.LoadLevel("update");
        }
    }
}
