package com.bc_chat.im.activity.envelopes.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bc_chat.bc_base.entity.FriendApplyRecordBean;
import com.bc_chat.im.R;
import com.jrmf360.rylib.rp.widget.RoundImageView;
import com.zhaohaoting.framework.imagepicker.camera.util.LogUtil;
import com.zhaohaoting.framework.utils.glid.GlideUtils;
import com.zhaohaoting.framework.view.recyclerview.BasicRecyclerAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author: hxm
 * Date: 2019/5/8
 * Desc:
 * Version: 1.0
 */
public class FriendApplyRecordAdapter extends BasicRecyclerAdapter<FriendApplyRecordBean> {

    public FriendApplyRecordAdapter(Context context, List<FriendApplyRecordBean> list) {
        super(context, list, R.layout.item_friend_apply_record);
    }

    @Override
    public void convert(ViewHolder holder, FriendApplyRecordBean item, int position) {
        if (item != null) {
            try {
                RoundImageView ivAvatar = holder.getView(R.id.ivAvatar);
                TextView tvName = holder.getView(R.id.tvName);
                TextView tvDate = holder.getView(R.id.tvDate);
                TextView tvAgree = holder.getView(R.id.tvAgree);
                TextView tvRefuse = holder.getView(R.id.tvRefuse);
                TextView tvStatus = holder.getView(R.id.tvStatus);

                GlideUtils.loadCenterCropImage(mContext, item.getPhoto(), ivAvatar);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                long lcc_time = Long.valueOf(item.getC_time());
                tvDate.setText(sdf.format(new Date(lcc_time * 1000L)));

                tvAgree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (friendHandlerListener != null) {
                            friendHandlerListener.onAgree(position);
                        }
                    }
                });

                tvRefuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (friendHandlerListener != null) {
                            friendHandlerListener.onRefuse(position);
                        }
                    }
                });

                tvAgree.setVisibility(View.GONE);
                tvRefuse.setVisibility(View.GONE);
                tvStatus.setVisibility(View.GONE);
                if (item.getType() == 1) {
                    tvName.setText("我申请添加 " + item.getNickname() + " 为好友");
                    //自己添加他人好友
                    if (item.getState() == 1) {
                        tvStatus.setText("好友验证中");
                    } else if (item.getState() == 2) {
                        tvStatus.setText("已同意");
                    } else if (item.getState() == 3) {
                        tvStatus.setText("已拒绝");
                    }
                    tvStatus.setVisibility(View.VISIBLE);
                } else if (item.getType() == 2) {
                    tvName.setText(item.getNickname() + " 申请添加我为好友");
                    //他人添加自己为好友
                    if (item.getState() == 1) {
                        tvAgree.setVisibility(View.VISIBLE);
                        tvRefuse.setVisibility(View.VISIBLE);
                    } else if (item.getState() == 2) {
                        tvStatus.setText("已同意");
                        tvStatus.setVisibility(View.VISIBLE);
                    } else if (item.getState() == 3) {
                        tvStatus.setText("已拒绝");
                        tvStatus.setVisibility(View.VISIBLE);
                    }
                }
            } catch (Exception e) {
                LogUtil.d(e.getMessage());
            }
        }
    }

    public interface IFriendHandlerListener {

        void onRefuse(int position);

        void onAgree(int position);

    }

    public IFriendHandlerListener friendHandlerListener;

    public void setFriendHandlerListener(IFriendHandlerListener friendHandlerListener) {
        this.friendHandlerListener = friendHandlerListener;
    }
}
