/*
 * ************************************************************************
 *
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2013 Adobe Systems Incorporated
 * All Rights Reserved.
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 *
 **************************************************************************
 */

package com.adobe.primetime.reference.ui.log;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.logging.InMemoryLogger.LogEntry;

import java.util.List;

public class LogListAdapter extends ArrayAdapter<LogEntry> {

	private Context context;
	private List<LogEntry> logList;

	private static class ViewHolder {
		ImageView imgIcon;
		TextView txtTag;
		TextView txtMessage;
		TextView txtTime;
	}

	public LogListAdapter(Context context, int textViewResourceId,
			List<LogEntry> objects) {
		super(context, textViewResourceId, objects);

		this.context = context;
		logList = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		LogEntry logEntry = logList.get(position);

		if (convertView == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.item_list_log, null);

			viewHolder = new ViewHolder();
			viewHolder.imgIcon = (ImageView) convertView
					.findViewById(R.id.imgLogIcon);
			viewHolder.txtTag = (TextView) convertView
					.findViewById(R.id.txtLogTag);
			viewHolder.txtMessage = (TextView) convertView
					.findViewById(R.id.txtLogMessage);
			viewHolder.txtTime = (TextView) convertView
					.findViewById(R.id.txtLogTime);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.txtTag.setText(logEntry.getTag());
		viewHolder.txtMessage.setText(logEntry.getMessage());
		viewHolder.txtTime.setText(logEntry.getDateTime());

		switch (logEntry.getVerbosity()) {
		case INFO:
			viewHolder.imgIcon.setImageResource(R.drawable.icon_info);
			viewHolder.txtTag.setTextColor(context.getResources().getColor(
					R.color.log_info));
			break;

		case WARN:
			viewHolder.imgIcon.setImageResource(R.drawable.icon_warn);
			viewHolder.txtTag.setTextColor(context.getResources().getColor(
					R.color.log_warn));
			break;

		case ERROR:
			viewHolder.imgIcon.setImageResource(R.drawable.icon_error);
			viewHolder.txtTag.setTextColor(context.getResources().getColor(
					R.color.log_error));
			break;

		case DEBUG:
			viewHolder.imgIcon.setImageResource(R.drawable.icon_debug);
			viewHolder.txtTag.setTextColor(context.getResources().getColor(
					R.color.log_debug));
			break;

		default:
			// do nothing
		}

		return convertView;
	}

}
