package co.epitre.aelf_lectures.bible;

import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.navigation.NavigationView;

import co.epitre.aelf_lectures.LecturesActivity;
import co.epitre.aelf_lectures.R;

public class BibleSearchFragment extends BibleFragment implements BibleSearchResultAdapter.ItemClickListener {
    /**
     * Global Views
     */
    protected ActionBar actionBar;
    protected NavigationView drawerView;
    protected LecturesActivity activity;

    /**
     * Results
     */
    private String mQuery = "";
    RecyclerView mRecyclerView;
    BibleSearchResultAdapter mResultAdapter;

    public static BibleSearchFragment newInstance(Uri uri) {
        String query = uri.getQueryParameter("query");
        return new BibleSearchFragment(query);
    }

    private BibleSearchFragment(String query) {
        mQuery = query;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // Load global views
        activity = (LecturesActivity) getActivity();
        actionBar = activity.getSupportActionBar();
        drawerView = activity.findViewById(R.id.drawer_navigation_view);

        // Option menu
        setHasOptionsMenu(true);

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_section_bible_search, container, false);

        // Set section title
        actionBar.setTitle(getTitle());

        // Perform the search
        Cursor cursor = BibleSearchEngine.getInstance().search(mQuery);

        // Set up the RecyclerView
        mRecyclerView = view.findViewById(R.id.search_results);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mResultAdapter = new BibleSearchResultAdapter(getContext(), cursor, mQuery);
        mResultAdapter.setClickListener(this);
        mRecyclerView.setAdapter(mResultAdapter);
        updateListBottomMargin();

        return view;
    }

    @Override
    public String getRoute() {
        return "/search";
    }

    @Override
    public String getTitle() {
        return "Recherche « "+mQuery+" »";
    }

    private void updateListBottomMargin() {
        // Inject margin at the bottom to account for the navigation bar
        Resources resources = getContext().getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int navigationBarHeight = 0;
        if (resourceId > 0) {
            navigationBarHeight = (int)(resources.getDimension(resourceId) / getResources().getDisplayMetrics().density);
        }

        // Update the layout params
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mRecyclerView.getLayoutParams();
        params.bottomMargin = navigationBarHeight;
        mRecyclerView.setLayoutParams(params);
    }

    //
    // Events
    //

    @Override
    public void onItemClick(String link) {
        LecturesActivity mainActivity;
        try {
            mainActivity = (LecturesActivity) getActivity();
        } catch (ClassCastException e) {
            return;
        }

        mainActivity.onLectureLink(Uri.parse(link));
    }
}

@@ -185,24 +185,114 @@
 utInflater;%0A
+import android.view.Menu;%0Aimport android.view.MenuInflater;%0Aimport android.view.MenuItem;%0A
 import andro
@@ -414,16 +414,61 @@
 ionBar;%0A
+import androidx.appcompat.widget.SearchView;%0A
 import a
@@ -825,16 +825,123 @@
 tener %7B%0A
+    /**%0A     * Internal%0A     */%0A%0A    private final static String BIBLE_SEARCH_QUERY = %22bibleSearchQuery%22;%0A%0A
     /**%0A
@@ -1300,63 +1300,75 @@
     
-String query = uri.getQueryParameter(%22query%22
+BibleSearchFragment fragment = new BibleSearchFragment(
 );%0A
+%0A
         
 retu
@@ -1367,124 +1367,173 @@
     
-return new BibleSearchF
+Bundle args = new Bundle();%0A        args.putString(BIBLE_SEARCH_QUERY, uri.getQueryPa
 ra
-g
 me
-nt(
+ter(%22
 query
+%22)
 );%0A    
-%7D%0A%0A
     
-private BibleSearchFragment(String query) %7B%0A        mQuery = query
+fragment.setArguments(args);%0A%0A        return fragment
 ;%0A  
@@ -2201,24 +2201,113 @@
 tTitle());%0A%0A
+        // Get the query%0A        mQuery = getArguments().getString(BIBLE_SEARCH_QUERY);%0A%0A
         // P
@@ -2812,32 +2812,545 @@
 rn view;%0A    %7D%0A%0A
+    @Override%0A    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) %7B%0A        super.onCreateOptionsMenu(menu, inflater);%0A%0A        // Expand the search box%0A        MenuItem searchMenuItem = menu.findItem(R.id.action_search);%0A        searchMenuItem.expandActionView();%0A%0A        // Initialize the search box with the query%0A        SearchView searchView = (SearchView) searchMenuItem.getActionView();%0A        searchView.setIconifiedByDefault(false);%0A        searchView.setQuery(mQuery, false);%0A    %7D%0A%0A
     @Override%0A  
@@ -3485,23 +3485,8 @@
 rche
- %C2%AB %22+mQuery+%22 %C2%BB
 %22;%0A 

initialize search result box with search query