package de.wak_sh.client.fragments;

import java.io.IOException;
import java.util.List;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import de.wak_sh.client.R;
import de.wak_sh.client.backend.DataStorage;
import de.wak_sh.client.fragments.backend.AdapterEmails;
import de.wak_sh.client.model.Email;
import de.wak_sh.client.service.JsoupEmailService;

public class FragmentEmails extends WakFragment implements OnItemClickListener {

	private DataStorage mStorage = DataStorage.getInstance();

	private ListView mListView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_emails, container,
				false);

		mListView = (ListView) rootView.findViewById(android.R.id.list);
		mListView.setOnItemClickListener(this);

		if (mStorage.getEmails().isEmpty()) {
			new EmailTask().execute();
		} else {
			updateViews();
		}

		setHasOptionsMenu(true);

		return rootView;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.emails, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.action_write) {
			Fragment fragment = new FragmentWriteEmail();

			Bundle bundle = new Bundle();
			bundle.putString("title", getString(R.string.email_write));
			bundle.putInt("iconRes", R.drawable.ic_launcher);

			fragment.setArguments(bundle);

			FragmentManager manager = getFragmentManager();

			FragmentTransaction transaction = manager.beginTransaction();
			transaction.addToBackStack(null);
			transaction.replace(R.id.content_frame, fragment, WakFragment.TAG);
			transaction.commit();

			getSherlockActivity().getSupportActionBar().setTitle(
					getString(R.string.email_write));
			getSherlockActivity().getSupportActionBar().setIcon(
					R.drawable.ic_launcher);

			return true;
		}
		return false;
	}

	private void updateViews() {
		mListView.setAdapter(new AdapterEmails(getActivity(), mStorage
				.getEmails()));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Email email = mStorage.getEmails().get(position);

		Fragment fragment = new FragmentEmail();

		Bundle bundle = new Bundle();
		bundle.putString("title", getString(R.string.email));
		bundle.putInt("iconRes", R.drawable.ic_launcher);
		bundle.putSerializable("email", email);

		fragment.setArguments(bundle);

		FragmentManager manager = getFragmentManager();

		FragmentTransaction transaction = manager.beginTransaction();
		transaction.addToBackStack(null);
		transaction.replace(R.id.content_frame, fragment, WakFragment.TAG);
		transaction.commit();
	}

	private class EmailTask extends AsyncTask<Void, Void, List<Email>> {
		private ProgressDialog mProgressDialog;

		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(getActivity());
			mProgressDialog.setMessage(getString(R.string.fetching_messages));
			mProgressDialog.setCancelable(false);
			mProgressDialog.setCanceledOnTouchOutside(false);
			mProgressDialog.show();
		}

		@Override
		protected List<Email> doInBackground(Void... params) {
			try {
				return JsoupEmailService.getInstance().getEmails();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onPostExecute(List<Email> result) {
			if (mProgressDialog != null && mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
			}

			if (result != null) {
				mStorage.setEmails(result);
				updateViews();
			}
		}
	}

}

@@ -101,55 +101,23 @@
 oid.
-app.ProgressDialog;%0Aimport android.os.AsyncTask
+content.Context
 ;%0Aim
@@ -699,16 +699,62 @@
 torage;%0A
+import de.wak_sh.client.backend.ProgressTask;%0A
 import d
@@ -1430,16 +1430,58 @@
 ailTask(
+getActivity(), null, %22Hole Nachrichten...%22
 ).execut
@@ -2477,16 +2477,160 @@
 cher);%0A%0A
+%09%09%09return true;%0A%09%09%7D else if (item.getItemId() == R.id.action_refresh) %7B%0A%09%09%09new EmailTask(getActivity(), null, %22Hole Nachrichten...%22).execute();%0A
 %09%09%09retur
@@ -3478,13 +3478,16 @@
 nds 
-Async
+Progress
 Task
@@ -3518,341 +3518,109 @@
 %3E %7B%0A
+%0A
 %09%09p
-rivate ProgressDialog mProgressDialog;%0A%0A%09%09@Override%0A%09%09protected void onPreExecute() %7B%0A%09%09%09mProgressDialog = new ProgressDialog(getActivity());%0A%09%09%09mProgressDialog.setMessage(getString(R.string.fetching_messages));%0A%09%09%09mProgressDialog.setCancelable(false);%0A%09%09%09mProgressDialog.setCanceledOnTouchOutside(false);%0A%09%09%09mProgressDialog.show(
+ublic EmailTask(Context context, String title, String message) %7B%0A%09%09%09super(context, title, message
 );%0A%09
@@ -3910,105 +3910,36 @@
 %0A%09%09%09
-if (mProgressDialog != null && mProgressDialog.isShowing()) %7B%0A%09%09%09%09mProgressDialog.dismiss();%0A%09%09%09%7D
+super.onPostExecute(result);
 %0A%0A%09%09

Implement refreshin emails when hit corresponding button