package com.ccwangluo.im.adapter;


import android.content.Context;
import android.content.Intent;
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.ccim.modle.Contact;
import com.ccwangluo.ccim.modle.Dialog;
import com.ccwangluo.im.R;
import com.ccwangluo.im.ui.dashboard.ConnectActivity;


import java.util.List;

public class DialogAdapter extends BaseAdapter {

    private List<Dialog> dataList;

    private Context context;

    public DialogAdapter(List<Dialog> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }


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

    }

    @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 childrenHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = LayoutInflater.from(context).inflate(R.layout.text_row_item, null);
            childrenHolder =new ViewHolder();
            childrenHolder.contactId = (TextView) convertView.findViewById(R.id.contactId);
            childrenHolder.contactName = (TextView) convertView.findViewById(R.id.contactName);
            childrenHolder.netStatus = (TextView) convertView.findViewById(R.id.netStatus);
            childrenHolder.status = (TextView) convertView.findViewById(R.id.status);
            childrenHolder.unReadMsg = (TextView) convertView.findViewById(R.id.unReadMsg);
            convertView.setTag(childrenHolder);
        } else {
            childrenHolder = (ViewHolder) convertView.getTag();
        }
        Dialog dialog = dataList.get(position);
        childrenHolder.contactId.setText("userid: " + dataList.get(position).getContactUserId());
        childrenHolder.contactName.setText("username: " + dataList.get(position).getContactUserName());
        childrenHolder.netStatus.setText("netStatus: " + dataList.get(position).getNetStatus().toString());
//        childrenHolder.status.setText("status: " + dataList.get(childPosition).);
        childrenHolder.unReadMsg.setText("unReadMsg : " + dataList.get(position).getUnreadCount().toString());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ConnectActivity.class);
                Contact contact = new Contact();
                contact.setContactUserId(dialog.getContactUserId());
                contact.setNetStatus(dialog.getNetStatus());
                contact.setContactUserName(dialog.getContactUserName());
                intent.putExtra("contact", contact);
                context.startActivity(intent);
                dialog.setUnreadCount(0L);
            }
        });
        return convertView;
    }

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

