package io.openim.android.ouicontact.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;

import io.openim.android.ouicore.adapter.RecyclerViewAdapter;
import io.openim.android.ouicore.adapter.ViewHol;
import io.openim.android.ouicore.entity.ExUserInfo;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.sdk.models.FriendInfo;

public class ContactAdapter extends RecyclerViewAdapter<ExUserInfo, RecyclerView.ViewHolder> {
    private Context mContext;

    public ContactAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHol.ItemViewHo(parent);
    }

    @Override
    public void onBindView(@NonNull RecyclerView.ViewHolder holder, ExUserInfo data, int position) {
        ViewHol.ItemViewHo itemViewHo = (ViewHol.ItemViewHo) holder;
        FriendInfo friendInfo = data.userInfo.getFriendInfo();
        String nickName = friendInfo.getNickname();
        String remark = friendInfo.getRemark();
        String str = StringUtils.isEmpty(remark) ? nickName : remark;
        itemViewHo.view.avatar.load(friendInfo.getFaceURL(),false,friendInfo.getNickname());
        itemViewHo.view.nickName.setText(str);
        itemViewHo.view.getRoot().setOnClickListener(v -> {
            ARouter.getInstance().build(Routes.Main.PERSON_DETAIL)
                    .withString(Constants.K_ID, friendInfo.getUserID())
                    .navigation();
        });
    }
}
