package de.danoeh.antennapod.activity;

import android.content.Intent;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FlattrClickWorker;
import de.danoeh.antennapod.asynctask.OpmlExportWorker;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.util.flattr.FlattrUtils;

/** The main preference activity */
public class PreferenceActivity extends SherlockPreferenceActivity {
	private static final String TAG = "PreferenceActivity";

	private static final String PREF_FLATTR_THIS_APP = "prefFlattrThisApp";
	private static final String PREF_FLATTR_AUTH = "pref_flattr_authenticate";
	private static final String PREF_FLATTR_REVOKE = "prefRevokeAccess";
	private static final String PREF_OPML_EXPORT = "prefOpmlExport";
	private static final String PREF_ABOUT = "prefAbout";

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(PodcastApp.getThemeResourceId());
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		addPreferencesFromResource(R.xml.preferences);
		findPreference(PREF_FLATTR_THIS_APP).setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						new FlattrClickWorker(PreferenceActivity.this,
								FlattrUtils.APP_URL).executeAsync();

						return true;
					}
				});

		findPreference(PREF_FLATTR_REVOKE).setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						FlattrUtils.revokeAccessToken(PreferenceActivity.this);
						checkItemVisibility();
						return true;
					}

				});

		findPreference(PREF_ABOUT).setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						PreferenceActivity.this.startActivity(new Intent(
								PreferenceActivity.this, AboutActivity.class));
						return true;
					}

				});

		findPreference(PREF_OPML_EXPORT).setOnPreferenceClickListener(
				new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						if (!FeedManager.getInstance().getFeeds().isEmpty()) {
							new OpmlExportWorker(PreferenceActivity.this)
									.executeAsync();
						}
						return true;
					}
				});
		findPreference(PodcastApp.PREF_THEME).setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				Intent i = getIntent();
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
				finish();
				startActivity(i);
				return true;
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		checkItemVisibility();
	}

	@SuppressWarnings("deprecation")
	private void checkItemVisibility() {

		boolean hasFlattrToken = FlattrUtils.hasToken();

		findPreference(PREF_FLATTR_AUTH).setEnabled(!hasFlattrToken);
		findPreference(PREF_FLATTR_REVOKE).setEnabled(hasFlattrToken);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		default:
			return false;
		}
		return true;
	}

	@Override
	protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
		theme.applyStyle(PodcastApp.getThemeResourceId(), true);
	}
	
	

}

@@ -68,52 +68,8 @@
 nt;%0A
-import android.content.res.Resources.Theme;%0A
 impo
@@ -3927,162 +3927,6 @@
 %09%7D%0A%0A
-%09@Override%0A%09protected void onApplyThemeResource(Theme theme, int resid, boolean first) %7B%0A%09%09theme.applyStyle(PodcastApp.getThemeResourceId(), true);%0A%09%7D%0A%09%0A%09%0A%0A
 %7D%0A

Revert "PreferenceActivity now applies its theme correctly"