package de.wak_sh.client.fragments;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

import de.wak_sh.client.R;
import de.wak_sh.client.backend.ProgressDialogTask;
import de.wak_sh.client.backend.model.Message;
import de.wak_sh.client.backend.service.MessageService;

public class NachrichtLesenFragment extends SherlockFragment {
	private TextView date;
	private TextView from;
	private TextView subject;
	private TextView text;
	private TextView attachment;
	private LinearLayout attachmentList;
	private Message message;

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

		date = (TextView) rootView.findViewById(R.id.msg_date);
		from = (TextView) rootView.findViewById(R.id.msg_from);
		subject = (TextView) rootView.findViewById(R.id.msg_subject);
		text = (TextView) rootView.findViewById(R.id.msg_text);
		attachment = (TextView) rootView.findViewById(R.id.msg_attachment);
		attachmentList = (LinearLayout) rootView
				.findViewById(R.id.msg_attachment_list);

		setHasOptionsMenu(true);

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		new MessageTask(activity).execute(getArguments().getInt("msgid"));
	}

	protected void setMessage(Message message) {
		date.setText(message.getDate());
		from.setText(message.getSender());
		subject.setText(message.getSubject());
		text.setText(message.getContent());
		this.message = message;

		if (message.getAttachmentIds().size() > 0) {
			showAttachments();
		}
	}

	public void showAttachments() {
		attachment.setVisibility(View.VISIBLE);
		List<Integer> attachmentIds = message.getAttachmentIds();
		List<String> filenames = message.getAttachmentFilenames();
		int msgId = message.getId();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				R.layout.attachment_list_item, R.id.attachment_list_item,
				message.getAttachmentFilenames());
		for (int i = 0; i < adapter.getCount(); i++) {
			View item = adapter.getView(i, null, null);
			attachmentList.addView(item);

			int attachmentId = attachmentIds.get(i);
			final String filename = filenames.get(i);
			final String url = MessageService.buildAttachmentUrl(msgId,
					attachmentId);
			item.setFocusable(true);
			item.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO: download file
				}
			});
		}
	}

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

	private class MessageTask extends ProgressDialogTask<Integer, Message> {
		private Activity activity;

		public MessageTask(Activity activity) {
			super(activity, activity.getString(R.string.fetching_message));
			this.activity = activity;
		}

		@Override
		protected Message doInBackground(Integer... params) {
			MessageService service = MessageService.getInstance();
			int msgid = params[0];
			try {
				service.fetchMessagesContent(msgid);
				return service.getMessage(msgid);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onPostExecute(final Message result) {
			super.onPostExecute(result);
			activity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					setMessage(result);
				}
			});
		}
	}
}

@@ -544,16 +544,66 @@
 ient.R;%0A
+import de.wak_sh.client.backend.FileDownloadTask;%0A
 import d
@@ -2962,30 +2962,67 @@
 %09%09%09%09
-// TODO: download file
+new FileDownloadTask(getActivity(), filename).execute(url);
 %0A%09%09%09

Implement message attachment download