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

public static class DataManager{
    public const string API_Endpoint="https://pogeracing.io/api/";
    public static string Username {
        get {if(PlayerPrefs.HasKey("username")){
        return PlayerPrefs.GetString("username");
    }else{
        return "";
    }
    }}
    private static string username;
    public static bool LoggedIn {get{return PlayerPrefs.HasKey("username");}}

    public static int Money {
        get{
            if(PlayerPrefs.HasKey("money")){
                return PlayerPrefs.GetInt("money");
            }else{
                return 0;
            }
        }

        set{
            PlayerPrefs.SetInt("money",value);
        }
    }

    public static int SpeedLevel {
        get{
            if(PlayerPrefs.HasKey("speed")){
                return PlayerPrefs.GetInt("speed");
            }else{
                return 0;
            }
        }

        set{
            PlayerPrefs.SetInt("speed",value);
        }
    }

    public static int FuelLevel {
        get{
            if(PlayerPrefs.HasKey("fuel")){
                return PlayerPrefs.GetInt("fuel");
                 PlayerPrefs.Save();
            }else{
                return 0;
            }
        }

        set{
            PlayerPrefs.SetInt("fuel",value);
            PlayerPrefs.Save();
        }
    }

    public static int inAirLevel {
        get{
            if(PlayerPrefs.HasKey("inAir")){
                return PlayerPrefs.GetInt("inAir");
            }else{
                return 0;
            }
        }

        set{
            PlayerPrefs.SetInt("inAir",value);
            PlayerPrefs.Save();
        }
    }

    public static bool OnLoginDone(string _username,string response){
        int uid = -1;
        try{
            uid = int.Parse(response);
        }catch{
            Debug.LogError("Error in registering : " + response);
        }


        if(uid >=0){
            username = _username +"#"+ uid;
            PlayerPrefs.SetString("username", username);
            PlayerPrefs.Save();
            return true;
        }


        return false;
    }   


}