content: /*
   * Copyright (C) 2016 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.example.android.pets;
  
  import android.content.ContentValues;
  import android.content.Intent;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.example.android.pets.Data.PetContract;
  import com.example.android.pets.Data.PetContract.PetEntry;
  import com.example.android.pets.Data.PetDbHelper;
  import com.google.android.material.floatingactionbutton.FloatingActionButton;
  
  /**
   * Displays list of pets that were entered and stored in the app.
   */
  public class CatalogActivity extends AppCompatActivity {
  
      private PetDbHelper mDbHelper;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_catalog);
  
          // Setup FAB to open EditorActivity
          FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
                  startActivity(intent);
              }
          });
  
          mDbHelper = new PetDbHelper(this);
  
      }
  
      /**
       * Temporary helper method to display information in the onscreen TextView about the state of
       * the pets database.
       */
      private void displayDatabaseInfo() {
  
          // Create and/or open a database to read from it
          SQLiteDatabase db = mDbHelper.getReadableDatabase();
  
          String[] projections = {PetEntry._ID,PetEntry.COLUMN_PET_NAME,PetEntry.COLUMN_PET_BREED,PetEntry.COLUMN_PET_GENDER,PetEntry.COLUMN_PET_WEIGHT};
  
          TextView displayView = (TextView) findViewById(R.id.text_view_pet);
  
          // Perform this raw SQL query "SELECT * FROM pets"
          // to get a Cursor that contains all rows from the pets table.
          //Cursor cursor = db.query(PetEntry.TABLE_NAME,columns, null,null,null,null,null);
          Cursor cursor = getContentResolver().query(PetEntry.CONTENT_URI,projections,null,null,null);;
          try {
              // Create a header in the Text View that looks like this:
              //
              // The pets table contains <number of rows in Cursor> pets.
              // _id - name - breed - gender - weight
              //
              // In the while loop below, iterate through the rows of the cursor and display
              // the information from each column in this order.
              displayView.setText("The pets table contains " + cursor.getCount() + " pets.\n\n");
              displayView.append(PetEntry._ID + " - " +
                      PetEntry.COLUMN_PET_NAME + "-" + PetEntry.COLUMN_PET_BREED + "-"
                      + PetEntry.COLUMN_PET_GENDER + "-" + PetEntry.COLUMN_PET_WEIGHT + "\n");
  
              // Figure out the index of each column
              int idColumnIndex = cursor.getColumnIndex(PetEntry._ID);
              int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
              int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
              int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
              int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);
  
              // Iterate through all the returned rows in the cursor
              while (cursor.moveToNext()) {
                  // Use that index to extract the String or Int value of the word
                  // at the current row the cursor is on.
                  int currentID = cursor.getInt(idColumnIndex);
                  String currentName = cursor.getString(nameColumnIndex);
                  String currentBreed = cursor.getString(breedColumnIndex);
                  String currentGender = (cursor.getInt(genderColumnIndex) == 0) ? "Male" : "Female";
                  String currentWeight = cursor.getString(weightColumnIndex);
  
                  // Display the values from each column of the current row in the cursor in the TextView
                  displayView.append(("\n" + currentID + " - " +
                          currentName)+ "-" + currentBreed + "-"
                          + currentGender + "-" + currentWeight);
              }
          } finally {
              // Always close the cursor when you're done reading from it. This releases all its
              // resources and makes it invalid.
              cursor.close();
          }
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          displayDatabaseInfo();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu options from the res/menu/menu_catalog.xml file.
          // This adds menu items to the app bar.
          getMenuInflater().inflate(R.menu.menu_catalog, menu);
          return true;
      }
  
      public void insertPet(){
  
          // Create a ContentValues object where column names are the keys,
          // and Toto's pet attributes are the values.
          ContentValues values = new ContentValues();
          values.put(PetEntry.COLUMN_PET_NAME, "Toto");
          values.put(PetEntry.COLUMN_PET_BREED, "Terrier");
          values.put(PetEntry.COLUMN_PET_GENDER, PetEntry.GENDER_MALE);
          values.put(PetEntry.COLUMN_PET_WEIGHT, 7);
  
          // Insert a new row for Toto into the provider using the ContentResolver.
          // Use the {@link PetEntry#CONTENT_URI} to indicate that we want to insert
          // into the pets database table.
          // Receive the new content URI that will allow us to access Toto's data in the future.
          Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // User clicked on a menu option in the app bar overflow menu
          switch (item.getItemId()) {
              // Respond to a click on the "Insert dummy data" menu option
              case R.id.action_insert_dummy_data:
                  // Do nothing for now
                  insertPet();
                  displayDatabaseInfo();
                  return true;
              // Respond to a click on the "Delete all entries" menu option
              case R.id.action_delete_all_entries:
                  // Do nothing for now
                  return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  