package me.devsaki.hentoid;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.devsaki.hentoid.adapters.ContentAdapter;
import me.devsaki.hentoid.components.HentoidActivity;
import me.devsaki.hentoid.components.HentoidFragment;
import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.util.Constants;
import me.devsaki.hentoid.util.ConstantsPreferences;


public class DownloadsActivity extends HentoidActivity<DownloadsActivity.DownloadsFragment> {

    private static final String TAG = DownloadsActivity.class.getName();

    @Override
    protected DownloadsFragment buildFragment() {
        return new DownloadsFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_content_list, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView =
                (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                getFragment().setQuery(s.trim());
                getFragment().searchContent();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                getFragment().setQuery(s.trim());
                getFragment().searchContent();
                return true;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_order_alphabetic) {
            SharedPreferences.Editor editor = getSharedPreferences().edit();
            editor.putInt(ConstantsPreferences.PREF_ORDER_CONTENT_LISTS, ConstantsPreferences.PREF_ORDER_CONTENT_ALPHABETIC).apply();
            getFragment().searchContent();
            return true;
        } else if (id == R.id.action_order_by_date) {
            SharedPreferences.Editor editor = getSharedPreferences().edit();
            editor.putInt(ConstantsPreferences.PREF_ORDER_CONTENT_LISTS, ConstantsPreferences.PREF_ORDER_CONTENT_BY_DATE).apply();
            getFragment().searchContent();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public static class DownloadsFragment extends HentoidFragment {
        private static String query = "";
        Toast mToast;
        private int currentPage = 1;
        private int prevPage = 0;
        private int qtyPages;
        private int order;
        private Button btnPage;
        private List<Content> contents;

        public void setQuery(String query) {
            DownloadsFragment.query = query;
            currentPage = 1;
        }

        @SuppressLint("ShowToast")
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_downloads, container, false);


            qtyPages = Integer.parseInt(getSharedPreferences()
                    .getString(ConstantsPreferences.PREF_QUANTITY_PER_PAGE_LISTS,
                            ConstantsPreferences.PREF_QUANTITY_PER_PAGE_DEFAULT + ""));

            order = getSharedPreferences()
                    .getInt(ConstantsPreferences.PREF_ORDER_CONTENT_LISTS,
                            ConstantsPreferences.PREF_ORDER_CONTENT_BY_DATE);

            // Initialize toast if needed
            if (mToast == null) {
                mToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
            }

            btnPage = (Button) rootView.findViewById(R.id.btnPage);
            ImageButton btnRefresh = (ImageButton) rootView.findViewById(R.id.btnRefresh);
            ImageButton btnNext = (ImageButton) rootView.findViewById(R.id.btnNext);
            ImageButton btnPrevious = (ImageButton) rootView.findViewById(R.id.btnPrevious);

            btnRefresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    searchContent();
                }
            });

            btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (qtyPages <= 0) {
                        mToast.setText(R.string.not_limit_per_page);
                        mToast.show();
                    } else {
                        currentPage++;
                        if (!searchContent()) {
                            btnPage.setText("" + --currentPage);
                            mToast.setText(R.string.not_next_page);
                            mToast.show();
                        }
                    }
                }
            });

            btnPrevious.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentPage > 1) {
                        currentPage--;
                        searchContent();
                    } else if (qtyPages > 0) {
                        mToast.setText(R.string.not_previous_page);
                        mToast.show();
                    } else {
                        mToast.setText(R.string.not_limit_per_page);
                        mToast.show();
                    }
                }
            });

            String settingDir = getSharedPreferences().getString(Constants.SETTINGS_FOLDER, "");

            if (settingDir.isEmpty()) {
                Intent intent = new Intent(getActivity(), SelectFolderActivity.class);
                startActivity(intent);
                getActivity().finish();
            } else searchContent();

            return rootView;
        }

        private boolean searchContent() {
            if (qtyPages < 0) {
                qtyPages = ConstantsPreferences.PREF_QUANTITY_PER_PAGE_DEFAULT;
            }

            List<Content> result = getDB()
                    .selectContentByQuery(query, currentPage, qtyPages,
                            order == ConstantsPreferences.PREF_ORDER_CONTENT_ALPHABETIC);

            if (result != null && !result.isEmpty())
                contents = result;
            else if (contents == null)
                contents = new ArrayList<>(0);

            if (query.isEmpty()) {
                getActivity().setTitle(R.string.title_activity_downloads);
            } else {
                getActivity().setTitle(getResources()
                        .getString(R.string.title_activity_search)
                        .replace("@search", query));
            }

            if (contents == result || contents.isEmpty()) {
                ContentAdapter adapter = new ContentAdapter(getActivity(), contents);
                setListAdapter(adapter);
            }

            if (prevPage != currentPage) {
                btnPage.setText("" + currentPage);
            }
            prevPage = currentPage;

            return result != null && !result.isEmpty();
        }
    }
}

@@ -500,16 +500,68 @@
 wGroup;%0A
+import android.view.inputmethod.InputMethodManager;%0A
 import a
@@ -1959,24 +1959,25 @@
 ault(true);%0A
+%0A
         sear
@@ -2116,32 +2116,219 @@
 mit(String s) %7B%0A
+                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);%0A                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);%0A
                 

Fix for issue #39