/*
 * Copyright (c) 2015, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.sdk.module.messagecenter.view;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.apptentive.android.sdk.Log;
import com.apptentive.android.sdk.R;
import com.apptentive.android.sdk.model.Message;
import com.apptentive.android.sdk.module.metric.MetricModule;
import com.apptentive.android.sdk.util.Util;

/**
 * @author Sky Kelsey
 */
abstract public class PersonalMessageView<T extends Message> extends MessageView<T> {

	public PersonalMessageView(Context context, final T message) {
		super(context, message);
	}

	/**
	 * Perform any view initialization here. Make sure to call super.init() first to initialise the parent hierarchy.
	 */
	protected void init(T message) {
		super.init(message);
		LayoutInflater inflater = LayoutInflater.from(context);
		if (message.isOutgoingMessage()) {
			inflater.inflate(R.layout.apptentive_message_outgoing, this);
		} else {
			try {
				inflater.inflate(R.layout.apptentive_message_incoming, this);
			} catch (Exception e) {
				Log.e("Error: ", e);
				Log.e("Caused by: ", e.getCause());
				Log.e("Caused by: ", e.getCause().getCause());
			}
		}
	}

	/**
	 * Call when you need to update the view with changed message contents. This should ONLY be called after the view has
	 * been initialized with a message.
	 *
	 * @param newMessage The new message whose contents we want to display.
	 */
	public void updateMessage(final T newMessage) {
		T oldMessage = message;
		super.updateMessage(newMessage);

		// Set timestamp
		TextView timestampView = (TextView) findViewById(R.id.timestamp);
		timestampView.setText(createTimestamp(message.getCreatedAt()));

		// Set avatar
		if (!message.isOutgoingMessage()) {
			AvatarView avatarView = (AvatarView) findViewById(R.id.avatar);
			String photoUrl = message.getSenderProfilePhoto();
			boolean avatarNeedsUpdate = oldMessage == null || (photoUrl != null && !photoUrl.equals(oldMessage.getSenderProfilePhoto()));
			if (avatarNeedsUpdate) {
				avatarView.fetchImage(message.getSenderProfilePhoto());
			}
		}
	}

	protected String createTimestamp(Double seconds) {
		Resources resources = context.getResources();
		if (seconds != null) {
			return Util.secondsToDisplayString(resources.getString(R.string.apptentive_message_sent_timestamp_format), seconds);
		}
		return resources.getString(R.string.apptentive_sending);
	}
}

@@ -397,46 +397,8 @@
 ew;%0A
-import com.apptentive.android.sdk.Log;
 %0Aimp
@@ -484,70 +484,8 @@
 ge;%0A
-import com.apptentive.android.sdk.module.metric.MetricModule;%0A
 impo
@@ -1095,18 +1095,8 @@
 e %7B%0A
-%09%09%09try %7B%0A%09
 %09%09%09i
@@ -1160,156 +1160,8 @@
 s);%0A
-%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09Log.e(%22Error: %22, e);%0A%09%09%09%09Log.e(%22Caused by: %22, e.getCause());%0A%09%09%09%09Log.e(%22Caused by: %22, e.getCause().getCause());%0A%09%09%09%7D%0A
 %09%09%7D%0A

Remove debugging information per code review