package com.epson.mobilephone.common.wifidirect;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import com.epson.mobilephone.common.wifidirect.WiFiControl;
import com.epson.mobilephone.common.wifidirect.WifiP2pUtils;
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) {
            this.type = printer_type;
            this.macAddr = str;
            this.f157ip = str2;
            this.ssid = str3;
            this.printer = obj;
        }
    }

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

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

    public void updateP2pPrinterMacAddr() {
        EPLog.m86d(TAG, "updateP2pPrinterMacAddr()");
        WifiP2pUtils.ConnectionInfo connectionInfo = WifiP2pUtils.getInstance(this.mAppContext).getConnectionInfo();
        if (connectionInfo == null) {
            this.p2pPrinterMacAddr = null;
            return;
        }
        if (connectionInfo.isGroupOwnerThisDevice) {
            this.p2pPrinterMacAddr = WiFiControl.p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName);
        } else {
            this.p2pPrinterMacAddr = WiFiControl.p2pAddr2PtrAddrP2P(connectionInfo.p2PMacAdder, connectionInfo.printerName);
        }
        if (MacAddrFilter.getInstance(this.mAppContext).isExcludedMacAddress(WiFiControl.p2pAddr2PtrAddr(connectionInfo.p2PMacAdder, connectionInfo.printerName))) {
            this.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.m90w(TAG, "Invalid MacAddress");
                    return;
                } else if (str2.equals(this.p2pPrinterMacAddr)) {
                    EPLog.m86d(TAG, "Ignore Printer. This printer communicates via P2P:" + str);
                    return;
                } else {
                    Iterator<PrinterInfo> it = this.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.m86d(TAG, "Replace Duplicate Printer:" + next.ssid + "->" + str);
                                    int indexOf = this.uiPrinterList.indexOf(next.printer);
                                    if (-1 != indexOf) {
                                        this.listController.replacePrinter(indexOf, obj, i);
                                        z = false;
                                    } else {
                                        EPLog.m87e(TAG, "Illegal curPrinterList1 (Maybe replaced):" + str);
                                    }
                                }
                            }
                        }
                    }
                    if (z) {
                        this.listController.addPrinter(obj, i);
                    }
                    this.innerPrinterList.add(new PrinterInfo(PRINTER_TYPE.LOCAL, str2, str, (String) null, obj));
                    return;
                }
            case WIFIDIRECT:
                Iterator<PrinterInfo> it2 = this.innerPrinterList.iterator();
                while (true) {
                    if (it2.hasNext()) {
                        PrinterInfo next2 = it2.next();
                        String str4 = next2.macAddr;
                        if (str4 == null || str4.isEmpty()) {
                            EPLog.m90w(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.m86d(TAG, "Replace Duplicate Printer:" + next2.ssid + "->" + str);
                                            int indexOf2 = this.uiPrinterList.indexOf(next2.printer);
                                            if (-1 == indexOf2) {
                                                EPLog.m87e(TAG, "Illegal curPrinterList2 (Maybe replaced):" + str);
                                                break;
                                            } else {
                                                this.listController.replacePrinter(indexOf2, obj, i);
                                                z = false;
                                                break;
                                            }
                                        }
                                    case SimpleAP:
                                        if (str.equals(this.curConnectInfo)) {
                                            break;
                                        } else {
                                            EPLog.m86d(TAG, "Found Duplicate Printer:" + next2.ssid + "(" + str + ")");
                                            z = false;
                                            break;
                                        }
                                    default:
                                        EPLog.m90w(TAG, "Invalid Type");
                                        break;
                                }
                            }
                        } else {
                            String p2pPtrAddr2PtrAddr = WiFiControl.p2pPtrAddr2PtrAddr(str4);
                            if ((str4 != null && str4.equals(str2)) || (p2pPtrAddr2PtrAddr != null && p2pPtrAddr2PtrAddr.equals(str2))) {
                                String curConnectInfo2 = this.wiFiControl.getCurConnectInfo(str4);
                                if (curConnectInfo2 != null) {
                                    int[] iArr = C11211.f155x793e0ec3;
                                    WiFiControl wiFiControl2 = this.wiFiControl;
                                    if (iArr[WiFiControl.getSSIDType(str).ordinal()] != 2) {
                                        z2 = z;
                                    } else if (curConnectInfo2.equals(this.curConnectInfo)) {
                                        EPLog.m86d(TAG, "Replace Duplicate Printer:" + next2.f157ip + "->" + str);
                                        int indexOf3 = this.uiPrinterList.indexOf(next2.printer);
                                        if (-1 != indexOf3) {
                                            this.listController.replacePrinter(indexOf3, obj, i);
                                        } else {
                                            EPLog.m87e(TAG, "Illegal curPrinterList3 (Maybe replaced):" + str);
                                        }
                                    } else {
                                        EPLog.m86d(TAG, "Found Duplicate Printer:" + next2.f157ip + "(" + str + ")");
                                    }
                                } else {
                                    EPLog.m86d(TAG, "Found Duplicate Printer:" + next2.f157ip + "(" + str + ")");
                                }
                            }
                        }
                    } else {
                        z2 = z;
                    }
                }
                if (z2) {
                    this.listController.addPrinter(obj, i);
                }
                this.innerPrinterList.add(new PrinterInfo(PRINTER_TYPE.WIFIDIRECT, str2, (String) null, str, obj));
                return;
            case USB:
                this.listController.addPrinter(obj, i);
                return;
            default:
                EPLog.m90w(TAG, "Un known Type");
                return;
        }
    }

    private PRINTER_TYPE getType(String str) {
        WiFiControl.ConnectType connectType = WiFiControl.ConnectType.NONE;
        WiFiControl wiFiControl2 = this.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;
    }
}
