package com.itant.joycity.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.itant.joycity.R;
import com.itant.joycity.bean.Kefu;

import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 * 图片列表适配器
 */
public class QQKefuAdapter extends BaseAdapter {

    private List<Kefu> kefus;
    private Context context;


    public QQKefuAdapter() {

    }

    public QQKefuAdapter(Context context, List<Kefu> kefus) {
        this.kefus = kefus;
        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return kefus == null ? null : kefus.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Kefu kefu = kefus.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_kefu_qq, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_kefu_name = (TextView) convertView.findViewById(R.id.tv_kefu_name);
            viewHolder.tv_kefu_number = (TextView) convertView.findViewById(R.id.tv_kefu_number);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_kefu_name.setText(kefu.getName());
        viewHolder.tv_kefu_number.setText(kefu.getNumber());

        viewHolder.tv_kefu_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击QQ就聊天
                String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=" + kefu.getNumber() + "&version=1";
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));

            }
        });

        return convertView;
    }


    private ViewHolder viewHolder;
    private static class ViewHolder {
        TextView tv_kefu_name;
        TextView tv_kefu_number;
    }
}
