package com.epson.mobilephone.common.wifidirect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class ActivityiPrintConnectStart extends ActivityWiFiDirectBase {
    private static final int ID_ENABLED_LOCATION_SETTINGS = 1;
    public static final String TAG = "ActivityiPrintConnectStart";
    boolean bFirstScanResultAvailable = true;
    Button btnNext;
    escprLib mEscprLib = new escprLib();
    WifiManager mWifiManager;
    NetworkStateChangeReciever networkStateChangeReciever = null;
    escprLib.ConnectStrings settingsTempAp = new escprLib.ConnectStrings();

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Service.WIFI_SERVICE);
        ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(R.layout.iprintconnect_start, (ViewGroup) null);
        setContentView((View) viewGroup);
        setupCommonHeaderControl(true, true);
        btnNext = (Button) viewGroup.findViewById(R.id.btn_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ActivityiPrintConnectStart.this.startActivityForResult(new Intent(ActivityiPrintConnectStart.this, ActivityiPrintConnect.class), 10);
            }
        });
        mEscprLib.epsWrapperGetSetupConnectStrings(this.settingsTempAp);
        if (!ActivityRequestLocationPermission.canAccessWiFiInfo(this, 1)) {
            ActivityRequestLocationPermission.requestLocationPermissionForce(this, 1);
        } else {
            StartScan();
        }
    }

    private void StartScan() {
        if (WiFiUtils.getInstance(this).getCurSSID() != null) {
            startActivityForResult(new Intent(this, ActivityWiFiDirectError.class), 10);
            finish();
            return;
        }
        bFirstScanResultAvailable = true;
        mWifiManager.startScan();
    }

    protected void onResume() {
        super.onResume();
        networkStateChangeReciever = new NetworkStateChangeReciever();
        registerReceiver(this.networkStateChangeReciever, new IntentFilter("android.net.wifi.SCAN_RESULTS"));
        getWindow().addFlags(128);
    }

    protected void onPause() {
        super.onPause();
        NetworkStateChangeReciever networkStateChangeReciever2 = networkStateChangeReciever;
        if (networkStateChangeReciever2 != null) {
            unregisterReceiver(networkStateChangeReciever2);
            networkStateChangeReciever = null;
        }
        getWindow().clearFlags(128);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            if (i2 != -1) {
                onBackPressed();
            } else {
                StartScan();
            }
        }
    }

    private void onScanResultAvailable() {
        if (this.bFirstScanResultAvailable) {
            EPLog.w(TAG, "First onScanResultAvailable, ignore this");
            bFirstScanResultAvailable = false;
        } else {
            List<ScanResult> arrayList = new ArrayList<>();
            try {
                arrayList = mWifiManager.getScanResults();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
            if (arrayList.size() != 0) {
                for (ScanResult scanResult : arrayList) {
                    if (this.settingsTempAp.ssid.equals(WiFiUtils.removeQuotationsInSSID(scanResult.SSID))) {
                        EPLog.d(TAG, "Found TempAP");
                        btnNext.performClick();
                        return;
                    }
                }
            }
        }
        new WiFiScanner().startScan(mWifiManager);
    }

    class NetworkStateChangeReciever extends BroadcastReceiver {
        NetworkStateChangeReciever() {
        }

        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.net.wifi.SCAN_RESULTS")) {
                EPLog.d(ActivityiPrintConnectStart.TAG, "Scan Results Available");
                ActivityiPrintConnectStart.this.onScanResultAvailable();
            }
        }
    }
}
