package com.brink.main.Fragments;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.brink.main.Utility.FlushedInputStream;
import com.brink.main.IncomingAPI;
import com.brink.main.OutgoingAPI;
import com.brink.main.R;
import com.brink.main.RequestMethod;
import com.brink.main.RestClient;
import com.brink.main.Stored;
public class TopBars extends FragmentActivity{
private static String TAG = "TopBarsFragmentActivity";
private static Context gContext;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
//Add our fragment if and only if it has not been added before
if(fm.findFragmentById(android.R.id.content) == null)
{
Log.i(TAG, "onCreate - Adding TopBarsFragment");
TopBarsFragment mTopBarsFragment = new TopBarsFragment();
fm.beginTransaction().add(android.R.id.content, mTopBarsFragment).commit();
}
}
public static class TopBarsFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.topbars, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gContext = getActivity();
TopBars tBars = new TopBars();
TopBarsAdapter barAdapt = tBars.new TopBarsAdapter(gContext, "Baton Rouge");
ListView topList = (ListView)getActivity().findViewById(R.id.list_topbars);
topList.setAdapter(barAdapt);
topList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i(TAG, "Logging CLICK");
Log.i(TAG, "AdapterView" + arg0);
Log.i(TAG, "View" + arg1);
Log.i(TAG, "Position" + arg2);
Log.i(TAG, "ID" + arg3);
}
});
}
}
public class TopBarsAdapter extends BaseAdapter
{
private ArrayList topbarsData = new ArrayList();
private LayoutInflater mInflater;
private Context mContext;
private String areaRegion;
private int topBarsLoaded;
private TopBar bar;
private String loadString;
private static final int TYPE_ITEM = 0;
private static final int TYPE_LOAD = 1;
public TopBarsAdapter(Context _context, String region)
{
mContext = _context;
mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
areaRegion = region;
Log.i(TAG, "In the Constructor, adding loadString");
topbarsData.add(loadString);
callPopulate();
}
public void callPopulate()
{
//This needs to communicated with the bars api and find which bars are rated higher than others for a certain region.
//Also needs to find out the subregions associated with that region.
//NotifyDataSetChanged once the list has been changed from a filter
new RetrieveTopBarsTask().execute(areaRegion);
}
public void addItem(JSONArray topBars)
{
Log.i(TAG, "Inside addItem for adapter");
if(topBars.length() != 0)
{
Log.i(TAG, "Array Length is " + topBars.length());
topbarsData.remove(topbarsData.size() - 1);
for(int i = 0; i < topBars.length(); i++)
{
bar = new TopBar();
try {
JSONObject mBar = new JSONObject();
mBar = topBars.getJSONObject(i);
bar.barName = mBar.getString("Name");
bar.barID = mBar.getString("_id");
bar.barHeat = mBar.getString("Hotness");
Log.i(TAG, bar.barName);
} catch (JSONException e) {
e.printStackTrace();
}
topbarsData.add(bar);
Log.i(TAG, "Adding another bar " + 0 + topbarsData.size());
}
topbarsData.add(loadString);
notifyDataSetChanged();
}
}
public int getCount() {
return topbarsData.size();
}
public Object getItem(int position) {
return topbarsData.get(position);
}
public int getViewTypeCount() {
return 2;
}
public int getItemViewType(int position) {
return position == (topbarsData.size() - 1) ? TYPE_LOAD : TYPE_ITEM;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TopBarHolder topBarItem = null;
LoadItemsHolder loadHolder = null;
if(convertView == null) {
switch(getItemViewType(position)) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.topbars_item, null);
topBarItem = new TopBarHolder();
topBarItem.barsName = (TextView)convertView.findViewById(R.id.topbars_item_bar);
topBarItem.barHeat = (ImageView)convertView.findViewById(R.id.topbars_item_heat);
topBarItem.populationPictures = (Gallery)convertView.findViewById(R.id.topbars_item_gallery);
convertView.setTag(topBarItem);
break;
case TYPE_LOAD:
convertView = mInflater.inflate(R.layout.feed_load, null);
loadHolder = new LoadItemsHolder();
loadHolder.loadingProgress = (ProgressBar)convertView.findViewById(R.id.feedloadprogress);
convertView.setTag(loadHolder);
break;
}
} else {
switch(getItemViewType(position)) {
case TYPE_ITEM:
topBarItem = (TopBarHolder) convertView.getTag();
break;
case TYPE_LOAD:
loadHolder = (LoadItemsHolder) convertView.getTag();
break;
}
}
switch(getItemViewType(position)) {
case TYPE_ITEM:
//Set the imageadapter for the Gallery
Log.i(TAG, "Position is " + 0 + position);
TopBar temp = (TopBar) getItem(position);
//Log.i(TAG, temp.barName);
topBarItem.populationPictures.setAdapter(new PopulationPicturesAdapter(mContext, temp.barID));
topBarItem.barsName.setText(temp.barName);
topBarItem.populationPictures.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i(TAG, "You Clicked a picture!!");
}
});
//Set a switch to choose which drawable to load for the barheat.
//topBarItem.barHeat.set
break;
case TYPE_LOAD:
//Set the progress bar visibility to invisible to indicate we are not expecting new items.
loadHolder.loadingProgress.setVisibility(View.INVISIBLE);
loadHolder.loadingProgress.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//Should set the progress bar into motion.
//Calls the populate method to load 10 more bars.
Log.i(TAG, "Clicked Load Items");
Log.i(TAG, "Size" + (topbarsData.size() - 1));
Log.i(TAG, "Size" + topbarsData.size());
}
});
break;
}
return convertView;
}
class TopBar{
String barName, barHeat, barID;
}
class TopBarHolder
{
TextView barsName;
ImageView barHeat;
Gallery populationPictures;
}
class LoadItemsHolder
{
ProgressBar loadingProgress;
}
private class RetrieveTopBarsTask extends AsyncTask<String, Void, JSONArray>
{
@Override
protected JSONArray doInBackground(String... params) {
//TODO make the client grab 10 bars at a time.
String url = OutgoingAPI.ApiUrl + "bars?$filter=City%20eq%20'Baton%20Rouge'";
RestClient client = new RestClient(url);
client.AddHeader("Content-Type", "application/json");
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
JSONArray bars = null;
try {
bars = new JSONArray(client.getResponse());
return bars;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONArray barObject)
{
Log.i(TAG, "Sending addItem the Array");
addItem(barObject);
}
}
public class PopulationPicturesAdapter extends BaseAdapter
{
private ArrayList popPics = new ArrayList();
private Context mContext;
private LayoutInflater mInflater;
private String barID;
private Bitmap loadBit;
private Bitmap popPicLoad;
int mGalleryItemBackground;
private static final int TYPE_PIC = 0;
private static final int TYPE_LOAD = 1;
public PopulationPicturesAdapter(Context _context, String id)
{
mContext = _context;
TypedArray a = mContext.obtainStyledAttributes(R.styleable.PopulationPicsGallery);
mGalleryItemBackground = a.getResourceId(R.styleable.PopulationPicsGallery_android_galleryItemBackground, 0);
a.recycle();
callTaskPopulate(id);
}
public void callTaskPopulate(String _id)
{
//Call task that goes through all the links and retrieves the pictures.
barID = _id;
new RetrievePopPics(mContext).execute(_id);
}
public void addPicture(Bitmap popPic)
{
if(popPics.size() != 0)
{
popPics.remove(popPics.size() - 1);
popPics.add(popPic);
popPicLoad = popPic;
popPics.add(loadBit);
notifyDataSetChanged();
}
else
{
//Just add the pics then add the load layout at the end of it.
popPics.add(popPic);
popPics.add(loadBit);
notifyDataSetChanged();
}
}
public int getCount() {
return popPics.size();
}
public int getViewTypeCount() {
return 2;
}
public int getItemViewType(int position) {
return position == (popPics.size() - 1) ? TYPE_LOAD : TYPE_PIC;
}
public Object getItem(int position) {
return popPics.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView popPic = null;
ImageView loadView = null;
switch(getItemViewType(position)) {
case TYPE_PIC:
popPic = new ImageView(mContext);
popPic.setImageBitmap(popPicLoad);
popPic.setLayoutParams(new Gallery.LayoutParams(150, 100));
popPic.setScaleType(ImageView.ScaleType.FIT_XY);
popPic.setBackgroundResource(mGalleryItemBackground);
return popPic;
case TYPE_LOAD:
loadView = new ImageView(mContext);
loadView.setImageBitmap(loadBit);
loadView.setBackgroundColor(Color.GRAY);
return loadView;
default:
Log.wtf(TAG, "Not supposed to be here wtf");
return null;
}
}
private class RetrievePopPics extends AsyncTask<String, Void, Bitmap>
{
private Context mContext;
public RetrievePopPics(Context _context)
{
mContext = _context;
}
protected Bitmap doInBackground(String... params) {
return IncomingAPI.RetrieveFacebookAvatar();
}
protected void onPostExecute(Bitmap result)
{
addPicture(result);
}
}
}
}
}