package com.SpinMoney.SpinMoney;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Utils {
    public static String userID = "";
    public static String userName = "";
    public static String userEmail = "";
    public static String userDP = "";
    public static String userPhone = "";
    public static String deposit = "";
    public static String withdraw = "";


    public static void saveTag(Context ctx, String tag, Object value) {
        SharedPreferences preferences = ctx.getSharedPreferences(ctx.getPackageName(),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        if(value.getClass()==String.class){
            editor.putString(tag, (String) value);
        } else if(value.getClass()==Integer.class){
            editor.putInt(tag, (Integer) value);
        } else if(value.getClass()==Boolean.class){
            editor.putBoolean(tag, (Boolean) value);
        }
        editor.apply();
    }

    public static String getString(Context ctx,String tag) {
        SharedPreferences preferences = ctx.getSharedPreferences(ctx.getPackageName(),Context.MODE_PRIVATE);
        if (preferences != null && preferences.contains(tag)) {
            return preferences.getString(tag, "");
        } else {
            return "";
        }
    }

    public static int getInt(Context ctx,String tag) {
        SharedPreferences preferences = ctx.getSharedPreferences(ctx.getPackageName(),Context.MODE_PRIVATE);
        if (preferences != null && preferences.contains(tag)) {
            return preferences.getInt(tag, 0);
        }
        return 0;
    }

    public static boolean getBoolean(Context ctx,String tag) {
        SharedPreferences preferences = ctx.getSharedPreferences(ctx.getPackageName(),Context.MODE_PRIVATE);
        if (preferences != null && preferences.contains(tag)) {
            return preferences.getBoolean(tag, false);
        }
        return false;
    }

    public static String getJSONString(String url) {
        String jsonString = null;
        HttpURLConnection linkConnection = null;
        try {
            URL linkurl = new URL(url);
            linkConnection = (HttpURLConnection) linkurl.openConnection();
            int responseCode = linkConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream linkinStream = linkConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int j = 0;
                while ((j = linkinStream.read()) != -1) {
                    baos.write(j);
                }
                byte[] data = baos.toByteArray();
                jsonString = new String(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (linkConnection != null) {
                linkConnection.disconnect();
            }
        }
        return jsonString;
    }
    
}
