/*
 * 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.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.android.pets.data.PetContract;
import com.example.android.pets.data.PetDbHelper;
import com.example.android.pets.data.PetContract.PetEntry;

/**
 * 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);



    }
    private void displayDatabaseInfo() {
        // To access our database, we instantiate our subclass of SQLiteOpenHelper
        // and pass the context, which is the current activity.
        PetDbHelper mDbHelper = new PetDbHelper(this);

        // Create and/or open a database to read from it
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        String[] projection={
                PetEntry._ID,
                PetEntry.COLUMN_PET_NAME,
                PetEntry.COLUMN_PET_WEIGHT,
                PetEntry.COLUMN_PET_GENDER,
                PetEntry.COLUMN_PET_BREED
        };

        Cursor cursor=db.query(PetEntry.TABLE_NAME,
                projection,
                null,
                null,
                null,
                null,
                null);


        try {
            // Display the number of rows in the Cursor (which reflects the number of rows in the
            // pets table in the database).
            TextView displayView = (TextView) findViewById(R.id.text_view_pet);
            displayView.setText("Number of rows in pets database table: " + cursor.getCount());
        } 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;
    }

    private void insertPet(){
        //Get the data repository in write mode
        SQLiteDatabase db=mDbHelper.getWritableDatabase();

        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);

        long newrowId=db.insert(PetEntry.TABLE_NAME,null,values);

        Log.v("CatalogActivity","New Row Id"+newrowId);
    }

    @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:
                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);
    }
}

@@ -1279,16 +1279,42 @@
 Entry;%0A%0A
+import org.w3c.dom.Text;%0A%0A
 /**%0A * D
@@ -2894,24 +2894,94 @@
     null);%0A%0A
+        TextView textView=(TextView)findViewById(R.id.text_view_pet);%0A
 %0A        try
@@ -3125,16 +3125,17 @@
 abase).%0A
+%0A
         
@@ -3142,171 +3142,1263 @@
     
-T
+t
 extView
- displayView = (TextView) findViewById(R.id.text_view_pet);%0A            displayView.setText(%22Number of rows in pets database table: %22 + cursor.getCount());
+.setText(%22Number of rows in pets database table: %22 + cursor.getCount());%0A            textView.append(PetEntry._ID+%22-%22+PetEntry.COLUMN_PET_NAME+%0A                            %22-%22+PetEntry.COLUMN_PET_BREED+%22-%22+PetEntry.COLUMN_PET_GENDER+%0A                            %22-%22+PetEntry.COLUMN_PET_WEIGHT+%22%5Cn%5Cn%22);%0A%0A            int idColumnIndex=cursor.getColumnIndex(PetEntry._ID);%0A            int nameColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);%0A            int weightColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);%0A            int genderColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);%0A            int breedColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);%0A%0A            while(cursor.moveToNext())%7B%0A%0A                int currentID= cursor.getInt(idColumnIndex);%0A                String currentName = cursor.getString(nameColumnIndex);%0A                int currentWeight= cursor.getInt(weightColumnIndex);%0A                int currentGender=cursor.getInt(genderColumnIndex);%0A                String currentBreed= cursor.getString(breedColumnIndex);%0A%0A                textView.append(%22%5Cn%22+currentID+%22-%22+currentName+%22-%22+currentBreed+%22-%22%0A                        +currentGender+%22-%22+currentWeight);%0A            %7D%0A
 %0A   

Modify cursor to get required data