package com.iptv.app;

import android.content.Context;
import android.webkit.JavascriptInterface;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AssetReader {
    private Context context;

    public AssetReader(Context context) {
        this.context = context;
    }

    @JavascriptInterface
    public String readFile(String path) {
        try {
            // path 如: "www/api/result.txt"
            InputStream is = context.getAssets().open(path);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            return "ERROR: " + e.getMessage();
        }
    }

    @JavascriptInterface
    public String readChannelData() {
        return readFile("www/api/result.txt");
    }

    @JavascriptInterface
    public boolean fileExists(String path) {
        try {
            context.getAssets().open(path).close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}
