package com.foxx.androidcast.entitlements;

import android.content.Context;

import com.foxx.androidcast.AppPreferences;

import java.io.File;

/** Paths and persisted key / anti-tamper timestamps for the local entitlement blob. */
final class EntitlementStore {
    private static final String FILE_NAME = "account_flags.obf";

    private EntitlementStore() {}

    static File entitlementFile(Context context) {
        File dir = new File(context.getFilesDir(), "entitlements");
        if (!dir.exists() && !dir.mkdirs()) {
            return new File(context.getFilesDir(), FILE_NAME);
        }
        return new File(dir, FILE_NAME);
    }

    static long getAccountKey(Context context) {
        return EntitlementPolicy.parseKeyHex(AppPreferences.getEntitlementAccountKey(context));
    }

    static void setAccountKey(Context context, long key) {
        AppPreferences.setEntitlementAccountKey(context, EntitlementPolicy.formatKeyHex(key));
    }

    static long getLastVerifiedWallMs(Context context) {
        return AppPreferences.getEntitlementLastVerifiedWallMs(context);
    }

    static void setLastVerifiedWallMs(Context context, long wallMs) {
        AppPreferences.setEntitlementLastVerifiedWallMs(context, wallMs);
    }

    static void clearLocal(Context context) {
        File f = entitlementFile(context);
        if (f.exists() && !f.delete()) {
            f.deleteOnExit();
        }
        AppPreferences.setEntitlementAccountKey(context, "");
        AppPreferences.setEntitlementLastVerifiedWallMs(context, 0L);
    }
}
