package me.thekey.android.lib.support.v4.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.webkit.WebView;
import android.widget.FrameLayout;

import me.thekey.android.lib.Builder;
import me.thekey.android.lib.R;
import me.thekey.android.lib.dialog.LoginDialogListener;
import me.thekey.android.lib.dialog.LoginDialogWebViewClient;
import me.thekey.android.lib.support.v4.fragment.FragmentBuilder;
import me.thekey.android.lib.util.DisplayUtil;

public class LoginDialogFragment extends DialogFragment implements me.thekey.android.lib.fragment.DialogFragment {
    // login WebView
    private FrameLayout frame = null;
    private WebView loginView = null;

    public static Builder<LoginDialogFragment> builder() {
        return new FragmentBuilder<>(LoginDialogFragment.class);
    }

    /** BEGIN lifecycle */

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRetainInstance(true);
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());

        // build dialog
        final FrameLayout frame =
                (FrameLayout) LayoutInflater.from(this.getActivity()).inflate(R.layout.thekey_login, null);
        this.attachLoginView(frame);
        builder.setView(frame);

        return builder.create();
    }

    @Override
    public void onDestroyView() {
        // Work around bug:
        // http://code.google.com/p/android/issues/detail?id=17423
        final Dialog dialog = this.getDialog();
        if ((dialog != null) && this.getRetainInstance()) {
            dialog.setDismissMessage(null);
        }

        super.onDestroyView();
    }

    /** END lifecycle */

    private void attachLoginView(final FrameLayout frame) {
        this.detachLoginView();

        // create a loginView if it doesn't exist already
        if (this.loginView == null) {
            final Bundle args = getArguments();
            this.loginView =
                    DisplayUtil.createLoginWebView(getActivity(), new LoginDialogWebViewClient(this, args), args);
        }

        // attach the login view to the current frame
        this.frame = frame;
        this.frame.addView(this.loginView);
    }

    private void detachLoginView() {
        // remove the login view from any existing frame
        if (this.frame != null) {
            this.frame.removeView(this.loginView);
            this.frame = null;
        }
    }

    public interface Listener extends LoginDialogListener<LoginDialogFragment> {
    }
}

@@ -277,16 +277,74 @@
 ayout;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import m
@@ -753,16 +753,107 @@
 gment %7B%0A
+    private static final Logger LOG = LoggerFactory.getLogger(LoginDialogFragment.class);%0A%0A
     // l
@@ -923,17 +923,18 @@
 WebView 
-l
+mL
 oginView
@@ -1080,17 +1080,16 @@
 %0A%0A    /*
-*
  BEGIN l
@@ -2062,17 +2062,16 @@
 %0A%0A    /*
-*
  END lif
@@ -2198,21 +2198,25 @@
 e a 
-l
+L
 ogin
+ Web
 View if 
 it d
@@ -2211,18 +2211,19 @@
 View if 
-it
+one
  doesn't
@@ -2249,22 +2249,18 @@
     if (
-this.l
+mL
 oginView
@@ -2331,22 +2331,18 @@
         
-this.l
+mL
 oginView
@@ -2347,28 +2347,8 @@
 ew =
-%0A                   
  Dis
@@ -2555,30 +2555,26 @@
 ame.addView(
-this.l
+mL
 oginView);%0A 
@@ -2724,45 +2724,379 @@
    t
-his.frame.removeView(this.loginView);
+ry %7B%0A                this.frame.removeView(mLoginView);%0A            %7D catch (final IllegalArgumentException e) %7B%0A                // XXX: KEYAND-12 IllegalArgumentException: Receiver not registered: android.webkit.WebViewClassic%0A                LOG.error(%22error removing Login WebView, let's just reset the login view%22, e);%0A                mLoginView = null;%0A            %7D
 %0A   

reset login WebView when there is an exception thrown trying to reuse it