package org.commcare.activities;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import org.commcare.activities.components.EntitySelectCalloutSetup;
import org.commcare.dalvik.R;
import org.commcare.suite.model.Callout;
import org.javarosa.core.services.locale.Localization;

import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;

/**
 * Manages case list activity's search state and UI
 *
 * @author Phillip Mates (pmates@dimagi.com)
 */
class EntitySelectSearchUI implements TextWatcher {
    private SearchView searchView;
    private MenuItem searchMenuItem;
    private MenuItem barcodeMenuItem;
    private EditText preHoneycombSearchBox;
    private TextView searchResultStatus;
    private ImageButton clearSearchButton;
    private View searchBanner;

    private final EntitySelectActivity activity;

    private String filterString = "";

    EntitySelectSearchUI(EntitySelectActivity activity) {
        this.activity = activity;
        initUIComponents();
    }

    private void initUIComponents() {
        searchBanner = activity.findViewById(R.id.search_result_banner);
        searchResultStatus = activity.findViewById(R.id.search_results_status);
        clearSearchButton = activity.findViewById(R.id.clear_search_button);
        clearSearchButton.setOnClickListener(v -> {
            activity.getAdapter().clearCalloutResponseData();
            activity.refreshView();
        });
        clearSearchButton.setVisibility(View.GONE);
    }

    protected CommCareActivity.ActionBarInstantiator getActionBarInstantiator() {
        // again, this should be unnecessary...
        return (searchItem, searchView, barcodeItem) -> {
            EntitySelectSearchUI.this.searchMenuItem = searchItem;
            EntitySelectSearchUI.this.searchView = searchView;
            EntitySelectSearchUI.this.barcodeMenuItem = barcodeItem;

            restoreLastQuery();

            EntitySelectSearchUI.this.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    activity.setLastQueryString(newText);
                    filterString = newText;
                    if (activity.getAdapter() != null) {
                        activity.getAdapter().filterByString(newText);
                    }
                    return false;
                }
            });
        };
    }

    public void setupActionImage(Callout callout) {
        if (callout != null && callout.getImage() != null) {
            // Replace the barcode scan callout with our custom callout
            EntitySelectCalloutSetup.setupImageLayout(activity, barcodeMenuItem, callout.getImage());
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void restoreLastQuery() {
        String lastQueryString = activity.getLastQueryString();
        if (lastQueryString != null && lastQueryString.length() > 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                MenuItemCompat.expandActionView(searchMenuItem);
            }
            filterString = lastQueryString;
            searchView.setQuery(lastQueryString, false);
            if (activity.getAdapter() != null) {
                activity.getAdapter().filterByString(lastQueryString);
            }
        }
    }

    @SuppressWarnings("NewApi")
    protected CharSequence getSearchText() {
        if (isUsingActionBar()) {
            return searchView.getQuery();
        } else {
            return preHoneycombSearchBox.getText();
        }
    }

    @SuppressWarnings("NewApi")
    protected void setSearchText(CharSequence text) {
        if (isUsingActionBar()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                MenuItemCompat.expandActionView(searchMenuItem);
            }
            searchView.setQuery(text, false);
        } else {
            preHoneycombSearchBox.setText(text);
        }
    }

    @Override
    public void afterTextChanged(Editable incomingEditable) {
        final String incomingString = incomingEditable.toString();
        final String currentSearchText = getSearchText().toString();
        if (incomingString.equals(currentSearchText)) {
            filterString = currentSearchText;
            if (activity.getAdapter() != null) {
                activity.getAdapter().filterByString(filterString);
            }
        }
        if (!isUsingActionBar()) {
            activity.setLastQueryString(filterString);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    /**
     * Checks if this activity uses the ActionBar
     */
    private boolean isUsingActionBar() {
        return searchView != null;
    }

    protected void restoreSearchString() {
        if (filterString != null && !"".equals(filterString)) {
            activity.getAdapter().filterByString(filterString);
        }
    }

    protected void setSearchBannerState() {
        if (!"".equals(activity.getAdapter().getSearchQuery())) {
            showSearchBanner();
            // Android's native SearchView has its own clear search button, so need to add our own
            clearSearchButton.setVisibility(View.GONE);
        } else if (activity.getAdapter().isFilteringByCalloutResult()) {
            showSearchBanner();
            clearSearchButton.setVisibility(View.VISIBLE);
        } else {
            searchBanner.setVisibility(View.GONE);
            clearSearchButton.setVisibility(View.GONE);
        }
    }

    private void showSearchBanner() {
        searchResultStatus.setText(activity.getAdapter().getSearchNotificationText());
        searchResultStatus.setVisibility(View.VISIBLE);
        searchBanner.setVisibility(View.VISIBLE);
    }

}

@@ -68,40 +68,8 @@
 pi;%0A
-import android.content.Context;%0A
 impo
@@ -89,16 +89,16 @@
 .Build;%0A
+
 import a
@@ -212,60 +212,8 @@
 ew;%0A
-import android.view.inputmethod.InputMethodManager;%0A
 impo
@@ -450,63 +450,8 @@
 out;
-%0Aimport org.javarosa.core.services.locale.Localization;
 %0A%0Aim
@@ -760,32 +760,32 @@
 searchMenuItem;%0A
+
     private Menu
@@ -810,52 +810,8 @@
 em;%0A
-    private EditText preHoneycombSearchBox;%0A
     
@@ -3828,36 +3828,25 @@
 );%0A        %7D
- else %7B%0A    
+%0A
         retu
@@ -3852,50 +3852,11 @@
 urn 
-preHoneycombSearchBox.getText();%0A        %7D
+%22%22;
 %0A   
@@ -4191,74 +4191,8 @@
 e);%0A
-        %7D else %7B%0A            preHoneycombSearchBox.setText(text);%0A
     

Remove all references to preHoneycombSearchBox