/*******************************************************************************
 * ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2014 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 ******************************************************************************/

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

import android.annotation.TargetApi;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.adobe.primetime.reference.utils.StringUtils;
import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.utils.DownloadImageTask;

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

public class MvpdListArrayAdapter extends ArrayAdapter<MvpdListItem>
{
    private static final String LOG_TAG = PrimetimeReference.LOG_APP_NAME + "MvpdListArrayAdapter";

    private int resource;

    private final ArrayList<MvpdListItem> mvpdList = new ArrayList<MvpdListItem>();
    private List<MvpdListItem> filterList;

    public MvpdListArrayAdapter(Context context, int textViewResourceId, List<MvpdListItem> objects)
    {
        super(context, textViewResourceId, objects);

        filterList = objects;
        mvpdList.addAll(filterList);
        resource = textViewResourceId;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewGroup mvpdItemView;

        MvpdListItem mvpdListItem = getItem(position);

        if (convertView == null)
        {
            mvpdItemView = new LinearLayout(getContext());
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(resource, mvpdItemView, true);
        }
        else
        {
            mvpdItemView = (LinearLayout) convertView;
        }

        View mvpd = mvpdItemView.findViewById(R.id.mvpd);
        if (mvpd instanceof TextView)
        {
            TextView mvpdNameTv = (TextView) mvpd;
            mvpdNameTv.setText(mvpdListItem.getMvpd().getDisplayName());
        }
        else if (mvpd instanceof ImageView)
        {
            ImageView mvpdLogo = (ImageView) mvpd;
            mvpdLogo.setContentDescription(mvpdListItem.getMvpd().getDisplayName());

            if (!StringUtils.isEmpty(mvpdListItem.getMvpd().getLogoUrl()))
            {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                {
                    new DownloadImageTask(mvpdLogo).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mvpdListItem.getMvpd().getLogoUrl());
                }
                else
                {
                    new DownloadImageTask(mvpdLogo).execute(mvpdListItem.getMvpd().getLogoUrl());
                }
            }
        }

        return mvpdItemView;
    }

    public void filter(String text)
    {
        text = text.toLowerCase(Locale.US);

        filterList.clear();
        if (StringUtils.isEmpty(text))
        {
            filterList.addAll(mvpdList);
        }
        else
        {
            for (MvpdListItem item : mvpdList)
            {
                if (item.getMvpd().getDisplayName().toLowerCase(Locale.US).contains(text))
                {
                    filterList.add(item);
                }
            }
        }

        notifyDataSetChanged();
    }
}
