package com.epson.mobilephone.common.wifidirect;



import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;




import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Toast;

import epson.common.DialogProgress;
import epson.common.DialogProgressViewModel;

import java.util.Deque;

public class ActivityWiFiDirectManual extends ActivityWiFiDirectBase {
    private static final String IDD_WIFI_WAITING = "idd_wifi_waiting";
    private static final int ID_FOUND = 1;
    private static final int ID_NOT_FOUND = 2;
    private static final int ID_REQUEST_PERMISSION = 1;
    private static final int ID_WIFI_SETTINGS = 0;
    private static final String PARAM_NO_LCD = "NO_LCD";
    private static final int TIMEOUT_CONNECTING = 30;
    Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            switch (message.what) {
                case 1:
                    Bundle data = message.getData();
                    if (data != null) {
                        String string = data.getString("name");
                        String string2 = data.getString(escprLib.PRINTER_IP);
                        String string3 = data.getString("id");
                        if (string != null && string2 != null && string3 != null) {
                            ActivityWiFiDirectManual.this.interruptFindingPrinter();
                            Intent intent = new Intent();
                            intent.putExtras(data);
                            ActivityWiFiDirectManual.this.setResult(-1, intent);
                            ActivityWiFiDirectManual.this.finish();
                            return;
                        }
                        return;
                    }
                    return;
                case 2:
                    ActivityWiFiDirectManual.mModelDialog.doDismiss(ActivityWiFiDirectManual.IDD_WIFI_WAITING);
                    ActivityWiFiDirectManual activityWiFiDirectManual = ActivityWiFiDirectManual.this;
                    Toast.makeText(activityWiFiDirectManual, activityWiFiDirectManual.getString(R.string.str_error_connecting_printer_short), 1).show();
                    return;
                default:
                    return;
            }
        }
    };
    private DialogProgressViewModel mModelDialog;
    WebView mNoLcdGuidanceWebView;
    WifiManager mWifiManager = null;
    ViewGroup mWithLcdGuidanceView;
    FindPrinterTask taskFindPrinter = null;
    View viewWiFiSettings;

    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) {
                ActivityWiFiDirectManual.lambda$onCreate$0(ActivityWiFiDirectManual.this, (Deque) obj);
            }
        });
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Service.WIFI_SERVICE);
        ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.wifidirect_manual, (ViewGroup) null);
        setContentView((View) viewGroup);
        setupCommonHeaderControl(true, true);
        viewWiFiSettings = viewGroup.findViewById(R.id.rlWifiSettings);
        viewWiFiSettings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(ActivityWiFiDirectManual.this, ActivityControlWiFi.class);
                intent.putExtra(ActivityControlWiFi.SHOWPROGRESS, true);
                intent.putExtra(ActivityControlWiFi.SHOWWIFISETTINGS, true);
                ActivityWiFiDirectManual.this.startActivityForResult(intent, 0);
            }
        });
        mNoLcdGuidanceWebView = (WebView) findViewById(R.id.guidance_webview);
        mWithLcdGuidanceView = (ViewGroup) findViewById(R.id.guidance_with_lcd);
        updateGuidance(getNoLcdValueFromIntent(getIntent()));
        if (!ActivityRequestLocationPermission.canAccessWiFiInfo(this, 1)) {
            ActivityRequestLocationPermission.requestLocationPermissionForce(this, 1);
        }
    }

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

    private void updateGuidance(boolean z) {
        if (z) {
            mWithLcdGuidanceView.setVisibility(View.GONE);
            mNoLcdGuidanceWebView.setVisibility(View.VISIBLE);
            String string = getString(R.string.path_wifidirect_guide_panelless);
            if (string == null) {
                finish();
            } else {
                mNoLcdGuidanceWebView.loadUrl(string);
            }
        } else {
            mNoLcdGuidanceWebView.setVisibility(View.GONE);
            mWithLcdGuidanceView.setVisibility(View.VISIBLE);
        }
    }

    protected void onPause() {
        super.onPause();
        interruptFindingPrinter();
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        switch (i) {
            case 0:
                if (WiFiDirectManager.isSimpleAP(this)) {
                    ManageDefaultNetwork.getInstance(this).setDefaultNetworkSimpleAp();
                    mModelDialog.doShow(IDD_WIFI_WAITING);
                    taskFindPrinter = new FindPrinterTask(mHandler, 30, 1, 2);
                    taskFindPrinter.execute(new Void[0]);
                    return;
                }
                return;
            case 1:
                if (i2 != -1) {
                    onBackPressed();
                    return;
                }
                return;
            default:
                return;
        }
    }

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

    private static boolean getNoLcdValueFromIntent(@Nullable Intent intent) {
        if (intent == null) {
            return false;
        }
        return intent.getBooleanExtra(PARAM_NO_LCD, false);
    }

    public static Intent getStartIntent(@NonNull Context context, boolean z) {
        Intent intent = new Intent(context, ActivityWiFiDirectManual.class);
        intent.putExtra(PARAM_NO_LCD, z);
        return intent;
    }

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

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