package com.foxx.androidcast.settings;

/*********************************************************************
 * AppSettingsJson.java
 * Created at: Wed 20 May 2026 14:31:55 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class AppSettingsJson
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 39 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	571859bdee05d4143b75e04c77a692cbdef6100f713817daf719d0961324a3f2
**********************************************************************/
import android.content.Context;

import org.json.JSONObject;

import com.foxx.androidcast.util.IoUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;

/** Reads app-private {@code files/settings.json}. */
public final class AppSettingsJson {
    public static final String SETTINGS_FILE = "settings.json";

    private AppSettingsJson() {}

    public static JSONObject readRoot(Context context) {
        File f = new File(context.getApplicationContext().getFilesDir(), SETTINGS_FILE);
        if (!f.exists()) {
            return new JSONObject();
        }
        try (FileInputStream fis = new FileInputStream(f)) {
            return new JSONObject(new String(IoUtils.readAllBytes(fis), StandardCharsets.UTF_8));
        } catch (Throwable ignored) {
            return new JSONObject();
        }
    }

    public static JSONObject section(Context context, String name) {
        return readRoot(context).optJSONObject(name);
    }
}
