/*
 * ************************************************************************
 *
 * 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.catalog;

import android.content.Context;
import android.content.res.Configuration;
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.manager.EntitlementManager;
import com.adobe.primetime.reference.manager.InstantOnManager;
import com.adobe.primetime.reference.manager.ManagerFactory;

import java.util.ArrayList;
import java.util.List;

public class ContentTileAdapter extends ArrayAdapter<VideoItem> {

	private List<VideoItem> contentList;
	private Context context;

    private final EntitlementManager entitlementManager = ManagerFactory.getEntitlementManager();

	public ContentTileAdapter(Context context, int textViewResourceId,
			ArrayList<VideoItem> contentList2) {
		super(context, textViewResourceId, contentList2);

		this.context = context;
		contentList = contentList2;
	}

	private static class ViewHolder {
		ImageView thumbnail;
        ImageView entitlement;
		TextView title;
		TextView instantOnStatus;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		VideoItem contentTileInfo = contentList.get(position);

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

			viewHolder = new ViewHolder();
			viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.imgTileImage);
			viewHolder.title = (TextView) convertView.findViewById(R.id.txtTileTitle);
            viewHolder.entitlement = (ImageView) convertView.findViewById(R.id.imgEntitlementIcon);
			viewHolder.instantOnStatus = (TextView) convertView.findViewById(R.id.txtInstantOnStatus);

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

		viewHolder.thumbnail.setImageResource(R.drawable.grid_view_tile);
		viewHolder.title.setText(contentTileInfo.getTitle());

		// set InstantOn text status
		if (InstantOnManager.contains(contentTileInfo.getId()))
		{
			viewHolder.instantOnStatus.setVisibility(View.VISIBLE);
			if (InstantOnManager.isBufferingComplete(contentTileInfo.getId()))
			{
				viewHolder.instantOnStatus.setText(R.string.instantOnItemReady);
				viewHolder.instantOnStatus.setTextColor(parent.getResources().getColor(R.color.blue_light));
			}
			else
			{
				viewHolder.instantOnStatus.setText(R.string.instantOnItemLoading);
				viewHolder.instantOnStatus.setTextColor(parent.getResources().getColor(R.color.log_warn));
			}
		}
		else
		{
			viewHolder.instantOnStatus.setVisibility(View.INVISIBLE);
		}

		// set entitlement icon in content tile.
        if (entitlementManager.requiresAuthorization(contentTileInfo))
        {
            // if user is authorized, show the authorized icon
            if (entitlementManager.isPreauthorized(contentTileInfo))
            {
                viewHolder.entitlement.setImageResource(EntitlementManager.ICON_AUTH);
            }
            else
            {
                viewHolder.entitlement.setImageResource(EntitlementManager.ICON_KEY);
            }

            viewHolder.entitlement.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.entitlement.setVisibility(View.INVISIBLE);
        }

		int bkColor = context.getResources().getColor(R.color.black);
		switch ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)) {
		case Configuration.SCREENLAYOUT_SIZE_XLARGE:
		case Configuration.SCREENLAYOUT_SIZE_LARGE:
			switch (position % 4) {
			case 0:
			case 3:
				bkColor = context.getResources().getColor(R.color.light_grey);
				break;

			case 1:
			case 2:
				bkColor = context.getResources().getColor(R.color.dark_grey);
				break;
			}
			break;

		case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
		case Configuration.SCREENLAYOUT_SIZE_SMALL:
		case Configuration.SCREENLAYOUT_SIZE_NORMAL:
			bkColor = ((position + 1) % 2 == 0) ? context.getResources()
					.getColor(R.color.light_grey) : context.getResources()
					.getColor(R.color.dark_grey);
			break;
		}

		convertView.setBackgroundColor(bkColor);

		return convertView;
	}

}
