package com.epson.mobilephone.common.wifidirect;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

import epson.print.MyPrinter;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;

public class WiFiDirectPrinterListUtils {
    private static final String TAG = "WiFiDirectPrinterListUtils";
    private String curConnectInfo;
    private ArrayList<PrinterInfo> innerPrinterList = null;
    private ListController listController = null;
    private Context mAppContext = null;
    private String p2pPrinterMacAddr;
    private AbstractList<Object> uiPrinterList = null;
    private WiFiControl wiFiControl = null;

    public interface ListController {
        void addPrinter(Object obj, int i);

        void replacePrinter(int i, Object obj, int i2);
    }

    enum PRINTER_TYPE {
        USB,
        LOCAL,
        WIFIDIRECT
    }

    private class PrinterInfo {

        /* renamed from: ip */
        String f157ip;
        String macAddr;
        Object printer;
        String ssid;
        PRINTER_TYPE type;

        public PrinterInfo(PRINTER_TYPE printer_type, String str, String str2, String str3, Object obj) {
            type = printer_type;
            macAddr = str;
            f157ip = str2;
            ssid = str3;
            printer = obj;
        }
    }

    public WiFiDirectPrinterListUtils(Context context, AbstractList<Object> abstractList, ListController listController2, String str) {
        mAppContext = context.getApplicationContext();
        wiFiControl = WiFiControl.getInstance(mAppContext);
        innerPrinterList = new ArrayList<>();
        uiPrinterList = abstractList;
        listController = listController2;
        curConnectInfo = str;
        clearPrinterInfoList();
    }

    public void clearPrinterInfoList() {
        EPLog.d(TAG, "clearPrinterInfoList()");
        innerPrinterList.clear();
        updateP2pPrinterMacAddr();
    }

    public void updateP2pPrinterMacAddr() {
        EPLog.d(TAG, "updateP2pPrinterMacAddr()");
        WifiP2pUtils.ConnectionInfo connectionInfo = WifiP2pUtils.getInstance(mAppContext).getConnectionInfo();
        if (connectionInfo == null) {
            p2pPrinterMacAddr = null;
            return;
        }
        if (connectionInfo.isGroupOwnerThisDevice) {
            p2pPrinterMacAddr = WiFiControl.p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName);
        } else {
            p2pPrinterMacAddr = WiFiControl.p2pAddr2PtrAddrP2P(connectionInfo.p2PMacAdder, connectionInfo.printerName);
        }
        if (MacAddrFilter.getInstance(mAppContext).isExcludedMacAddress(WiFiControl.p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName))) {
            p2pPrinterMacAddr = null;
        }
    }

    public static String getCurPrinterString(Activity activity, String str, String str2) {
        if (str2 == null) {
            return str;
        }
        return (!WiFiControl.ConnectType.SimpleAP.equals(WiFiControl.getSSIDType(str2)) || Build.VERSION.SDK_INT < 26 || !WiFiControl.isSimpleAP(WiFiUtils.getInstance(activity).getCurSSID()) || activity.checkSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == 0) ? str2 : str;
    }

    public void addPrinter(Object obj, int i, String str, String str2) {
        boolean z = true;
        boolean z2 = false;
        switch (getType(str)) {
            case LOCAL:
                if (str2 == null || str2.isEmpty()) {
                    EPLog.w(TAG, "Invalid MacAddress");
                    return;
                } else if (str2.equals(this.p2pPrinterMacAddr)) {
                    EPLog.d(TAG, "Ignore Printer. This printer communicates via P2P:" + str);
                    return;
                } else {
                    Iterator<PrinterInfo> it = innerPrinterList.iterator();
                    while (true) {
                        if (it.hasNext()) {
                            PrinterInfo next = it.next();
                            if (!PRINTER_TYPE.LOCAL.equals(next.type)) {
                                String str3 = next.macAddr;
                                if (str2 != null && str2.equals(str3)) {
                                    EPLog.d(TAG, "Replace Duplicate Printer:" + next.ssid + "->" + str);
                                    int indexOf = uiPrinterList.indexOf(next.printer);
                                    if (-1 != indexOf) {
                                        listController.replacePrinter(indexOf, obj, i);
                                        z = false;
                                    } else {
                                        EPLog.e(TAG, "Illegal curPrinterList1 (Maybe replaced):" + str);
                                    }
                                }
                            }
                        }
                    }
                    if (z) {
                        listController.addPrinter(obj, i);
                    }
                    innerPrinterList.add(new PrinterInfo(PRINTER_TYPE.LOCAL, str2, str, (String) null, obj));
                    return;
                }
            case WIFIDIRECT:
                Iterator<PrinterInfo> it2 = innerPrinterList.iterator();
                while (true) {
                    if (it2.hasNext()) {
                        PrinterInfo next2 = it2.next();
                        String str4 = next2.macAddr;
                        if (str4 == null || str4.isEmpty()) {
                            EPLog.w(TAG, "Invalid MacAddress");
                        } else if (PRINTER_TYPE.WIFIDIRECT.equals(next2.type)) {
                            if (str4 != null && str4.equals(str2)) {
                                switch (WiFiControl.getSSIDType(str)) {
                                    case WiFiP2P:
                                        if (next2.ssid.equals(this.curConnectInfo)) {
                                            break;
                                        } else {
                                            EPLog.d(TAG, "Replace Duplicate Printer:" + next2.ssid + "->" + str);
                                            int indexOf2 = uiPrinterList.indexOf(next2.printer);
                                            if (-1 == indexOf2) {
                                                EPLog.e(TAG, "Illegal curPrinterList2 (Maybe replaced):" + str);
                                                break;
                                            } else {
                                                listController.replacePrinter(indexOf2, obj, i);
                                                z = false;
                                                break;
                                            }
                                        }
                                    case SimpleAP:
                                        if (str.equals(this.curConnectInfo)) {
                                            break;
                                        } else {
                                            EPLog.d(TAG, "Found Duplicate Printer:" + next2.ssid + "(" + str + ")");
                                            z = false;
                                            break;
                                        }
                                    default:
                                        EPLog.w(TAG, "Invalid Type");
                                        break;
                                }
                            }
                        } else {
                            String p2pPtrAddr2PtrAddr = WiFiControl.p2pPtrAddr2PtrAddr(str4);
                            if ((str4 != null && str4.equals(str2)) || (p2pPtrAddr2PtrAddr != null && p2pPtrAddr2PtrAddr.equals(str2))) {
                                String curConnectInfo2 = wiFiControl.getCurConnectInfo(str4);
                                if (curConnectInfo2 != null) {
                                    int[] iArr = C11211.f155x793e0ec3;
                                    WiFiControl wiFiControl2 = wiFiControl;
                                    if (iArr[WiFiControl.getSSIDType(str).ordinal()] != 2) {
                                        z2 = z;
                                    } else if (curConnectInfo2.equals(this.curConnectInfo)) {
                                        EPLog.d(TAG, "Replace Duplicate Printer:" + next2.f157ip + "->" + str);
                                        int indexOf3 = uiPrinterList.indexOf(next2.printer);
                                        if (-1 != indexOf3) {
                                            listController.replacePrinter(indexOf3, obj, i);
                                        } else {
                                            EPLog.e(TAG, "Illegal curPrinterList3 (Maybe replaced):" + str);
                                        }
                                    } else {
                                        EPLog.d(TAG, "Found Duplicate Printer:" + next2.f157ip + "(" + str + ")");
                                    }
                                } else {
                                    EPLog.d(TAG, "Found Duplicate Printer:" + next2.f157ip + "(" + str + ")");
                                }
                            }
                        }
                    } else {
                        z2 = z;
                    }
                }
                if (z2) {
                    listController.addPrinter(obj, i);
                }
                innerPrinterList.add(new PrinterInfo(PRINTER_TYPE.WIFIDIRECT, str2, (String) null, str, obj));
                return;
            case USB:
                listController.addPrinter(obj, i);
                return;
            default:
                EPLog.w(TAG, "Un known Type");
                return;
        }
    }

    private PRINTER_TYPE getType(String str) {
        WiFiControl.ConnectType connectType = WiFiControl.ConnectType.NONE;
        WiFiControl wiFiControl2 = wiFiControl;
        if (!connectType.equals(WiFiControl.getSSIDType(str))) {
            return PRINTER_TYPE.WIFIDIRECT;
        }
        if (MyPrinter.PRINTER_USB.equals(str)) {
            return PRINTER_TYPE.USB;
        }
        return PRINTER_TYPE.LOCAL;
    }
}
