package com.epson.mobilephone.common.ble;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;

import com.epson.mobilephone.common.ble.util.BLEUtility;
import com.epson.mobilephone.common.ble.util.ScannedDevice;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import epson.print.R;
import epson.print.Util.EPLog;

public class BleAsyncTask extends AsyncTask<Void, Void, Boolean> {
    private static AlertDialog dialog = null;
    private static HandlerThread handlerThread = null;
    private static boolean mStop = false;
    private static BleAsyncTask mTask;
    private static Runnable mTimeOutTask;
    private BleScanWork mBleWork = null;
    private BLEUtility.BleWorkCallback mCallBack = null;
    private List<ScannedDevice> mDevicelist = null;
    private Boolean mFind = false;
    private long mTimeOut = -1;
    private long mTimeOutScan = -1;
    private Activity m_Activity;

    public static void setStop(boolean z) {
        EPLog.d("mStop   " + z);
        mStop = z;
        if (z) {
            EPLog.d("★ stopScan");
            BleScanWork.getInstace().stopScan();
            if (mTimeOutTask != null) {
                mTimeOutTask = null;
                BleAsyncTask bleAsyncTask = mTask;
                if (bleAsyncTask != null) {
                    cancel(true);
                }
            }
        }
    }

    public BleAsyncTask(Activity activity) {
        mTimeOutTask = new Runnable() {
            public void run() {
                try {
                    get(mTimeOut, TimeUnit.MILLISECONDS);
                } catch (InterruptedException unused) {
                    EPLog.e("InterruptedException");
                } catch (ExecutionException unused2) {
                    EPLog.e("ExecutionException");
                } catch (TimeoutException unused3) {
                    EPLog.i("Running task has timed out!!");
                    if (mBleWork != null) {
                        mBleWork.stopScan();
                    }
                    mTask.cancel(true);
                } catch (CancellationException unused4) {
                    EPLog.e("CancellationException");
                }
            }
        };
        m_Activity = activity;
        mTimeOut = (long) (BleWork.getScanAdvertisingPacketTimeout() + 2000);
        mTimeOutScan = -1;
    }

    public BleAsyncTask(Activity activity, BLEUtility.BleWorkCallback bleWorkCallback, int i) {
        mTimeOutTask = new Runnable() {
            public void run() {
                try {
                    get(mTimeOut, TimeUnit.MILLISECONDS);
                } catch (InterruptedException unused) {
                    EPLog.e("InterruptedException");
                } catch (ExecutionException unused2) {
                    EPLog.e("ExecutionException");
                } catch (TimeoutException unused3) {
                    EPLog.i("Running task has timed out!!");
                    if (mBleWork != null) {
                        mBleWork.stopScan();
                    }
                    mTask.cancel(true);
                } catch (CancellationException unused4) {
                    EPLog.e("CancellationException");
                }
            }
        };
        m_Activity = activity;
        mTimeOut = (long) (i + 2000);
        mTimeOutScan = (long) i;
        mCallBack = bleWorkCallback;
    }

    protected void onPreExecute() {
        super.onPreExecute();
        mBleWork = BleScanWork.getInstace();
    }


    public java.lang.Boolean doInBackground(java.lang.Void... r5) {

        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.doInBackground(java.lang.Void[]):java.lang.Boolean");
    }

    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        EPLog.d("" + bool);
    }

    private void showAdvanceToSettingDialog() {
        if (mCallBack != null) {
            mDevicelist = mBleWork.getScannedDeviceList();
            ArrayList arrayList = new ArrayList();
            for (ScannedDevice next : mDevicelist) {
                arrayList.add(new ScannedDevice(next.getDevice(), next.getRssi(), next.getDisplayName(), next.getHardwareType()));
            }
            EPLog.d(" ★　mDevicelist " + arrayList.size());
            mCallBack.call(arrayList);
            return;
        }
        dialog = new AlertDialog.Builder(m_Activity).setCancelable(false).setMessage(m_Activity.getString(R.string.BLE_find_unconfigured_printer_title)).setPositiveButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_setup_button), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                EPLog.d("★ stopScan");
                mBleWork.stopScan();
                BleAsyncTask bleAsyncTask = BleAsyncTask.this;
                List unused = mDevicelist = mBleWork.getScannedDeviceList();
                ArrayList arrayList = new ArrayList();
                for (ScannedDevice scannedDevice : mDevicelist) {
                    arrayList.add(new ScannedDevice(scannedDevice.getDevice(), scannedDevice.getRssi(), scannedDevice.getDisplayName(), scannedDevice.getHardwareType()));
                }
                Intent intent = new Intent(m_Activity.getApplicationContext(), BleWorkActivity.class);
                intent.putParcelableArrayListExtra(ScannedDevice.TYPE, arrayList);
                m_Activity.startActivity(intent);
            }
        }).setNegativeButton(m_Activity.getString(R.string.BLE_find_unconfigured_printer_not_now_button), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                EPLog.d("★ stopScan");
                mBleWork.stopScan();
                setStop(true);
            }
        }).create();
        dialog.show();
    }

    protected void onCancelled(Boolean bool) {
        super.onCancelled(bool);
        EPLog.d("死にました。  " + bool);
    }

    protected void onCancelled() {
        super.onCancelled();
    }

    public BleAsyncTask setTimeOut(long j) {
        mTimeOut = j;
        return this;
    }

    public static void blePrinterCheck(Context context) {
        setStop(false);
        EPLog.i();
        AlertDialog alertDialog = dialog;
        if (alertDialog == null || !alertDialog.isShowing()) {
            final Activity activity = (Activity) context;
            if (BLEUtility.isThreadAlive("bleCheck")) {
                EPLog.d("isAlive bleCheck");
            } else if (handlerThread != null) {
                EPLog.e("handlerThread != null   ");
            } else {
                EPLog.i("★　　　　　handlerThread　start   ");
                handlerThread = new HandlerThread("bleCheck");
                handlerThread.start();
                new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
                    public void run() {
                        EPLog.i(" 0 handlerThread run !!!!");
                        handlerThread.quitSafely();
                        new BleAsyncTask(activity).execute(new Void[0]);
                    }
                }, 500);
            }
        }
    }

    public static void blePrinterCheck(Context context, final BLEUtility.BleWorkCallback bleWorkCallback) {
        setStop(false);
        EPLog.i();
        final Activity activity = (Activity) context;
        if (BLEUtility.isThreadAlive("bleCheck")) {
            EPLog.d("　　　‼　isAlive bleCheck");
            return;
        }
        EPLog.i("handlerThread");
        handlerThread = new HandlerThread("bleCheck");
        handlerThread.start();
        new Handler(handlerThread.getLooper()).postDelayed(new Runnable() {
            public void run() {
                EPLog.i(" handlerThread run !!!!");
                handlerThread.quitSafely();
                new BleAsyncTask(activity, bleWorkCallback, BleWork.getScanAdvertisingPacketTimeout()).execute(new Void[0]);
            }
        }, 1000);
    }

    public static void blePrinterCheckStop() {
        EPLog.i();
        if (handlerThread != null) {
            setStop(true);
            handlerThread.quitSafely();
            handlerThread = null;
            AlertDialog alertDialog = dialog;
            if (alertDialog != null && alertDialog.isShowing()) {
                dialog.dismiss();
            }
            Thread thread = BLEUtility.getThread("bleCheck");
            if (thread != null && thread.isAlive()) {
                EPLog.i("interrupt");
                thread.interrupt();
                try {
                    EPLog.i("join");
                    thread.join(50);
                } catch (InterruptedException e) {
                    EPLog.e(e.getMessage());
                }
            }
        } else {
            EPLog.i("handlerThread NULL return !!");
        }
    }
}
