package epson.print.Util;

import android.content.Context;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;

import java.util.Locale;

import epson.common.Constants;
import epson.print.CommonDefine;
import epson.print.MyPrinter;
import epson.print.R;

public class BuyInkUrl implements CommonDefine {
    private static final String NOZZLE_CHECK_GUIDANCE_URL_FORMAT = "https://support.epson.net/dvc_sn/?DeviceID=%s&PINF=support&EXE=iprint";

    public static Uri getNozzleCheckGuidanceUri(@NonNull Context context) {
        String printerDeviceId = MyPrinter.getPrinterDeviceId(context, false);
        if (printerDeviceId == null) {
            printerDeviceId = "";
        }
        String replaceFirst = printerDeviceId.replaceFirst("(?i)" + context.getString(R.string.epson) + " +", "");
        return Uri.parse(String.format(Locale.US, NOZZLE_CHECK_GUIDANCE_URL_FORMAT, new Object[]{Uri.encode(replaceFirst)}));
    }

    public static Uri getBuyInkUri(@NonNull Context context) {
        return Uri.parse(urlSupport(context, CommonDefine.URL_INK));
    }

    public static String urlSupport(Context context, String str) {
        boolean z;
        String serialNo;
        StringBuilder sb = new StringBuilder();
        sb.append(CommonDefine.headUrl);
        sb.append(CommonDefine.LG2);
        sb.append(Locale.getDefault().getLanguage());
        sb.append(CommonDefine.CN2);
        sb.append(Locale.getDefault().getCountry());
        sb.append(CommonDefine.CTC);
        if (str.equals(CommonDefine.URL_INK)) {
            sb.append(getPriunterLanguage(context));
            z = true;
        } else {
            sb.append(str);
            z = false;
        }
        sb.append(CommonDefine.PRN);
        sb.append(getPrinterName(context, z));
        sb.append(CommonDefine.OSC);
        sb.append(CommonDefine.ARD);
        sb.append(CommonDefine.OSV);
        sb.append(getArdVer());
        sb.append(CommonDefine.OATR);
        sb.append(Build.MODEL.replace(" ", CommonDefine.UNDER_BAR));
        if (str.equals(CommonDefine.URL_ONLINE_REGISTRATION)) {
            sb.append(CommonDefine.SID);
            sb.append(MyPrinter.getCurPrinter(context).getSerialNo());
        }
        if (str.equals(CommonDefine.URL_INK) && (serialNo = MyPrinter.getCurPrinter(context).getSerialNo()) != null && serialNo.length() >= 4) {
            sb.append(CommonDefine.PSI4);
            sb.append(serialNo.substring(0, 4));
        }
        return sb.toString();
    }

    private static String getPrinterName(Context context, boolean z) {
        String printerDeviceId = MyPrinter.getPrinterDeviceId(context, z);
        if (printerDeviceId == null || printerDeviceId == "") {
            printerDeviceId = CommonDefine.NO_PRINT;
        }
        if (printerDeviceId.equals(context.getString(R.string.str_lbl_title_scan))) {
            printerDeviceId = CommonDefine.NO_PRINT;
        }
        String replace = printerDeviceId.replace(" ", CommonDefine.UNDER_BAR);
        try {
            return replace.substring(0, replace.indexOf(CommonDefine.UNDER_BAR)).equalsIgnoreCase(context.getString(R.string.epson)) ? replace.substring(replace.indexOf(CommonDefine.UNDER_BAR) + 1) : replace;
        } catch (StringIndexOutOfBoundsException unused) {
            return replace;
        }
    }

    private static String getPriunterLanguage(Context context) {
        switch (context.getSharedPreferences("PrintSetting", 0).getInt(Constants.LANG, 1)) {
            case 1:
                return CommonDefine.URL_INK;
            case 7:
                return "BUYLPSUPPLY";
            default:
                return CommonDefine.URL_INK;
        }
    }

    private static String getArdVer() {
        return CommonDefine.ARDAPI + CommonDefine.UNDER_BAR + CommonDefine.ARDAPINUM + Build.VERSION.SDK_INT;
    }

    @NonNull
    public static String getFaqUrlString(Context context) {
        return urlSupport(context, CommonDefine.URL_FAQ);
    }
}
