package epson.common;

import android.app.Application;
import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import epson.print.R;

public class InformationGuide extends Application {
    public static int GUIDE_VER;
    private static Context mContext;
    private String mJsonpath = "Guide/list.json";
    private ArrayList<String> mhtml = new ArrayList<>();

    public InformationGuide(Context context) {
        mContext = context;
        readGuideHtml();
    }

    public void readGuideHtml() {
        mhtml = new ArrayList<>();
        try {
            JSONObject jSONObject = new JSONObject(loadJSONFromAsset());
            GUIDE_VER = jSONObject.getJSONObject("Guide").getInt("Version");
            JSONArray jSONArray = jSONObject.getJSONObject("Guide").getJSONObject("Lang").getJSONArray(getGuideLaunguage());
            for (int i = 0; i < jSONArray.length(); i++) {
                ArrayList<String> arrayList = mhtml;
                arrayList.add("file:///android_asset/Guide/" + jSONArray.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String loadJSONFromAsset() {
        try {
            InputStream open = mContext.getAssets().open(mJsonpath);
            byte[] bArr = new byte[open.available()];
            open.read(bArr);
            open.close();
            return new String(bArr);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getGuideLaunguage() {
        return mContext.getString(R.string.guide_language);
    }

    public ArrayList<String> getHtmlPath() {
        return mhtml;
    }
}
