package eu.project.rapid.storage;

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

/**
 * Created by Talha on 4/5/2018.
 */

public class Preferences {

    public static void saveDeviceId(Long id, Context cxt) {

        SharedPreferences sharedPref = cxt.getSharedPreferences("device", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putLong("deviceId", id);
        editor.commit();
    }

    public static Long getDeviceId(Context cxt) {

        SharedPreferences sharedPref = cxt.getSharedPreferences("device", Context.MODE_PRIVATE);
        return sharedPref.getLong("deviceId", -1);

    }

    public static void saveCurrencyUnits(Long currency, Context cxt) {

        SharedPreferences sharedPref = cxt.getSharedPreferences("device", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putLong("currency", currency);
        editor.commit();
    }

    public static Long getCurrencyUnits(Context cxt) {

        SharedPreferences sharedPref = cxt.getSharedPreferences("device", Context.MODE_PRIVATE);
        return sharedPref.getLong("currency", -1);

    }

    public static Long deductBalance(Context cxt, Long cost) {
        Long balance = getCurrencyUnits(cxt);
        saveCurrencyUnits(balance - cost, cxt);
        return balance - cost;
    }

}
