package com.humbughq.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class HumbugActivity extends Activity {
    public static final String SERVER_URI = "https://app.humbughq.com/";
    public static final String USER_AGENT = "HumbugMobile 1.0";

    ListView listView;

    HashMap<String, Bitmap> profile_pictures;

    /*
     * A "message" refers to an instance of the object Message. A "tile" is an
     * instance of LinearLayout which represents a single message in the UI.
     */

    SparseArray<Message> messageIndex;
    MessageAdapter adapter;

    AsyncPoller current_poll;

    boolean suspended = false;

    boolean logged_in = false;

    String api_key;

    String email;

    HumbugActivity that = this; // self-ref
    SharedPreferences settings;
    String client_id;
    protected int mIDSelected;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("funny", "starting...");

        settings = getPreferences(Activity.MODE_PRIVATE);

        this.email = settings.getString("email", null);
        this.api_key = settings.getString("api_key", null);

        if (this.api_key == null) {

            setContentView(R.layout.login);
            ((Button) findViewById(R.id.login))
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TextView errorText = (TextView) findViewById(R.id.error_text);
                            errorText.setText("Logging in...");
                            (new AsyncLogin(that,
                                    ((EditText) findViewById(R.id.username))
                                            .getText().toString(),
                                    ((EditText) findViewById(R.id.password))
                                            .getText().toString())).execute();
                        }
                    });
        } else {
            this.openLogin();
        }
        return;

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.compose:
            return true;
        case R.id.logout:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    protected void openLogin() {
        this.logged_in = true;
        messageIndex = new SparseArray<Message>();
        this.profile_pictures = new HashMap<String, Bitmap>();

        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.listview);

        adapter = new MessageAdapter(this, new ArrayList<Message>());

        listView.setAdapter(adapter);

        listView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // pass

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                int mID = ((Message) view.getItemAtPosition(view
                        .getFirstVisiblePosition())).getID();
                if (mIDSelected != mID) {
                    Log.i("scrolling", "Now at " + mID);
                    (new AsyncPointerUpdate(that)).execute(mID);
                    mIDSelected = mID;
                }
            }
        });

        listView.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                int mID = (Integer) view.getTag(R.id.messageID);
                if (mIDSelected != mID) {
                    Log.i("keyboard", "Now at " + mID);
                    (new AsyncPointerUpdate(that)).execute(mID);
                    mIDSelected = mID;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // pass

            }

        });
        (new AsyncPointerUpdate(this)).execute();

    }

    protected void onPause() {
        super.onPause();
        Log.i("status", "suspend");
        this.suspended = true;
        if (this.current_poll != null) {
            this.current_poll.cancel(true);
        }
    }

    protected void onResume() {
        super.onResume();
        Log.i("status", "resume");
        this.suspended = false;
        if (this.logged_in) {
            // Update the pointer

            if (this.adapter.getCount() != 0) {
                this.current_poll = new AsyncPoller(this, true, true);
                this.current_poll.execute((int) this.adapter
                        .getItemId(this.adapter.getCount() - 1) + 1);
            }
        }
    }
}

@@ -108,16 +108,48 @@
 tivity;%0A
+import android.content.Context;%0A
 import a
@@ -182,16 +182,16 @@
 rences;%0A
-
 import a
@@ -410,24 +410,59 @@
 .view.View;%0A
+import android.view.WindowManager;%0A
 import andro
@@ -659,16 +659,16 @@
 Button;%0A
-
 import a
@@ -687,24 +687,57 @@
 t.EditText;%0A
+import android.widget.ImageView;%0A
 import andro
@@ -3639,24 +3639,739 @@
 listview);%0A%0A
+        /*%0A         * We want to add a footer to the ListView that is half the window%0A         * height.%0A         * %0A         * Example algorithmic explanation:%0A         * http://stackoverflow.com/a/13366310/90777%0A         */%0A%0A        @SuppressWarnings(%22deprecation%22)%0A        // needed for compat with API %3C13%0A        int windowHeight = ((WindowManager) this%0A                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()%0A                .getHeight();%0A%0A        ImageView dummy = new ImageView(this);%0A        AbsListView.LayoutParams params = new AbsListView.LayoutParams(0, 0);%0A        params.height = windowHeight / 2;%0A        dummy.setLayoutParams(params);%0A%0A        listView.addFooterView(dummy);%0A
         adap
@@ -4428,17 +4428,16 @@
 ge%3E());%0A
-%0A
         

Implement an extra space at the bottom of the message list.