package epson.print;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;

import com.box.androidsdk.content.BoxConstants;
import com.fasterxml.jackson.core.JsonFactory;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

import epson.print.Util.EPLog;

public class MenudataAdapter {
    ArrayList<MenuDataApp> apps;
    ArrayList<MenuDataCapability> capabilities;
    MenuDataCapability defaultCapability = null;
    ArrayList<MenuDataFunction> functions;
    JSONObject json;
    private Context mContext;

    public void setContext(Context context) {
        mContext = context;
    }

    public void parseJSONFile() {
        EPLog.i("parseJSONFile", "Start");
        json = getJSONFile();
        apps = parseApps(this.json);
        functions = parseFunctions(this.json);
        defaultCapability = parseDefaultCapabilities(this.json);
        capabilities = parseCapabilities(this.json);
        for (int i = 0; i < apps.size(); i++) {
            for (int i2 = 0; i2 < functions.size(); i2++) {
                if (this.apps.get(i).appID.equalsIgnoreCase(this.functions.get(i2).appID)) {
                    apps.get(i).appfunctions.add(this.functions.get(i2));
                }
            }
        }
        EPLog.i("parseJSONFile", CommonDefine.f335OK);
    }

    public JSONObject getJSONFile() {
        InputStream inputStream;
        EPLog.i("getJSONFile", CommonDefine.f335OK);
        AssetManager assets = mContext.getResources().getAssets();
        String str = "";
        try {
            if (IprintApplication.isReleaseUnlimited()) {
                inputStream = assets.open("menudata/MenuDataUnlimited.json");
            } else {
                inputStream = assets.open("menudata/MenuData.json");
            }
            EPLog.i("jsonRead", "jsonObject" + inputStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            while (true) {
                try {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                    sb.append(readLine);
                } catch (IOException unused) {
                    EPLog.i("jsonRead", "error occur");
                }
            }
            str = sb.toString();
            inputStream.close();
            try {
                return new JSONObject(str);
            } catch (JSONException unused2) {
                EPLog.i("jsonRead", "error occur");
                return null;
            }
        } catch (IOException unused3) {
            EPLog.i("jsonRead", "cannot read");
            return null;
        }
    }

    public ArrayList<MenuDataApp> parseApps(JSONObject jSONObject) {
        EPLog.i("parseApps", "Start");
        ArrayList<MenuDataApp> arrayList = new ArrayList<>();
        try {
            JSONArray jSONArray = jSONObject.getJSONArray("Apps");
            for (int i = 0; i < jSONArray.length(); i++) {
                MenuDataApp menuDataApp = new MenuDataApp();
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                menuDataApp.appID = jSONObject2.getString("AppID");
                menuDataApp.name = jSONObject2.getString("Name");
                menuDataApp.appDescription = jSONObject2.getString("Description");
                menuDataApp.googlePlayURL = jSONObject2.getString("GooglePlayURL");
                menuDataApp.PackageName = jSONObject2.getString("AndroidPackageName");
                menuDataApp.ClassName = jSONObject2.getString("AndroidClassName");
                menuDataApp.icon = jSONObject2.getString("AndroidIconName");
                menuDataApp.isRemoteAvailable = jSONObject2.getBoolean("IsRemoteAvailable");
                menuDataApp.appfunctions = new ArrayList<>();
                arrayList.add(menuDataApp);
            }
        } catch (JSONException unused) {
            EPLog.i(JsonFactory.FORMAT_NAME_JSON, "cannnot parseApps");
        }
        EPLog.i("parseApps", CommonDefine.f335OK);
        return arrayList;
    }

    public ArrayList<MenuDataFunction> parseFunctions(JSONObject jSONObject) {
        EPLog.i("parseFunctions", "Start");
        ArrayList<MenuDataFunction> arrayList = new ArrayList<>();
        try {
            JSONArray jSONArray = jSONObject.getJSONArray("Functions");
            for (int i = 0; i < jSONArray.length(); i++) {
                MenuDataFunction menuDataFunction = new MenuDataFunction();
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                menuDataFunction.appID = jSONObject2.getString("AppID");
                menuDataFunction.functionID = jSONObject2.getString("FunctionID");
                menuDataFunction.name = jSONObject2.getString("Name");
                menuDataFunction.PackageName = jSONObject2.getString("AndroidPackageName");
                menuDataFunction.ClassName = jSONObject2.getString("AndroidClassName");
                menuDataFunction.icon = jSONObject2.getString("AndroidIconName");
                menuDataFunction.isRemoteAvailable = Boolean.valueOf(jSONObject2.getBoolean("IsRemoteAvailable"));
                menuDataFunction.key = jSONObject2.getString("AndroidKey");
                arrayList.add(menuDataFunction);
            }
        } catch (JSONException unused) {
            EPLog.i(JsonFactory.FORMAT_NAME_JSON, "cannnot parseFunctions");
        }
        EPLog.i("parseFunctions", CommonDefine.f335OK);
        return arrayList;
    }

    public ArrayList<MenuDataCapability> parseCapabilities(JSONObject jSONObject) {
        EPLog.i("parseCapabilities", "Start");
        ArrayList<MenuDataCapability> arrayList = new ArrayList<>();
        try {
            if (this.defaultCapability == null) {
                EPLog.i("parseCapabilities", "not set defaultcapability");
                return null;
            }
            JSONArray jSONArray = jSONObject.getJSONArray("Capabilities");
            for (int i = 0; i < jSONArray.length(); i++) {
                MenuDataCapability menuDataCapability = new MenuDataCapability();
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                menuDataCapability.deviceIDs = JSONArrayToString(jSONObject2.getJSONArray("DeviceIDs"));
                menuDataCapability.isPrinter = Boolean.valueOf(jSONObject2.getBoolean("IsPrinter"));
                menuDataCapability.isScanner = Boolean.valueOf(jSONObject2.getBoolean("IsScanner"));
                menuDataCapability.appIDs.addAll(this.defaultCapability.appIDs);
                menuDataCapability.functionIDs.addAll(this.defaultCapability.functionIDs);
                menuDataCapability.appIDs.addAll(FilterApps(JSONArrayToString(jSONObject2.getJSONArray("AppIDs"))));
                menuDataCapability.functionIDs.addAll(FilterFunctions(JSONArrayToString(jSONObject2.getJSONArray("FunctionIDs"))));
                menuDataCapability.functionIDs = setExcludedlist(menuDataCapability.functionIDs, getExcludedlist(jSONObject2));
                arrayList.add(menuDataCapability);
            }
            EPLog.i("parseCapabilities", CommonDefine.f335OK);
            return arrayList;
        } catch (JSONException unused) {
            EPLog.i(JsonFactory.FORMAT_NAME_JSON, "cannnot parseCapabilities");
            return null;
        }
    }

    public MenuDataCapability parseDefaultCapabilities(JSONObject jSONObject) {
        EPLog.i("parseDefaultCapabilities", "Start");
        MenuDataCapability menuDataCapability = new MenuDataCapability();
        try {
            JSONObject jSONObject2 = jSONObject.getJSONObject("DefaultCapability");
            menuDataCapability.deviceIDs = JSONArrayToString(jSONObject2.getJSONArray("DeviceIDs"));
            menuDataCapability.isPrinter = Boolean.valueOf(jSONObject2.getBoolean("IsPrinter"));
            menuDataCapability.isScanner = Boolean.valueOf(jSONObject2.getBoolean("IsScanner"));
            menuDataCapability.appIDs = FilterApps(JSONArrayToString(jSONObject2.getJSONArray("AppIDs")));
            menuDataCapability.functionIDs = FilterFunctions(JSONArrayToString(jSONObject2.getJSONArray("FunctionIDs")));
            EPLog.i("parseDefaultCapabilities", CommonDefine.f335OK);
            return menuDataCapability;
        } catch (JSONException unused) {
            EPLog.i(JsonFactory.FORMAT_NAME_JSON, "cannnot parseDefaultCapabilities");
            return null;
        }
    }

    private ArrayList<String> getExcludedlist(JSONObject jSONObject) {
        try {
            return FilterFunctions(JSONArrayToString(jSONObject.getJSONArray("FunctionIDsExcluded")));
        } catch (JSONException unused) {
            return null;
        }
    }

    private ArrayList<String> setExcludedlist(ArrayList<String> arrayList, ArrayList<String> arrayList2) {
        ArrayList<String> arrayList3 = new ArrayList<>();
        Iterator<String> it = arrayList.iterator();
        while (it.hasNext()) {
            String next = it.next();
            if (arrayList2 == null || !arrayList2.contains(next)) {
                arrayList3.add(next);
            }
        }
        return arrayList3;
    }

    private ArrayList<String> FilterApps(ArrayList<String> arrayList) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        Iterator<MenuDataApp> it = apps.iterator();
        while (it.hasNext()) {
            MenuDataApp next = it.next();
            if (arrayList.contains(next.appID)) {
                arrayList2.add(next.appID);
            }
        }
        return arrayList2;
    }

    private ArrayList<String> FilterFunctions(ArrayList<String> arrayList) {
        ArrayList<String> arrayList2 = new ArrayList<>();
        Iterator<MenuDataFunction> it = functions.iterator();
        while (it.hasNext()) {
            MenuDataFunction next = it.next();
            if (arrayList.contains(next.functionID)) {
                arrayList2.add(next.functionID);
            }
        }
        return arrayList2;
    }

    public ArrayList<String> JSONArrayToString(JSONArray jSONArray) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < jSONArray.length(); i++) {
            try {
                arrayList.add((String) jSONArray.get(i));
            } catch (JSONException unused) {
                EPLog.i(JsonFactory.FORMAT_NAME_JSON, "cannot JSONArrayToString");
            }
        }
        return arrayList;
    }

    public ArrayList<IconTextArrayItem> MenuListfordeviceID(String str, int i, Context context) {
        int i2 = i;
        ArrayList<IconTextArrayItem> arrayList = new ArrayList<>();
        MenuDataCapability capabilityForDeviceID = capabilityForDeviceID(str);
        Resources resources = context.getResources();
        ArrayList<MenuDataApp> appsForAppIDs = appsForAppIDs(capabilityForDeviceID.appIDs);
        for (int i3 = 0; i3 < capabilityForDeviceID.appIDs.size(); i3++) {
            if (appsForAppIDs.get(i3).isInstalled(mContext).booleanValue()) {
                MenuDataCapability capabilityForDeviceID2 = capabilityForDeviceID(str);
                if ("iPrint".equals(appsForAppIDs.get(i3).appID)) {
                    Iterator<MenuDataFunction> it = appsForAppIDs.get(i3).appfunctions.iterator();
                    while (it.hasNext()) {
                        MenuDataFunction next = it.next();
                        if (capabilityForDeviceID2.functionIDs.contains(next.functionID)) {
                            IconTextArrayItem iconTextArrayItem = new IconTextArrayItem();
                            iconTextArrayItem.appId = functionForFunctionID(next.functionID).appID;
                            iconTextArrayItem.menuId = resources.getIdentifier(functionForFunctionID(next.functionID).name, "string", context.getPackageName());
                            iconTextArrayItem.ClassName = functionForFunctionID(next.functionID).ClassName;
                            iconTextArrayItem.PackageName = functionForFunctionID(next.functionID).PackageName;
                            iconTextArrayItem.isRemoteAvailable = functionForFunctionID(next.functionID).isRemoteAvailable.booleanValue();
                            iconTextArrayItem.isInstall = true;
                            iconTextArrayItem.key = functionForFunctionID(next.functionID).key;
                            String str2 = functionForFunctionID(next.functionID).icon;
                            iconTextArrayItem.showMenu = true;
                            if (i2 == 2 && iconTextArrayItem.appId.equalsIgnoreCase("iPrint") && !iconTextArrayItem.isRemoteAvailable) {
                                str2 = functionForFunctionID(next.functionID).icon.concat("_disabled");
                                iconTextArrayItem.showMenu = false;
                            }
                            iconTextArrayItem.imageID = resources.getIdentifier(str2, "drawable", context.getPackageName());
                            arrayList.add(iconTextArrayItem);
                        }
                    }
                } else {
                    IconTextArrayItem iconTextArrayItem2 = new IconTextArrayItem();
                    iconTextArrayItem2.appId = appsForAppIDs.get(i3).appID;
                    String str3 = appsForAppIDs.get(i3).icon;
                    iconTextArrayItem2.menuId = resources.getIdentifier(appsForAppIDs.get(i3).name, "string", context.getPackageName());
                    iconTextArrayItem2.ClassName = appsForAppIDs.get(i3).ClassName;
                    iconTextArrayItem2.PackageName = appsForAppIDs.get(i3).PackageName;
                    iconTextArrayItem2.isRemoteAvailable = appsForAppIDs.get(i3).isRemoteAvailable;
                    iconTextArrayItem2.isInstall = true;
                    String replace = str3.replace("_notinstalled", "");
                    iconTextArrayItem2.key = BoxConstants.ROOT_FOLDER_ID;
                    if ((i2 == 3 || i2 == 2) && (iconTextArrayItem2.appId.equalsIgnoreCase("Creative") || iconTextArrayItem2.appId.equalsIgnoreCase("3DFramePrint") || iconTextArrayItem2.appId.equalsIgnoreCase("CameraCopy") || iconTextArrayItem2.appId.equalsIgnoreCase("MultiRollPrint") || iconTextArrayItem2.appId.equalsIgnoreCase("CardPrint") || iconTextArrayItem2.appId.equalsIgnoreCase("Nenga"))) {
                        replace = replace.concat("_disabled");
                        iconTextArrayItem2.showMenu = false;
                    }
                    iconTextArrayItem2.imageID = resources.getIdentifier(replace, "drawable", context.getPackageName());
                    arrayList.add(iconTextArrayItem2);
                }
            } else {
                IconTextArrayItem iconTextArrayItem3 = new IconTextArrayItem();
                iconTextArrayItem3.appId = appsForAppIDs.get(i3).appID;
                iconTextArrayItem3.imageID = resources.getIdentifier(appsForAppIDs.get(i3).icon, "drawable", context.getPackageName());
                iconTextArrayItem3.menuId = resources.getIdentifier(appsForAppIDs.get(i3).name, "string", context.getPackageName());
                iconTextArrayItem3.ClassName = appsForAppIDs.get(i3).ClassName;
                iconTextArrayItem3.PackageName = appsForAppIDs.get(i3).PackageName;
                iconTextArrayItem3.isRemoteAvailable = appsForAppIDs.get(i3).isRemoteAvailable;
                iconTextArrayItem3.AppDiscId = resources.getIdentifier(appsForAppIDs.get(i3).appDescription, "string", context.getPackageName());
                iconTextArrayItem3.isInstall = false;
                iconTextArrayItem3.googleStoreUrl = appsForAppIDs.get(i3).googlePlayURL;
                arrayList.add(iconTextArrayItem3);
            }
        }
        return arrayList;
    }

    public MenuDataCapability capabilityForDeviceID(String str) {
        MenuDataCapability menuDataCapability;
        EPLog.i("capabilityForSerialHeader", CommonDefine.f335OK);
        int i = 0;
        while (true) {
            if (i >= capabilities.size()) {
                menuDataCapability = null;
                break;
            } else if (this.capabilities.get(i).deviceIDs.contains(str)) {
                menuDataCapability = capabilities.get(i);
                break;
            } else {
                i++;
            }
        }
        return menuDataCapability == null ? defaultCapability : menuDataCapability;
    }

    public ArrayList<MenuDataApp> appsForAppIDs(ArrayList<String> arrayList) {
        EPLog.i("appsForAppIDs", CommonDefine.f335OK);
        ArrayList<MenuDataApp> arrayList2 = new ArrayList<>();
        for (int i = 0; i < apps.size(); i++) {
            if (arrayList.contains(this.apps.get(i).appID)) {
                arrayList2.add(this.apps.get(i));
            }
        }
        return arrayList2;
    }

    public ArrayList<MenuDataFunction> functionsForFunctionIDs(ArrayList<String> arrayList) {
        EPLog.i("functionsForFunctionIDs", CommonDefine.f335OK);
        ArrayList<MenuDataFunction> arrayList2 = new ArrayList<>();
        for (int i = 0; i < functions.size(); i++) {
            if (arrayList.contains(this.functions.get(i).functionID)) {
                arrayList2.add(this.functions.get(i));
            }
        }
        return arrayList2;
    }

    public MenuDataFunction functionForFunctionID(String str) {
        for (int i = 0; i < functions.size(); i++) {
            if (this.functions.get(i).functionID.equalsIgnoreCase(str)) {
                return functions.get(i);
            }
        }
        return null;
    }

    class MenuDataApp {
        private static final int VERSIONCODE_3D = 30100;
        private static final int VERSIONCODE_CREATIVE = 60000;
        private static final int VERSIONCODE_MEISHI = 1001000;
        private static final int VERSIONCODE_MROOL = 2020000;
        public static final int VERSIONCODE_NENGA = 201900000;
        String ClassName;
        String PackageName;
        String appDescription;
        String appID;
        ArrayList<MenuDataFunction> appfunctions;
        String googlePlayURL;
        String icon;
        Boolean isInstalled;
        boolean isRemoteAvailable;
        String name;

        MenuDataApp() {
        }

        public Boolean isInstalled(Context context) {
            EPLog.i("isInstalled", CommonDefine.f335OK);
            try {
                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(this.PackageName, 128);
                if ("Creative".equals(this.appID) && 60000 > packageInfo.versionCode) {
                    return false;
                }
                if ("3DFramePrint".equals(this.appID) && VERSIONCODE_3D > packageInfo.versionCode) {
                    return false;
                }
                if ("CardPrint".equals(this.appID) && VERSIONCODE_MEISHI > packageInfo.versionCode) {
                    return false;
                }
                if (!"MultiRollPrint".equals(this.appID) || VERSIONCODE_MROOL <= packageInfo.versionCode) {
                    return true;
                }
                return false;
            } catch (PackageManager.NameNotFoundException unused) {
                return false;
            }
        }
    }

    class MenuDataFunction {
        String ClassName;
        String PackageName;
        String appID;
        String functionID;
        String icon;
        Boolean isInstalled;
        Boolean isRemoteAvailable;
        String key;
        String name;

        MenuDataFunction() {
        }
    }

    class MenuDataCapability {
        ArrayList<String> appIDs = new ArrayList<>();
        ArrayList<String> deviceIDs = new ArrayList<>();
        ArrayList<String> functionIDs = new ArrayList<>();
        Boolean isPrinter = false;
        Boolean isScanner = false;

        MenuDataCapability() {
        }
    }
}
