package com.epson.mobilephone.common.wifidirect;

import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;


public abstract class ActivityConnectBase extends AppCompatActivity {
    public static final String APNAME = "com.epson.iprint.wifidirect.apname";
    public static final String APPASS = "com.epson.iprint.wifidirect.appass";
    public static final int CONNECTING_TIMEOUT = 30;
    public static final String CREATE = "com.epson.iprint.wifidirect.create";
    protected static final int ID_ENABLED_LOCATIONSETTINGS = 2;
    protected static final int ID_ENABLED_WIFI = 1;
    public static final int ID_SHOWWIFISETTINGS = 4;
    public static final int ID_SHOWWIFISETTINGS_PROFILEFAILED = 5;
    public static final String NEEDINFO = "com.epson.iprint.wifidirect.needinfo";
    public static final int RESULT_ERROR = 2;
    public static final int RESULT_USERCANCEL = 1;
    public static final String SHOWERROR = "com.epson.iprint.wifidirect.showerror";
    public static final String SHOWTIP = "com.epson.iprint.wifidirect.showconnecttip";
    private static final String TAG = "ActivityConnectBase";
    public static final String TIMEOUT = "com.epson.iprint.wifidirect.timeout";
    private static int sLastDetailResult = -1;
    ConnectingObserver connectingObserver = new ConnectingObserver();
    String expectedPrtMacAddr = "";
    WifiManager mWifiManager = null;
    boolean needInfo = false;
    SearchWiFiDirectPrinterTask searchWiFiDirectPrinter = null;
    boolean showConnectedTip = true;
    boolean showErrorDlg = false;
    protected Status status = Status.IDLE;
    String targetSsid = "";
    FindPrinterTask taskFindPrinter = null;
    int timeout = 30;

    enum Status {
        IDLE,
        WIFI_SCANNING,
        WIFI_CONNECTING,
        WIFI_CONNECTED,
        PRINTER_FINDING
    }

    private abstract void closeWaitingDialog();

    protected abstract void onEnabledWifi();

    private abstract void onError();

    public static int getLastDetailResult() {
        return sLastDetailResult;
    }

    public static void setLastDetailResult(int i) {
        sLastDetailResult = i;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mWifiManager = (WifiManager) getApplicationContext().getApplicationContext().getSystemService("wifi");
        requestWindowFeature(1);
        searchWiFiDirectPrinter = SearchWiFiDirectPrinterTask.getInstance(this);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            if (i2 != -1) {
                closeWaitingDialog();
                setResult(0);
                setLastDetailResult(1);
                finish();
                return;
            }
            onEnabledWifi();
        }
    }

    protected void onResume() {
        EPLog.m86d(TAG, "onResume()");
        super.onResume();
        NfcDispatchUtils.enableForegroundDispatch(this, (IntentFilter[]) null, (String[][]) null);
        EPLog.m86d(TAG, "addFlags : FLAG_KEEP_SCREEN_ON ");
        getWindow().addFlags(128);
    }

    protected void onPause() {
        EPLog.m86d(TAG, "onPause()");
        super.onPause();
        NfcDispatchUtils.disableForegroundDispatch(this);
        EPLog.m87e(TAG, "clearFlags : FLAG_KEEP_SCREEN_ON ");
        getWindow().clearFlags(128);
    }

    protected void onStop() {
        EPLog.m86d(TAG, "onStop()");
        super.onStop();
    }

    protected void onDestroy() {
        EPLog.m86d(TAG, "onDestroy()");
        super.onDestroy();
        interrupt();
    }

    public void onBackPressed() {
        EPLog.m86d(TAG, "onBackPressed()");
        super.onBackPressed();
        interrupt();
    }

    private void interrupt() {
        status = Status.IDLE;
        SearchWiFiDirectPrinterTask searchWiFiDirectPrinterTask = searchWiFiDirectPrinter;
        if (searchWiFiDirectPrinterTask != null) {
            searchWiFiDirectPrinterTask.interrupt();
            searchWiFiDirectPrinter = null;
        }
        interruptConnecting();
        interruptFindingPrinter();
    }

    private void interruptFindingPrinter() {
        FindPrinterTask findPrinterTask = taskFindPrinter;
        if (findPrinterTask != null) {
            findPrinterTask.cancel();
            taskFindPrinter = null;
        }
    }

    protected void interruptConnecting() {
        connectingObserver.interrupt();
    }

    private void onConnectedPrinter(Intent intent) {
        EPLog.m86d(TAG, "finish():RESULT_OK");
        closeWaitingDialog();
        setResult(-1, intent);
        setLastDetailResult(-1);
        finish();
    }

    private void setupObserver() {
        connectingObserver.start(this.timeout * 1000);
    }

    class ConnectingObserver {
        AsyncTask<Void, Void, Void> observerTask = null;

        ConnectingObserver() {
        }

        private void start(final int i) {
            interrupt();
            EPLog.m86d(ActivityConnectBase.TAG, "Start ConnectingObserver");
            observerTask = new AsyncTask<Void, Void, Void>() {
                protected Void doInBackground(Void... voidArr) {
                    int i = 0;
                    do {
                        try {
                            if (i >= i) {
                                return null;
                            }
                            Thread.sleep(100);
                            i += 100;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            return null;
                        }
                    } while (!isCancelled());
                    return null;
                }

                protected void onPostExecute(Void voidR) {
                    EPLog.m88i(ActivityConnectBase.TAG, "Timeout ConnectingObserver");
                    ActivityConnectBase.this.onError();
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }

        private void interrupt() {
            AsyncTask<Void, Void, Void> asyncTask = observerTask;
            if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING && !this.observerTask.isCancelled()) {
                EPLog.m86d(ActivityConnectBase.TAG, "Stop ConnectingObserver");
                observerTask.cancel(false);
            }
        }
    }
}
