package by.vshkl.translate2.util;

import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import by.vshkl.translate2.R;
import by.vshkl.translate2.database.local.LocalRepository;
import by.vshkl.translate2.ui.StopBookmarkListener;
import permissions.dispatcher.PermissionRequest;

public class DialogUtils {

    public static void showLocationRationaleDialog(Context context, PermissionRequest request) {
        new MaterialDialog.Builder(context)
                .title(R.string.map_permission_rationale_title)
                .content(R.string.map_permission_rationale_message)
                .positiveText(R.string.map_location_ok)
                .negativeText(R.string.map_location_cancel)
                .onPositive((dialog, which) -> request.proceed())
                .onNegative(((dialog, which) -> request.cancel()))
                .show();
    }

    public static void showLocationTurnOnDialog(Context context) {
        new MaterialDialog.Builder(context)
                .title(R.string.map_permission_rationale_title)
                .content(R.string.map_location_message)
                .positiveText(R.string.map_location_ok)
                .negativeText(R.string.map_location_cancel)
                .onPositive((dialog, which) -> Navigation.navigateToLocationSettings(context))
                .show();
    }

    public static void showBookmarkActionsDialog(Context context, StopBookmarkListener listener) {
        new MaterialDialog.Builder(context)
                .items(R.array.bookmarks_dialog_actions)
                .itemsColorRes(R.color.colorPrimaryText)
                .itemsCallback((dialog, itemView, position, text) -> {
                    if (position == 0) {
                        listener.onEditBookmark();
                    } else if (position == 1) {
                        listener.onDeleteBookmark();
                    }
                })
                .show();
    }

    public static void showBookmarkDeleteConfirmationDialog(Context context, StopBookmarkListener listener) {
        new MaterialDialog.Builder(context)
                .title(R.string.bookmark_delete_message)
                .positiveText(R.string.bookmark_delete_ok)
                .negativeText(R.string.bookmark_delete_cancel)
                .onPositive((dialog, which) -> listener.onDeleteConfirmed())
                .show();
    }

    public static void showBookmarksDeleteConfirmationDialog(Activity activity) {
        new MaterialDialog.Builder(activity)
                .title(R.string.bookmarks_delete_title)
                .content(R.string.bookmarks_delete_message)
                .positiveText(R.string.bookmark_delete_ok)
                .negativeText(R.string.bookmark_delete_cancel)
                .onPositive((dialog, which) -> LocalRepository.removeStopBookmarks()
                        .compose(RxUtils.applySchedulers())
                        .subscribe(aBoolean -> {
                            if (!aBoolean) {
                                Toast.makeText(activity, activity.getString(R.string.bookmarks_delete_success),
                                        Toast.LENGTH_SHORT).show();
                                Navigation.navigateToMap(activity.getBaseContext());
                            }
                        }))
                .show();
    }

    public static void showBookmarkRenameDialog(Context context, String stopName, StopBookmarkListener listener) {
        new MaterialDialog.Builder(context)
                .title(R.string.bookmark_dialog_rename_title)
                .positiveText(R.string.bookmark_dialog_rename_ok)
                .negativeText(R.string.bookmark_dialog_rename_cancel)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input(context.getString(R.string.bookmark_dialog_rename_hint), stopName, false,
                        (dialog, input) -> {
                        })
                .onPositive(
                        (dialog, which) -> listener.onRenameConfirmed(dialog.getInputEditText().getText().toString()))
                .show();
    }

    public static void showLogoutConfirmationDialog(Activity activity) {
        new MaterialDialog.Builder(activity)
                .title(R.string.logout_title)
                .positiveText(R.string.logout_confirm)
                .negativeText(R.string.logout_decline)
                .onPositive((dialog, which) -> {
                    CookieUtils.deleteCookies(activity.getBaseContext());
                    Navigation.restartApp(activity.getBaseContext());
                })
                .show();
    }
}

@@ -108,22 +108,84 @@
 oid.
-text.InputType
+os.Build;%0Aimport android.text.InputType;%0Aimport android.webkit.CookieManager
 ;%0Aim
@@ -4658,32 +4658,372 @@
 BaseContext());%0A
+                    if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.LOLLIPOP) %7B%0A                        CookieManager.getInstance().removeAllCookies(null);%0A                        CookieManager.getInstance().flush();%0A                    %7D else %7B%0A                        CookieManager.getInstance().removeAllCookie();%0A                    %7D%0A
                 

Fix webkit cookies removing