package kz.arta.synergy.workflow.mobile.android.attachments.adapters;

import android.app.Activity;
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 kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.attachments.models.AttachmentItem;
import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.common.utils.ImageLoader;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;

/**
 * Created by neox on 08.04.14.
 * Адаптер для списка вложений
 */
public class AdapterAttachments extends BaseAdapter {

    private List<AttachmentItem> attachmentItemAttachments;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private Activity activity;
    private Person person;

    public AdapterAttachments(Activity activity, List<AttachmentItem> attachmentItemAttachments, Person person) {
        this.attachmentItemAttachments = attachmentItemAttachments;
        this.person = person;
        this.activity = activity;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext(), person);
    }

    public int getCount() {
        return attachmentItemAttachments.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

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

        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.item_attachment, null);
        }

        TextView nameAttachment = (TextView) vi.findViewById(R.id.text_attach);
        nameAttachment.setTypeface(Fonts.getMedium(activity));
        nameAttachment.setText(attachmentItemAttachments.get(position).getName());

        ImageView image = (ImageView) vi.findViewById(R.id.image_attach);
        try {
            imageLoader.displayImage(Utils.parseUrlToIcon(person.getHost() + attachmentItemAttachments.get(position).getIcon()), image);
        } catch (Exception ignored) {
        }

        if (position == attachmentItemAttachments.size() - 1) {
            View separator = vi.findViewById(R.id.separator_attachment_item);
            separator.setVisibility(View.GONE);
        }

        return vi;
    }
}