/*
 * 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.udacity.example.quizexample;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.udacity.example.droidtermsprovider.DroidTermsExampleContract;

/**
 * Gets the data from the ContentProvider and shows a series of flash cards.
 */

public class MainActivity extends AppCompatActivity {

    // The data from the DroidTermsExample content provider
    private Cursor mData;

    // The current state of the app
    private int mCurrentState;

    private Button mButton;

    // This state is when the word definition is hidden and clicking the button will therefore
    // show the definition
    private final int STATE_HIDDEN = 0;

    // This state is when the word definition is shown and clicking the button will therefore
    // advance the app to the next word
    private final int STATE_SHOWN = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the views
        // TODO (1) You'll probably want more than just the Button
        mButton = (Button) findViewById(R.id.button_next);

        //Run the database operation to get the cursor off of the main thread
        new WordFetchTask().execute();

    }

    /**
     * This is called from the layout when the button is clicked and switches between the
     * two app states.
     * @param view The view that was clicked
     */
    public void onButtonClick(View view) {

        // Either show the definition of the current word, or if the definition is currently
        // showing, move to the next word.
        switch (mCurrentState) {
            case STATE_HIDDEN:
                showDefinition();
                break;
            case STATE_SHOWN:
                nextWord();
                break;
        }
    }

    public void nextWord() {

        // Change button text
        mButton.setText(getString(R.string.show_definition));

        // TODO (3) Go to the next word in the Cursor, show the next word and hide the definition
        // Note that you shouldn't try to do this if the cursor hasn't been set yet.
        // If you reach the end of the list of words, you should start at the beginning again.
        mCurrentState = STATE_HIDDEN;

    }

    public void showDefinition() {

        // Change button text
        mButton.setText(getString(R.string.next_word));

        // TODO (4) Show the definition
        mCurrentState = STATE_SHOWN;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO (5) Remember to close your cursor!
    }

    // Use an async task to do the data fetch off of the main thread.
    public class WordFetchTask extends AsyncTask<Void, Void, Cursor> {

        // Invoked on a background thread
        @Override
        protected Cursor doInBackground(Void... params) {
            // Make the query to get the data

            // Get the content resolver
            ContentResolver resolver = getContentResolver();

            // Call the query method on the resolver with the correct Uri from the contract class
            Cursor cursor = resolver.query(DroidTermsExampleContract.CONTENT_URI,
                    null, null, null, null);
            return cursor;
        }


        // Invoked on UI thread
        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);

            // Set the data for MainActivity
            mData = cursor;

            // TODO (2) Initialize anything that you need the cursor for, such as setting up
            // the screen with the first word and setting any other instance variables
        }
    }

}

@@ -887,16 +887,48 @@
 .Button;
+%0Aimport android.widget.TextView;
 %0A%0Aimport
@@ -1320,16 +1320,80 @@
 utton;%0A%0A
+    private TextView mWord;%0A%0A    private TextView mDefinition;%0A%0A
     // T
@@ -2034,24 +2034,162 @@
 ton_next);%0A%0A
+        mWord = (TextView) findViewById(R.id.text_view_word);%0A%0A        mDefinition = (TextView) findViewById(R.id.text_view_definition);%0A%0A
         //Ru
@@ -3275,16 +3275,649 @@
  again.%0A
+        if (mData != null) %7B%0A            int wordCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_WORD);%0A            int definitionCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_DEFINITION);%0A            if (mData.moveToNext()) %7B%0A                String word = mData.getString(wordCol);%0A                String definition = mData.getString(definitionCol);%0A                mWord.setText(word);%0A                mDefinition.setText(definition);%0A                mDefinition.setVisibility(View.INVISIBLE);%0A            %7D else %7B%0A                mData.moveToFirst();%0A                nextWord();%0A            %7D%0A        %7D%0A%0A
         
@@ -3946,17 +3946,16 @@
 HIDDEN;%0A
-%0A
     %7D%0A%0A 
@@ -4108,32 +4108,82 @@
  the definition%0A
+        mDefinition.setVisibility(View.VISIBLE);%0A%0A
         mCurrent
@@ -4336,16 +4336,39 @@
 cursor!%0A
+        mData.close();%0A
     %7D%0A%0A 
@@ -5439,16 +5439,524 @@
 riables%0A
+            int wordCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_WORD);%0A            int definitionCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_DEFINITION);%0A            if (mData.moveToFirst()) %7B%0A                String word = mData.getString(wordCol);%0A                String definition = mData.getString(definitionCol);%0A                mWord.setText(word);%0A                mDefinition.setText(definition);%0A                mDefinition.setVisibility(View.INVISIBLE);%0A            %7D%0A
         
@@ -5963,11 +5963,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Complete TODOs for T08.03 FinishQuizExample exercise