package com.example.calendaryapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Arrays;

public class Prebiblio extends AppCompatActivity {
    ListView search_libros;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prebiblio);


        search_libros = (ListView) findViewById(R.id.search_libros);

        search_libros.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(Prebiblio.this, BiblioActivity.class);
                intent.putExtra("Nombre_Libro",search_libros.getItemAtPosition(i).toString());
                startActivity(intent);
            }
        });

        ArrayList<String> arrayLibros = new ArrayList<>();
        arrayLibros.addAll(Arrays.asList(getResources().getStringArray(R.array.libros)));

        adapter = new ArrayAdapter<String>(
                Prebiblio.this,
                android.R.layout.simple_list_item_1,
                arrayLibros
        );

        search_libros.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu, menu);
        MenuItem item = menu.findItem(R.id.search_libros);
        SearchView searchView = (SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String s){
                return false;
            }
            @Override
            public boolean onQueryTextChange(String s){
                adapter.getFilter().filter(s);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}
