package com.tasomaniac.openwith.preferred;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.Html;

import com.tasomaniac.openwith.R;
import com.tasomaniac.openwith.resolver.DisplayResolveInfo;

public class AppRemoveDialogFragment extends AppCompatDialogFragment {

    private static final String EXTRA_INFO = "EXTRA_INFO";

    static AppRemoveDialogFragment newInstance(DisplayResolveInfo info) {
        Bundle args = new Bundle();
        args.putParcelable(EXTRA_INFO, info);
        AppRemoveDialogFragment fragment = new AppRemoveDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private Callbacks callbacks = Callbacks.EMPTY;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Callbacks) {
            callbacks = (Callbacks) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        callbacks = Callbacks.EMPTY;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final DisplayResolveInfo info = getArguments().getParcelable(EXTRA_INFO);
        if (info == null) {
            throw new IllegalArgumentException("Use newInstance method to create the Dialog");
        }

        final String message = getString(
                R.string.message_remove_preferred,
                info.displayLabel(),
                info.extendedInfo(),
                info.extendedInfo()
        );

        //noinspection deprecation
        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.title_remove_preferred)
                .setMessage(Html.fromHtml(message))
                .setPositiveButton(android.R.string.ok, (dialog, which) -> callbacks.onAppRemoved(info))
                .setNegativeButton(android.R.string.cancel, null)
                .show();
    }

    interface Callbacks {
        void onAppRemoved(DisplayResolveInfo info);

        Callbacks EMPTY = info -> {
        };
    }
}

@@ -96,16 +96,54 @@
 ontext;%0A
+import android.content.res.Resources;%0A
 import a
@@ -625,24 +625,98 @@
 nfo info) %7B%0A
+        AppRemoveDialogFragment fragment = new AppRemoveDialogFragment();%0A
         Bund
@@ -789,82 +789,8 @@
 o);%0A
-        AppRemoveDialogFragment fragment = new AppRemoveDialogFragment();%0A
     
@@ -885,34 +885,16 @@
 allbacks
- = Callbacks.EMPTY
 ;%0A%0A    @
@@ -983,56 +983,8 @@
 t);%0A
-        if (context instanceof Callbacks) %7B%0A    
     
@@ -1020,26 +1020,16 @@
 ontext;%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1082,61 +1082,50 @@
     
-super.onDetach();%0A        callbacks = Callbacks.EMPTY
+callbacks = null;%0A        super.onDetach()
 ;%0A  
@@ -1214,24 +1214,547 @@
 nceState) %7B%0A
+        DisplayResolveInfo info = getInfo();%0A        CharSequence message = appRemoveDialogContentFrom(getResources(), info);%0A%0A        return new AlertDialog.Builder(getActivity())%0A                .setTitle(R.string.title_remove_preferred)%0A                .setMessage(message)%0A                .setPositiveButton(android.R.string.ok, (dialog, which) -%3E callbacks.onAppRemoved(info))%0A                .setNegativeButton(android.R.string.cancel, null)%0A                .show();%0A    %7D%0A%0A    private DisplayResolveInfo getInfo() %7B%0A
         fina
@@ -1956,25 +1956,24 @@
       %7D%0A
-%0A
         
 final St
@@ -1968,31 +1968,170 @@
     
-final String message = 
+return info;%0A    %7D%0A%0A    private static CharSequence appRemoveDialogContentFrom(Resources resources, DisplayResolveInfo info) %7B%0A        String content = resources.
 getS
@@ -2309,17 +2309,16 @@
      );%0A
-%0A
         
@@ -2363,351 +2363,29 @@
 urn 
-new AlertDialog.Builder(getActivity())%0A                .setTitle(R.string.title_remove_preferred)%0A                .setMessage(Html.fromHtml(message))%0A                .setPositiveButton(android.R.string.ok, (dialog, which) -%3E callbacks.onAppRemoved(info))%0A                .setNegativeButton(android.R.string.cancel, null)%0A                .show(
+Html.fromHtml(content
 );%0A 
@@ -2471,56 +2471,8 @@
 fo);
-%0A%0A        Callbacks EMPTY = info -%3E %7B%0A        %7D;
 %0A   

Simplify AppRemoveDialogFragment