package epson.print.inkrpln;

import android.net.Uri;



import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;

public class InkRplnInfoClient {
    private static final String INFO_PATH = "sncheck";
    private static final String KEY_APPEND_SERIAL = "appendSerial";
    private static final String KEY_BUTTON_TYPE = "showButton";
    private static final String KEY_LANDING_URL = "urlLandingPage";
    private static final String KEY_SHOW_INVITATION = "showInvitation";
    private static final String VALUE_BUTTON_TYPE_BUY_INK_AND_READY_INK = "BuyInk_ReadyInk";
    private static final String VALUE_BUTTON_TYPE_READY_INK_ONLY = "ReadyInk";

    @Nullable
    public InkRsInfo getInkDsInfo(@NonNull String str, @NonNull String str2) throws IOException {
        return convertString(SimpleHttpClient.getString(getInkReplenishInfoUrl(str2, str)));
    }

    @VisibleForTesting
    @NonNull
    static URL getInkReplenishInfoUrl(String str, String str2) throws MalformedURLException {
        Uri.Builder builder = new Uri.Builder();
        builder.encodedPath(str);
        builder.appendPath(INFO_PATH);
        builder.appendPath(InkReplenishSystem.encodePrinterSerial(str2));
        return new URL(builder.build().toString());
    }

    @Nullable
    public static InkRsInfo convertString(@Nullable String str) {
        if (str == null) {
            return null;
        }
        InkRsInfo inkRsInfo = new InkRsInfo();
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.has(KEY_LANDING_URL)) {
                inkRsInfo.landingUrl = jSONObject.getString(KEY_LANDING_URL);
            }
            if (jSONObject.has(KEY_BUTTON_TYPE)) {
                inkRsInfo.buttonType = 0;
                String string = jSONObject.getString(KEY_BUTTON_TYPE);
                if (VALUE_BUTTON_TYPE_READY_INK_ONLY.equals(string)) {
                    inkRsInfo.buttonType = 1;
                } else if (VALUE_BUTTON_TYPE_BUY_INK_AND_READY_INK.equals(string)) {
                    inkRsInfo.buttonType = 2;
                }
            }
            if (jSONObject.has(KEY_SHOW_INVITATION)) {
                inkRsInfo.showInvitation = jSONObject.getBoolean(KEY_SHOW_INVITATION);
            }
            if (jSONObject.has(KEY_APPEND_SERIAL)) {
                inkRsInfo.appendSerial = jSONObject.getBoolean(KEY_APPEND_SERIAL);
            }
            return inkRsInfo;
        } catch (NullPointerException | JSONException unused) {
            return null;
        }
    }

    public static class InkRsInfo {
        public boolean appendSerial;
        public int buttonType = 0;
        public String landingUrl = null;
        public boolean showInvitation = false;

        public void copy(InkRsInfo inkRsInfo) {
            if (inkRsInfo != null) {
                this.landingUrl = inkRsInfo.landingUrl;
                this.buttonType = inkRsInfo.buttonType;
                this.showInvitation = inkRsInfo.showInvitation;
                this.appendSerial = inkRsInfo.appendSerial;
            }
        }

        public boolean needToDisplayInvitationDialog() {
            return this.showInvitation && this.landingUrl != null;
        }

        public int getButtonType() {
            return this.buttonType;
        }
    }
}
