package com.epson.iprint.prtlogger;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.box.androidsdk.content.BoxConstants;
import com.epson.mobilephone.common.EpLog;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;


import epson.print.IprintApplication;
import epson.print.MyPrinter;
import epson.print.Util.EPLog;
import epson.print.copy.Component.ecopycomponent.CopyParams;
import epson.print.rpcopy.Component.ecopycomponent.RepeatCopyAnalyticsParams;
import epson.print.screen.PrintProgress;
import epson.print.screen.PrintSetting;
import epson.scan.lib.I1ScanParams;

public class Analytics {
    public static final String ACTION_ID_HOME_BUY_INK = "Home-BuyInk";
    public static final String ACTION_ID_HOME_CAMERACOPY = "Home-CameraCopy";
    public static final String ACTION_ID_HOME_COPY = "Home-Copy";
    public static final String ACTION_ID_HOME_DOCUMENT = "Home-Document";
    public static final String ACTION_ID_HOME_INFO = "Home-Info";
    public static final String ACTION_ID_HOME_MAINTAIN = "Home-Maintain";
    public static final String ACTION_ID_HOME_MEMORYCARD_ACCESS = "Home-MemoryCardAccess";
    public static final String ACTION_ID_HOME_ONLINE_STORAGE = "Home-OnlineStorage";
    public static final String ACTION_ID_HOME_PHOTO = "Home-Photo";
    public static final String ACTION_ID_HOME_PHOTO_TRANSFER = "Home-PhotoTransfer";
    public static final String ACTION_ID_HOME_READY_INK = "Home-ReadyInk";
    public static final String ACTION_ID_HOME_SCAN = "Home-Scan";
    public static final String ACTION_ID_HOME_WEB = "Home-Web";
    public static final String ACTION_ID_MAINTENANCE_BUY_INK = "Maintenance-BuyInk";
    public static final String ACTION_ID_MAINTENANCE_FIRMWARE_UPDATE = "Firmware Update";
    public static final String ACTION_ID_MAINTENANCE_HEAD_CLEANING = "HeadCleaning";
    public static final String ACTION_ID_MAINTENANCE_NOZZLE_CHECK = "NozzlleCheck";
    public static final String ACTION_ID_MAINTENANCE_READY_INK = "Maintenance-ReadyInk";
    public static final String ACTION_ID_SETUP_BLE = "PrinterSetup-BLE";
    public static final int CONNECTION_TYPE_NO_CONNECTION_TYPE = -1;
    public static final int CONNECTION_TYPE_OTG = 11;
    public static final int CONNECTION_TYPE_REMOTE = 10;
    private static final int CUSTOM_DIMENSION_BLE_PATH = 1;
    private static final int CUSTOM_DIMENSION_BLE_RESULT = 2;
    private static final int CUSTOM_DIMENSION_DEBUG = 28;
    private static final String CUSTOM_DIMENSION_DEBUG_VALUE = "develop";
    public static final String EXTENSION_STRING_WEB = "http";
    public static final String SCREEN_ID_HOME = "Home";
    private static final String TAG = "Analytics";
    public static final int WIFI_DIRECT_TYPE_NORMAL_WIFI_DIRECT = 1;
    public static final int WIFI_DIRECT_TYPE_NOT_WIFI_DIRECT = 0;
    public static final int WIFI_DIRECT_TYPE_SIMPLE_AP = 2;

    private static String getActionString(int i) {
        if (i == 4100) {
            return "Intent-Web";
        }
        if (i == 4353) {
            return "Intent-Scan";
        }
        switch (i) {
            case 1:
                return "Photos";
            case 2:
                return "Saved Documents";
            case 3:
                return "Web";
            case 4:
                return "Scan-Print";
            case 5:
                return "Document Capture";
            default:
                switch (i) {
                    case 256:
                        return "Evernote";
                    case 257:
                        return "Google Drive";
                    case PrintLog.PRINT_SOURCE_DROPBOX /*258*/:
                        return "Dropbox";
                    case PrintLog.PRINT_SOURCE_BOX /*259*/:
                        return "Box";
                    case PrintLog.PRINT_SOURCE_ONEDRIVE /*260*/:
                        return "OneDrive";
                    case PrintLog.PRINT_SOURCE_MYPOCKET /*261*/:
                        return "MyPocket";
                    default:
                        switch (i) {
                            case 4097:
                                return "Intent-Photos";
                            case 4098:
                                return "Intent-Documents";
                            default:
                                switch (i) {
                                    case PrintLog.ACTION_ID_MEMORY_CARD_COPY_TO_PRINTER:
                                    case PrintLog.ACTION_ID_MEMORY_CARD_COPY_TO_MOBILE_DEVICE:
                                        return "Memory Access";
                                    case PrintLog.ACTION_PHOTO_TRANSFER:
                                        return "Send Photo";
                                    case PrintLog.ACTION_COPY:
                                        return "Copy";
                                    case PrintLog.ACTION_SCAN:
                                        return "Scan";
                                    case PrintLog.ACTION_REPEAT_COPY:
                                        return "RepeatCopy";
                                    default:
                                        return "";
                                }
                        }
                }
        }
    }

    @NonNull
    private static String getConnectionTypeString(int i) {
        switch (i) {
            case 0:
                return "Infra";
            case 1:
                return "Wi-Fi Direct";
            case 2:
                return "SimpleAP";
            default:
                switch (i) {
                    case 10:
                        return "Remote";
                    case 11:
                        return "OTG";
                    default:
                        return "";
                }
        }
    }

    private static String getDateString(int i) {
        return i == 0 ? "OFF" : "ON";
    }

    private static String getOnOff(boolean z) {
        return z ? "ON" : "OFF";
    }

    private static String getScanColorTypeString(int i) {
        switch (i) {
            case 1:
                return "Color";
            case 2:
                return "Grayscale";
            case 3:
                return "Monochrome";
            default:
                return "";
        }
    }

    private static String getScanDeviceString(int i) {
        switch (i) {
            case 0:
                return "DocumentTable";
            case 1:
                return "ADF";
            default:
                return "";
        }
    }

    private static String getScanGammaString(int i) {
        return i != 0 ? "1" : BoxConstants.ROOT_FOLDER_ID;
    }

    public enum SetUpResult {
        Success("Success"),
        Fail("Fail"),
        Error("Error"),
        Error106("Error-106"),
        Error119("Error-119");

        private final String name;

        private SetUpResult(String str) {
            name = str;
        }

        public String toString() {
            return name;
        }
    }

    public enum SetUpPath {
        Home("HomePopup"),
        Button("SetUpButton");

        private final String name;

        private SetUpPath(String str) {
            name = str;
        }

        public String toString() {
            return name;
        }
    }

    public static void sendPrintLog(@NonNull Context context, @NonNull PrintProgress.ProgressParams progressParams) {
        if (NewLoggerController.isLoggerEnabled(context)) {
            PrintLog printLog = progressParams.getPrintLog();
            if (printLog == null) {
                printLog = new PrintLog();
            }
            int printNumber = AnalyticsPreferences.getPrintNumber(context);
            EpLog.m77i("    **************     " + printNumber);
            if (printNumber > 0) {
                PrintSetting printSetting = progressParams.getPrintSetting(context);
                printSetting.loadSettings();
                HitBuilders.EventBuilder value = createEventBuilder().setCategory("function").setAction(getActionString(printLog)).setValue((long) (printNumber * printSetting.copiesValue));
                String defaultPrinterName = getDefaultPrinterName(context);
                if (defaultPrinterName != null) {
                    value.setLabel(defaultPrinterName);
                }
                ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) value.setCustomDimension(2, getConnectionString(context))).setCustomDimension(3, Integer.toString(printSetting.paperSizeValue))).setCustomDimension(4, Integer.toString(printSetting.paperTypeValue))).setCustomDimension(5, Integer.toString(printSetting.qualityValue))).setCustomDimension(6, Integer.toString(getLayoutValue(printSetting)))).setCustomDimension(7, Integer.toString(printSetting.paperSourceValue))).setCustomDimension(8, Integer.toString(printSetting.colorValue))).setCustomDimension(9, Integer.toString(printSetting.duplexValue));
                if (printLog.previewType == 1) {
                    ((HitBuilders.EventBuilder) value.setCustomDimension(11, progressParams.getApfMode() ? "ON" : "OFF")).setCustomDimension(10, getDateString(printSetting.printdate));
                }
                String sizeLimitString = getSizeLimitString(printLog.originalFileExtension, 150);
                if (!sizeLimitString.isEmpty()) {
                    value.setCustomDimension(1, sizeLimitString);
                }
                String sizeLimitString2 = getSizeLimitString(printLog.callerPackage, 150);
                if (!sizeLimitString2.isEmpty()) {
                    value.setCustomDimension(12, sizeLimitString2);
                }
                IprintApplication.getInstance().getDefaultTracker().send(value.build());
            }
        }
    }

    private static int getLayoutValue(PrintSetting printSetting) {
        int i = printSetting.layoutMultiPageValue;
        if (i == 0) {
            return printSetting.layoutValue;
        }
        if (i == 65536) {
            return 256;
        }
        if (i != 131072) {
            return i != 262144 ? 2 : 1024;
        }
        return 512;
    }

    @NonNull
    static String getSizeLimitString(String str, int i) {
        if (str == null) {
            return "";
        }
        int length = str.length();
        if (i > length) {
            i = length;
        }
        return str.substring(0, i);
    }

    public static String getDefaultPrinterName(@NonNull Context context) {
        return MyPrinter.getCurPrinter(context).getName();
    }

    @NonNull
    private static String getConnectionString(@NonNull Context context) {
        return getConnectionTypeString(AnalyticsPreferences.getConnectionPath(context));
    }

    private static String getActionString(PrintLog printLog) {
        return printLog == null ? "" : getActionString(printLog.uiRoute);
    }

    public static void sendScanI1Log(Context context, I1ScanParams i1ScanParams, CommonLog commonLog, boolean z) {
        if (commonLog != null && context != null && i1ScanParams != null && NewLoggerController.isLoggerEnabled(context)) {
            HitBuilders.EventBuilder createEventBuilder = createEventBuilder();
            setCommonData(commonLog, createEventBuilder);
            setScanDataFromI1(i1ScanParams, createEventBuilder, z);
            sendData(createEventBuilder);
        }
    }

    private static void setScanDataFromI1(@NonNull I1ScanParams i1ScanParams, @NonNull HitBuilders.EventBuilder eventBuilder, boolean z) {
        ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) eventBuilder.setCustomDimension(13, getScanDeviceString(i1ScanParams.inputDevice))).setCustomDimension(15, getScanColorTypeString(i1ScanParams.colorType))).setCustomDimension(16, Integer.toString(i1ScanParams.resolution))).setCustomDimension(17, getOnOff(i1ScanParams.twoSide != 0))).setCustomDimension(18, getScanGammaString(i1ScanParams.gamma))).setCustomDimension(19, Integer.toString(i1ScanParams.brightness))).setCustomDimension(41, getOnOff(z));
    }

    public static void sendCopyLog(@NonNull Context context, CopyParams copyParams, CommonLog commonLog) {
        if (copyParams != null && NewLoggerController.isLoggerEnabled(context)) {
            HitBuilders.EventBuilder createEventBuilder = createEventBuilder();
            setCommonData(commonLog, createEventBuilder);
            setCopyData(copyParams, createEventBuilder);
            sendData(createEventBuilder);
        }
    }

    private static void setCopyData(@NonNull CopyParams copyParams, @NonNull HitBuilders.EventBuilder eventBuilder) {
        ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) eventBuilder.setCustomDimension(20, copyParams.colorMode)).setCustomDimension(21, copyParams.density)).setCustomDimension(22, copyParams.magnification)).setCustomDimension(23, copyParams.paperSize)).setCustomDimension(24, copyParams.paperType)).setCustomDimension(25, copyParams.printDevice)).setCustomDimension(26, copyParams.copyType)).setCustomDimension(27, copyParams.copyQuality);
    }

    public static void sendRepeatCopyLog(@NonNull Context context, RepeatCopyAnalyticsParams repeatCopyAnalyticsParams, @NonNull CommonLog commonLog) {
        if (repeatCopyAnalyticsParams != null && NewLoggerController.isLoggerEnabled(context)) {
            HitBuilders.EventBuilder createEventBuilder = createEventBuilder();
            setCommonData(commonLog, createEventBuilder);
            setRepeatCopyData(repeatCopyAnalyticsParams, createEventBuilder);
            sendData(createEventBuilder);
        }
    }

    private static void setRepeatCopyData(@NonNull RepeatCopyAnalyticsParams repeatCopyAnalyticsParams, @NonNull HitBuilders.EventBuilder eventBuilder) {
        ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) ((HitBuilders.EventBuilder) eventBuilder.setCustomDimension(30, repeatCopyAnalyticsParams.colorMode)).setCustomDimension(31, repeatCopyAnalyticsParams.density)).setCustomDimension(32, repeatCopyAnalyticsParams.repeatLayout)).setCustomDimension(33, repeatCopyAnalyticsParams.paperSize)).setCustomDimension(34, repeatCopyAnalyticsParams.paperType)).setCustomDimension(35, repeatCopyAnalyticsParams.printDevice)).setCustomDimension(36, repeatCopyAnalyticsParams.copyQuality)).setCustomDimension(37, repeatCopyAnalyticsParams.removeBackground);
        sendDataIfNonNull(eventBuilder, 38, repeatCopyAnalyticsParams.XCutLine);
        sendDataIfNonNull(eventBuilder, 39, repeatCopyAnalyticsParams.XCutLineStyle);
        sendDataIfNonNull(eventBuilder, 40, repeatCopyAnalyticsParams.XCutLineWeight);
    }

    private static void sendDataIfNonNull(@NonNull HitBuilders.EventBuilder eventBuilder, int i, @Nullable String str) {
        if (str != null) {
            eventBuilder.setCustomDimension(i, str);
        }
    }

    public static void sendCommonLog(Context context, CommonLog commonLog) {
        if (NewLoggerController.isLoggerEnabled(context) && commonLog != null && context != null) {
            HitBuilders.EventBuilder createEventBuilder = createEventBuilder();
            setCommonData(commonLog, createEventBuilder);
            sendData(createEventBuilder);
        }
    }

    private static void setCommonData(@NonNull CommonLog commonLog, @NonNull HitBuilders.EventBuilder eventBuilder) {
        eventBuilder.setCategory("function").setAction(getActionString(commonLog.action)).setValue((long) commonLog.numberOfSheet);
        int i = commonLog.connectionType;
        if (i >= 0) {
            eventBuilder.setCustomDimension(2, getConnectionTypeString(i));
        }
        if (commonLog.printerName != null) {
            eventBuilder.setLabel(commonLog.printerName);
        }
        if (commonLog.callerPackage != null) {
            eventBuilder.setCustomDimension(12, getSizeLimitString(commonLog.callerPackage, 150));
        }
        setDirectionData(commonLog, eventBuilder);
    }

    private static void sendData(@NonNull HitBuilders.EventBuilder eventBuilder) {
        IprintApplication.getInstance().getDefaultTracker().send(eventBuilder.build());
    }

    private static void setDirectionData(CommonLog commonLog, HitBuilders.EventBuilder eventBuilder) {
        String str;
        switch (commonLog.action) {
            case PrintLog.ACTION_ID_MEMORY_CARD_COPY_TO_PRINTER /*8193*/:
                str = "toPrinter";
                break;
            case 8194:
                str = "toSmartPhone";
                break;
            default:
                return;
        }
        eventBuilder.setCustomDimension(1, str);
    }

    public static void savePrintInfo(@NonNull Context context, int i) {
        EpLog.m77i("  @@@@@@@@@@@@@     printSheet = " + i);
        AnalyticsPreferences.savePrintNumber(context, i);
        AnalyticsPreferences.saveConnectionPath(context, getConnectionType(context));
    }

    public static void cleanPrintNumber(@NonNull Context context) {
        AnalyticsPreferences.savePrintNumber(context, 0);
    }

    public static int getConnectionType(@NonNull Context context) {
        if (MyPrinter.isRemotePrinter(context)) {
            return 10;
        }
        if (MyPrinter.isPrinterRouteOtg(context)) {
            return 11;
        }
        return getWifiDirectTypeIprintV4(context);
    }

    public static void countScreen(@NonNull Context context, String str) {
        if (NewLoggerController.isLoggerEnabled(context)) {
            if (str == null) {
                EPLog.m318w(TAG, "ERROR: countScreen() screenId == null");
                return;
            }
            Tracker defaultTracker = IprintApplication.getInstance().getDefaultTracker();
            defaultTracker.setScreenName(str);
            defaultTracker.send(createScreenViewBuilder().build());
        }
    }

    public static void sendAction(@NonNull Context context, String str) {
        if (NewLoggerController.isLoggerEnabled(context)) {
            if (str == null) {
                EPLog.m318w(TAG, "ERROR: sendAction() actionId == null");
            } else {
                IprintApplication.getInstance().getDefaultTracker().send(createEventBuilder().setCategory("tap").setAction(str).build());
            }
        }
    }

    public static void sendSetup(String str, SetUpPath setUpPath, SetUpResult setUpResult) {
        HitBuilders.EventBuilder label = createEventBuilder().setCategory("setup").setAction(ACTION_ID_SETUP_BLE).setLabel(str);
        label.setCustomDimension(1, setUpPath.toString());
        label.setCustomDimension(2, setUpResult.toString());
        IprintApplication.getInstance().getDefaultTracker().send(label.build());
    }

    private static HitBuilders.EventBuilder createEventBuilder() {
        HitBuilders.EventBuilder eventBuilder = new HitBuilders.EventBuilder();
        if (isAnalyticsDebug()) {
            eventBuilder.setCustomDimension(28, CUSTOM_DIMENSION_DEBUG_VALUE);
        }
        return eventBuilder;
    }

    private static HitBuilders.ScreenViewBuilder createScreenViewBuilder() {
        HitBuilders.ScreenViewBuilder screenViewBuilder = new HitBuilders.ScreenViewBuilder();
        if (isAnalyticsDebug()) {
            screenViewBuilder.setCustomDimension(28, CUSTOM_DIMENSION_DEBUG_VALUE);
        }
        return screenViewBuilder;
    }

    private static boolean isAnalyticsDebug() {
        return IprintApplication.isConnectStaging();
    }

    public static int getWifiDirectTypeIprintV4(Context context) {
        if (!WiFiDirectManager.isWifiDirectFY13(context) && !WiFiDirectManager.isWifiDirectP2P(context)) {
            return WiFiDirectManager.isSimpleAP(context) ? 2 : 0;
        }
        return 1;
    }
}
