/*
 * WebViewActivity.java
 *
 * Copyright(c) 2016 Auth0 (http://auth0.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.auth0.android.lock.provider;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.auth0.android.lock.R;

public class WebViewActivity extends AppCompatActivity {

    private static final String TAG = WebViewActivity.class.getSimpleName();
    private static final String KEY_REDIRECT_URI = "redirect_uri";
    public static final String CONNECTION_NAME_EXTRA = "serviceName";

    private WebView webView;
    private ProgressBar progressBar;
    private View errorView;
    private TextView errorMessage;
    private boolean isShowingError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.com_auth0_lock_activity_web_view);
        final ActionBar bar = getSupportActionBar();
        if (bar != null) {
            String serviceName = getIntent().getStringExtra(CONNECTION_NAME_EXTRA);
            bar.setIcon(android.R.color.transparent);
            bar.setDisplayShowTitleEnabled(false);
            bar.setDisplayUseLogoEnabled(false);
            bar.setDisplayHomeAsUpEnabled(false);
            bar.setDisplayShowCustomEnabled(true);
            bar.setTitle(serviceName);
        }
        webView = (WebView) findViewById(R.id.com_auth0_lock_webview);
        webView.setVisibility(View.INVISIBLE);
        progressBar = (ProgressBar) findViewById(R.id.com_auth0_lock_progressbar);
        progressBar.setIndeterminate(true);
        progressBar.setMax(100);
        errorView = findViewById(R.id.com_auth0_lock_error_view);
        errorMessage = (TextView) findViewById(R.id.com_auth0_lock_text);
        Button retryButton = (Button) findViewById(R.id.com_auth0_lock_retry);
        retryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isShowingError = false;
                errorView.setVisibility(View.GONE);
                startUrlLoading();
            }
        });

        startUrlLoading();
    }

    private void startUrlLoading() {
        if (!isNetworkAvailable()) {
            renderLoadError(getString(R.string.com_auth0_lock_network_error));
            return;
        }

        final Intent intent = getIntent();
        final Uri uri = intent.getData();
        final String redirectUrl = uri.getQueryParameter(KEY_REDIRECT_URI);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress > 10) {
                    progressBar.setIndeterminate(false);
                    progressBar.setProgress(newProgress);
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith(redirectUrl)) {
                    final Intent intent = new Intent();
                    intent.setData(Uri.parse(url));
                    setResult(RESULT_OK, intent);
                    finish();
                    return true;
                }
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setProgress(0);
                progressBar.setIndeterminate(true);
                progressBar.setVisibility(View.GONE);
                webView.setVisibility(isShowingError ? View.INVISIBLE : View.VISIBLE);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setProgress(0);
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.VISIBLE);
            }

            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                renderLoadError(description);
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                renderLoadError(error.getDescription().toString());
                super.onReceivedError(view, request, error);
            }

        });
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(uri.toString());
    }

    private void renderLoadError(String description) {
        isShowingError = true;
        errorMessage.setText(description);
        webView.setVisibility(View.INVISIBLE);
        errorView.setVisibility(View.VISIBLE);
    }

    private boolean isNetworkAvailable() {
        boolean available = true;
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            available = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
        } catch (SecurityException e) {
            Log.w(TAG, "Could not check for Network status. Please, be sure to include the android.permission.ACCESS_NETWORK_STATE permission in the manifest");
        }
        return available;
    }
}
@@ -2337,44 +2337,8 @@
 age;
-%0A    private boolean isShowingError;
 %0A%0A  
@@ -3321,32 +3321,76 @@
 ck_error_view);%0A
+        errorView.setVisibility(View.GONE);%0A
         errorMes
@@ -3662,48 +3662,8 @@
 ) %7B%0A
-                isShowingError = false;%0A
     
@@ -4401,17 +4401,16 @@
 gress %3E 
-1
 0) %7B%0A   
@@ -5337,32 +5337,122 @@
 ity(View.GONE);%0A
+                final boolean isShowingError = errorView.getVisibility() == View.VISIBLE;%0A
                 
@@ -5746,61 +5746,8 @@
 0);%0A
-                progressBar.setIndeterminate(false);%0A
     
@@ -6675,32 +6675,32 @@
 ring());%0A    %7D%0A%0A
+
     private void
@@ -6742,39 +6742,8 @@
 ) %7B%0A
-        isShowingError = true;%0A
     

remove unnecesary field declaration. fix progressbar issue