package com.BreakingBytes.SifterReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.TextView;

public class CategoryDetail extends Activity {
	
	public static final String CATEGORY_ISSUES_URL = "issues_url";
	private SifterHelper mSifterHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categories);

		mSifterHelper = new SifterHelper(this);
		
		// capture our View elements
		TextView categoryName = (TextView) findViewById(R.id.category_name);
		TextView issuesURL = (TextView) findViewById(R.id.category_issues_url);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			try {
				JSONObject category = new JSONObject(extras.getString(CategoriesActivity.CATEGORY));
				if (category != null && checkFields(category)) {
					categoryName.setText(category.getString(CategoriesActivity.CATEGORY_NAME));
					issuesURL.setText(category.getString(CATEGORY_ISSUES_URL));
					Linkify.addLinks(issuesURL, Linkify.WEB_URLS);
				}
			} catch (JSONException e) {
				e.printStackTrace();
				mSifterHelper.onException(e.toString());
			}
		}
	}
	
	private boolean checkFields(JSONObject category) throws JSONException {
		String API_ISSUES_URL = "api_issues_url";
		JSONArray fieldNames = category.names();
		int numKeys = fieldNames.length();
		for (int j = 0;j < numKeys; j++) {
			if (!CategoriesActivity.CATEGORY_NAME.equals(fieldNames.getString(j)) &&
					!CATEGORY_ISSUES_URL.equals(fieldNames.getString(j)) &&
					!API_ISSUES_URL.equals(fieldNames.getString(j)))
				return false;
		}
		return true;
	}
}


@@ -587,39 +587,8 @@
 %0A%09%09%0A
-%09%09// capture our View elements%0A
 %09%09Te
@@ -654,16 +654,16 @@
 _name);%0A
+
 %09%09TextVi
@@ -789,20 +789,28 @@
 ras 
-!
+=
 = null)
- %7B
 %0A%09
+%09%09return;%0A
 %09%09tr
@@ -816,17 +816,16 @@
 ry %7B%0A%09%09%09
-%09
 JSONObje
@@ -901,17 +901,16 @@
 GORY));%0A
-%09
 %09%09%09if (c
@@ -957,17 +957,16 @@
 ) %7B%0A%09%09%09%09
-%09
 category
@@ -1029,25 +1029,24 @@
 ORY_NAME));%0A
-%09
 %09%09%09%09issuesUR
@@ -1101,17 +1101,16 @@
 ));%0A%09%09%09%09
-%09
 Linkify.
@@ -1151,20 +1151,18 @@
 LS);%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D catc
@@ -1186,17 +1186,16 @@
 e) %7B%0A%09%09%09
-%09
 e.printS
@@ -1207,17 +1207,16 @@
 race();%0A
-%09
 %09%09%09mSift
@@ -1251,21 +1251,16 @@
 ing());%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09

refactor nested if statements