package ua.com.team5.wikiaapp.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import ua.com.team5.wikiaapp.R;
import ua.com.team5.wikiaapp.core.WikiaDetails;
import ua.com.team5.wikiaapp.core.WikiaItem;

/**
 * Created by dw on 9/14/2015.
 */
public class WikiaListAdapter extends BaseAdapter {

    private final Context context;
    private final DetailsProvider detailsProvider;
    private final WebImageLoader webImageLoader;
    private final LayoutInflater inflater;

    private List<WikiaItem> items;

    WikiaListAdapter(Context context, DetailsProvider detailsProvider,
                     WebImageLoader webImageLoader, List<WikiaItem> items) {
        this.context = context;
        this.detailsProvider = detailsProvider;
        this.webImageLoader = webImageLoader;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.items = items;
    }

    @Override
    public int getCount() {
        return items != null ? items.size(): 0;
    }

    @Override
    public WikiaItem getItem(int position) {
        return items != null ? items.get(position): null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void swapItems(List<WikiaItem> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.wikia_list_item, parent, false);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.thumbView = (ImageView) convertView.findViewById(R.id.image_thumb);
            viewHolder.textTitle = (TextView) convertView.findViewById(R.id.text_title);
            viewHolder.textUrl = (TextView) convertView.findViewById(R.id.text_url);

            convertView.setTag(viewHolder);
        }

        WikiaItem item = getItem(position);

        ViewHolder viewHolder = (ViewHolder) convertView.getTag();

        if (item != null) {
            viewHolder.id = item.getId();

            viewHolder.textTitle.setText(item.getName());
            viewHolder.thumbView.setImageBitmap(null);

            WikiaDetails details = detailsProvider.getDetails(item.getId());
            if (details != null) {
                viewHolder.textUrl.setText(details.getUrl());
                webImageLoader.displayImage(details.getImage(), viewHolder.thumbView);
            } else {
                viewHolder.textUrl.setText(null);
                detailsProvider.requestDetails(item.getId(), 50, 50, new DetailsProviderListener(item.getId(), viewHolder));
            }
        }

        return convertView;
    }

    private static class ViewHolder {
        ImageView thumbView;
        TextView textTitle;
        TextView textUrl;
        long id;
    }

    private class DetailsProviderListener implements DetailsProvider.LoadDetailsListener {

        private final long id;
        private final ViewHolder viewHolder;

        DetailsProviderListener(long id, ViewHolder viewHolder) {
            this.id = id;
            this.viewHolder = viewHolder;
        }

        @Override
        public void onLoaded(WikiaDetails details) {
            if (id == viewHolder.id) {
                viewHolder.textUrl.setText(details.getUrl());
                webImageLoader.displayImage(details.getImage(), viewHolder.thumbView);
            }
        }
    }
}
