package com.epson.memcardacc;


import android.os.AsyncTask;
import android.os.Bundle;

import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import java.util.Deque;

import epson.common.DialogProgress;
import epson.common.DialogProgressViewModel;
import epson.print.ActivityIACommon;

public abstract class MemcardTopSuper extends ActivityIACommon implements PasswordDialogFragment.Callback, LocalAlertDialogFragment.DialogCallback {
    protected static final int DIALOG_AUTHENTICATION = 50;
    protected static final int DIALOG_COMMUNICATION_ERROR = 2;
    protected static final int DIALOG_MEMCARD_NOT_FOUND = 1;
    protected static final int DIALOG_NO_DIALOG = 0;
    protected static final int DIALOG_PRINTER_BUSY = 3;
    protected static final String DIALOG_PROGRESS = "dialog_progress";
    protected static final String FRAGMENT_TAG_ERROR_DIALOG = "error_dialog";
    protected static final String FRAGMENT_TAG_PASSWORD_DIALOG = "password_dialog";
    protected static final String FRAGMENT_TAG_PROGRESS = "progress_dialog";
    protected static final int LAUNCH_TYPE_NOT_SET = 0;
    protected static final int LAUNCH_TYPE_READER = 2;
    protected static final int LAUNCH_TYPE_WRITER = 1;
    boolean isTryConnectSimpleAp = false;
    protected boolean mActivityIsFinishing = false;
    protected MemcardBitmapCache mBitmapCache;
    protected boolean mIsActivityForeground;
    protected int mLaunchType;
    protected MemcardCheckTask mMemcardCheckTask;
    private DialogProgressViewModel mModelDialog;
    private PasswordDialogFragment mPasswordDialogFragment;
    protected int mReservationDialog;

    protected abstract int getStorageSetType();

    public abstract void launchReaderActivity(int i);

    public abstract void launchWriterActivity();

    public void onDialogCallback(int i) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mModelDialog = (DialogProgressViewModel) ViewModelProviders.m2of((FragmentActivity) this).get(DialogProgressViewModel.class);
        mModelDialog.getDialogJob().observe(this, new Observer() {
            public final void onChanged(Object obj) {
                MemcardTopSuper.lambda$onCreate$0(MemcardTopSuper.this, (Deque) obj);
            }
        });
        mReservationDialog = 0;
    }

    public static /* synthetic */ void lambda$onCreate$0(MemcardTopSuper memcardTopSuper, Deque deque) {
        String[] checkQueue = memcardTopSuper.mModelDialog.checkQueue();
        if (checkQueue != null) {
            String str = checkQueue[0];
            String str2 = checkQueue[1];
            if (str2.equals("do_show")) {
                memcardTopSuper.showDialog(str);
            }
            if (str2.equals("do_dismiss")) {
                memcardTopSuper.dismissDialog(str);
            }
        }
    }

    protected void onResume() {
        super.onResume();
        mIsActivityForeground = true;
        if (!showDialogOnResume() && mLaunchType != 0) {
            execMemcardCheck();
            isTryConnectSimpleAp = false;
        }
    }

    private boolean showDialogOnResume() {
        int i = mReservationDialog;
        mReservationDialog = 0;
        if (i == 0) {
            return false;
        }
        if (i != 50) {
            localShowDialog(i);
            return false;
        }
        showPasswordDialog(true);
        return true;
    }

    protected void onPause() {
        mIsActivityForeground = false;
        if (isFinishing()) {
            mActivityIsFinishing = true;
            CifsAccess.clearSmbAuthInfo();
            releaseResource();
        }
        super.onPause();
    }

    private void disconnectWifiDirect() {
        WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
    }

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

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

    protected void startMemcardStorageCheck() {
        if (!chechAndConnectWiFiDirect()) {
            execMemcardCheck();
        }
    }

    protected boolean chechAndConnectWiFiDirect() {
        if (!WiFiDirectManager.isNeedConnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER)) {
            isTryConnectSimpleAp = false;
        } else if (!this.isTryConnectSimpleAp) {
            isTryConnectSimpleAp = true;
            if (WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, -1)) {
                return true;
            }
        }
        return false;
    }

    protected void execMemcardCheck() {
        MemcardCheckTask memcardCheckTask = mMemcardCheckTask;
        if (memcardCheckTask == null || memcardCheckTask.getStatus() == AsyncTask.Status.FINISHED) {
            mMemcardCheckTask = new MemcardCheckTask(this, new MemcardCheckTask.MemcardCheckCallback() {
                public void onAuthInfoRequired(MemcardCheckTask memcardCheckTask, boolean z) {
                    MemcardTopSuper.this.authInfoRequested(memcardCheckTask, z);
                }

                public void onPreExecute() {
                    MemcardTopSuper.this.onPreMemcardChek();
                }

                public void onMemcardCheckEnd(Integer num, int i) {
                    MemcardTopSuper.this.onMemcardCheckEnd(num, i);
                }
            }, getStorageSetType());
            mMemcardCheckTask.execute(new Void[0]);
        }
    }

    private void onPreMemcardChek() {
        mModelDialog.doShow(DIALOG_PROGRESS);
    }

    public void onPositiveClicked(String str, String str2) {
        MemcardCheckTask memcardCheckTask = mMemcardCheckTask;
        if (memcardCheckTask != null) {
            memcardCheckTask.setAuthData(str, str2);
        }
        mModelDialog.doShow(DIALOG_PROGRESS);
    }

    public void onNegativeClicked() {
        mLaunchType = 0;
        MemcardCheckTask memcardCheckTask = mMemcardCheckTask;
        if (memcardCheckTask != null) {
            memcardCheckTask.cancel(true);
        }
        mModelDialog.doShow(DIALOG_PROGRESS);
    }

    private void showPasswordDialog(boolean z) {
        if (!mActivityIsFinishing) {
            PasswordDialogFragment passwordDialogFragment = mPasswordDialogFragment;
            if (passwordDialogFragment != null) {
                passwordDialogFragment.dismissAllowingStateLoss();
            }
            mModelDialog.doDismiss(DIALOG_PROGRESS);
            mPasswordDialogFragment = PasswordDialogFragment.newInstance(z);
            mPasswordDialogFragment.show(getSupportFragmentManager(), FRAGMENT_TAG_PASSWORD_DIALOG);
        }
    }

    protected void authInfoRequested(final MemcardCheckTask memcardCheckTask, final boolean z) {
        if (!mIsActivityForeground) {
            mReservationDialog = 50;
        } else {
            runOnUiThread(new Runnable() {
                public void run() {
                    MemcardCheckTask memcardCheckTask = memcardCheckTask;
                    if (memcardCheckTask != null && !memcardCheckTask.isCancelled()) {
                        MemcardTopSuper.this.showPasswordDialog(z);
                    }
                }
            });
        }
    }

    private void releaseResource() {
        MemcardBitmapCache memcardBitmapCache = mBitmapCache;
        if (memcardBitmapCache != null) {
            memcardBitmapCache.clearCache();
        }
    }

    private void localShowDialog(int i) {
        if (!mActivityIsFinishing) {
            if (!mIsActivityForeground) {
                mReservationDialog = i;
                return;
            }
            LocalAlertDialogFragment localAlertDialogFragment = null;
            switch (i) {
                case 1:
                    localAlertDialogFragment = LocalAlertDialogFragment.newInstance(R.string.memcard_media_not_found_message, R.string.memcard_media_not_found_title, 1);
                    break;
                case 2:
                    localAlertDialogFragment = LocalAlertDialogFragment.newInstance(R.string.memcard_comm_error_message, R.string.memcard_comm_error_title, 2);
                    break;
                case 3:
                    localAlertDialogFragment = LocalAlertDialogFragment.newInstance(R.string.EPS_PRNST_BUSY_MSG, R.string.EPS_PRNST_BUSY_TITLE, 2);
                    break;
            }
            if (localAlertDialogFragment != null) {
                localAlertDialogFragment.show(getSupportFragmentManager(), FRAGMENT_TAG_ERROR_DIALOG);
            }
        }
    }

    private void onMemcardCheckEnd(Integer num, int i) {
        mMemcardCheckTask = null;
        mPasswordDialogFragment = null;
        if (num == null || num.intValue() == 0) {
            WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
            mLaunchType = 0;
            mModelDialog.doDismiss(DIALOG_PROGRESS);
            if (i == 1) {
                localShowDialog(1);
            } else if (i != 3) {
                switch (i) {
                    case 5:
                    case 6:
                        return;
                    default:
                        localShowDialog(2);
                        return;
                }
            } else {
                localShowDialog(3);
            }
        } else {
            mModelDialog.doDismiss(DIALOG_PROGRESS);
            launchActivity(num.intValue());
        }
    }

    public void launchActivity(int i) {
        switch (mLaunchType) {
            case 1:
                launchWriterActivity();
                WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
                break;
            case 2:
                launchReaderActivity(i);
                break;
        }
        mLaunchType = 0;
    }

    private void showDialog(String str) {
        DialogProgress newInstance = DialogProgress.newInstance(str, 0, getString(R.string.memcard_connecting_printer), (String) null, (String) null, (String) null, (String) null);
        newInstance.setCancelable(false);
        newInstance.show(getSupportFragmentManager(), str);
    }

    private void dismissDialog(String str) {
        DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(str);
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }
}
