package com.ccwangluo.im.adapter;


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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ccwangluo.im.R;
import com.ccwangluo.im.listener.ChannelClickListener;

import java.util.List;


public class ChannelAdapter extends BaseAdapter {

    private List<Long> dataList;

    private Context context;

    private ChannelClickListener channelClickListener;

    public ChannelAdapter(List<Long> dataList, Context context, ChannelClickListener channelClickListener) {
        this.dataList = dataList;
        this.context = context;
        this.channelClickListener = channelClickListener;
    }

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder {
        public TextView msg_content;

    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.msg_row_item, null, true);
            mHolder.msg_content = convertView.findViewById(R.id.msg_content);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.msg_content.setText(dataList.get(position).toString());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                channelClickListener.itemClick(dataList.get(position));
            }
        });
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                channelClickListener.itemLongClick(dataList.get(position));
                return false;
            }
        });
        return convertView;
    }

    public void refreshData() {
        notifyDataSetChanged();
    }
}

