package de.bitdroid.flooding.news;

import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.text.format.DateFormat;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Date;

import de.bitdroid.flooding.R;
import de.bitdroid.flooding.main.MainActivity;
import it.gmariotti.cardslib.library.internal.Card;

final class NewsCard extends Card {

	private final Pair<NewsItem, Boolean> data;

	public NewsCard(
			final Activity activity,
			final NewsManager manager,
			final Pair<NewsItem, Boolean> data) {

		super(activity, R.layout.news_card);
		this.data = data;

		setSwipeable(true);
		setOnSwipeListener(new OnSwipeListener() {
			@Override
			public void onSwipe(Card card) {
				manager.removeItem(data.first);
			}
		});
		setOnUndoSwipeListListener(new OnUndoSwipeListListener() {
			@Override
			public void onUndoSwipe(Card card) {
				manager.addItem(data.first, false);
			}
		});


		setOnClickListener(new OnCardClickListener() {
			@Override
			public void onClick(Card card, View view) {
				Intent intent = new Intent(MainActivity.ACTION_NAVIGATE);
				intent.putExtra(MainActivity.EXTRA_POSITION, data.first.getNavigationPos());
				activity.sendBroadcast(intent);
			}
		});
	}


	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		TextView title = (TextView) parent.findViewById(R.id.news_title);
		TextView msg  = (TextView) parent.findViewById(R.id.news_timestamp);
		TextView content = (TextView) parent.findViewById(R.id.news_content);

		Date date = new Date(data.first.getTimestamp());

		title.setText(data.first.getTitle());
		msg.setText(DateFormat.getDateFormat(getContext()).format(date)
			+ " " + DateFormat.getTimeFormat(getContext()).format(date));
		content.setText(Html.fromHtml(data.first.getContent()));
	}

	public NewsItem getNewsItem() {
		return data.first;
	}

	public boolean isRead() {
		return data.second;
	}

}

@@ -1015,17 +1015,62 @@
 %0A%09%09%7D);%0A%0A
-%0A
+%09%09if (getNewsItem().isNavigationEnabled()) %7B%0A%09
 %09%09setOnC
@@ -1102,32 +1102,33 @@
 ickListener() %7B%0A
+%09
 %09%09%09@Override%0A%09%09%09
@@ -1119,32 +1119,33 @@
 %09%09%09@Override%0A%09%09%09
+%09
 public void onCl
@@ -1168,24 +1168,25 @@
 iew view) %7B%0A
+%09
 %09%09%09%09Intent i
@@ -1235,16 +1235,17 @@
 IGATE);%0A
+%09
 %09%09%09%09inte
@@ -1321,16 +1321,17 @@
 ));%0A%09%09%09%09
+%09
 activity
@@ -1357,24 +1357,30 @@
 nt);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%0A%09@O

disable card click for items with disabled navigation setting