package com.example.omgandroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private static final String PREFS = "prefs";
    private static final String PREF_NAME = "name";
    private static final String QUERY_URL = "http://openlibrary.org/search.json?q=";

    TextView mainTextView;
    Button mainButton;
    EditText mainEditText;
    ListView mainListView;
    ShareActionProvider mShareActionProvider;
    SharedPreferences mSharedPreferences;
    JSONAdapter mJSONAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We want to add a spinning progress bar (and make sure it's off)
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setProgressBarIndeterminateVisibility(false);

        // We have to tell the activity which XML layout is the right one
        setContentView(R.layout.activity_main);

        // Access the TextView defined in layout XML and set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);

        // Access the Button defined in layout XML and listen for it with this activity
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener(this);

        // Access the EditText defined in layout XML
        mainEditText = (EditText) findViewById(R.id.main_edittext);

        // Access the ListView
        mainListView = (ListView) findViewById(R.id.main_listview);

        // Create a JSONAdapter for the ListView
        mJSONAdapter = new JSONAdapter(this);

        // Set the ListView to use the ArrayAdapter
        mainListView.setAdapter(mJSONAdapter);

        // Set this activity to react to list items being pressed
        mainListView.setOnItemClickListener(this);

        // Greet the user, or ask for their name if new
        displayWelcome();
    }

    public void displayWelcome() {

        // Access the device's key-value storage
        mSharedPreferences = getSharedPreferences(PREFS, MODE_PRIVATE);

        // Read the user's name, or an empty string if nothing found
        String name = mSharedPreferences.getString(PREF_NAME, "");

        if (name.length() > 0) {

            // If the name is valid, display a Toast welcoming them
            Toast.makeText(this,
                    "Welcome back, " + name + "!",
                    Toast.LENGTH_LONG)
                    .show();
        } else {

            // otherwise, show a dialog to ask for their name
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Hello!");
            alert.setMessage("What is your name?");

            // Create EditText for entry
            final EditText input = new EditText(this);
            alert.setView(input);

            // Make an "OK" button to save the name
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    // Put the EditText's input into memory (don't forget to commit!)
                    SharedPreferences.Editor e = mSharedPreferences.edit();
                    e.putString(PREF_NAME, input.getText().toString());
                    e.commit();
                }
            });

            // Make a "Cancel" button that simply dismisses the alert
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });

            alert.show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        // Access the Share Item defined in menu XML
        MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        // Access the object responsible for putting together the sharing submenu
        if (shareItem != null) {
            mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
        }

        // Create an Intent to share our content
        // It is in a separate method so we can call it from elsewhere too
        setShareIntent();

        return true;
    }

    private void setShareIntent() {

        // create an Intent with the contents of the TextView
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Android Development");
        shareIntent.putExtra(Intent.EXTRA_TEXT, mainTextView.getText());

        // Make sure the provider knows it should work with that Intent
        mShareActionProvider.setShareIntent(shareIntent);
    }

    @Override public void onClick(View v) {

        // Take what was typed into the EditText and use in search
        queryBooks(mainEditText.getText().toString());
    }

    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Now that the user's chosen a book, let's grab the cover data
        String coverID = mJSONAdapter.getItem(position).optString("cover_i","");

        // create an Intent to take us over to a new DetailActivity
        Intent detailIntent = new Intent(this, DetailActivity.class);

        // pack away the data about the cover into our Intent before we head out
        detailIntent.putExtra("coverID", coverID);

        // TODO: add any other data we'd like as Extras

        // start the next Activity using our prepared Intent
        startActivity(detailIntent);
    }

    private void queryBooks(String searchString) {

        // Prepare our search string to be put in a URL
        // It might have reserved characters or something
        String urlString = "";
        try {
           urlString = URLEncoder.encode(searchString, "UTF-8");
        } catch (UnsupportedEncodingException e) {

            // if this fails for some reason, let the user know why
            e.printStackTrace();
            Toast.makeText(this,
                    "Error: " + e.getMessage(),
                    Toast.LENGTH_LONG)
                    .show();
        }

        // Create a client to perform networking for us
        AsyncHttpClient client = new AsyncHttpClient();

        setProgressBarIndeterminateVisibility(true);
        //mProgressBar.setVisibility(View.VISIBLE);

        // Have the client get a JSONArray of data, and define how to respond
        client.get(QUERY_URL + urlString,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONObject jsonObject) {

                        setProgressBarIndeterminateVisibility(false);

                        // Display a "Toast" message to announce our success
                        Toast.makeText(getApplicationContext(),
                                "Success!",
                                Toast.LENGTH_LONG)
                                .show();

                        // Now we are being wise and have created the JSONAdapter subclass
                        // update the data in our custom method.
                        mJSONAdapter.updateData(jsonObject);
                    }

                    @Override
                    public void onFailure(Throwable throwable, String s) {

                        setProgressBarIndeterminateVisibility(false);

                        // Display a "Toast" message to announce the failure
                        Toast.makeText(getApplicationContext(),
                                "Error: " + throwable.getMessage() + " " + s,
                                Toast.LENGTH_LONG)
                                .show();

                        // Log error message to help solve any problems
                        Log.e("omg android", throwable.getMessage() + " " + s);
                    }
                });
    }
}
@@ -3883,10 +3883,10 @@
  // 
-Pu
+Ge
 t th
@@ -3903,16 +3903,114 @@
 's input
+%0A                    String inputName = input.getText().toString();%0A%0A                    // Put it
  into me
@@ -4168,60 +4168,301 @@
 nput
-.getText().toString());%0A                    e.commit
+Name);%0A                    e.commit();%0A%0A                    // Display a Toast welcoming them%0A                    Toast.makeText(getApplicationContext(),%0A                            %22Welcome, %22 + inputName + %22!%22,%0A                            Toast.LENGTH_LONG)%0A                            .show
 ();%0A

add welcome for new users too