package com.tofly.ghrq_route.common.ui.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.common.ui.view.CircleImageView;
import com.tofly.ghrq_route.module.user.domain.DepartmentUserBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zombiexq on 2016/7/6.
 */
public class UserPhoneAdapter extends ARecyclerBaseAdapter<UserPhoneAdapter.MyViewHolder, DepartmentUserBean> {
    private Context mContext;
    private OnItemPhoneClickListener mItemPhoneClickListener;
    private OnItemMessageClickListener mItemMessageClickListener;

    public UserPhoneAdapter(Context context, List<DepartmentUserBean> userList) {
        super(context, userList);
        this.mContext = context;
    }

    public interface OnItemPhoneClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemMessageClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemPhoneClickListener(OnItemPhoneClickListener listener) {
        mItemPhoneClickListener = listener;
    }

    public void setItemMessageClickListener(OnItemMessageClickListener listener) {
        mItemMessageClickListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext)
                .inflate(R.layout.listview_user_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, final int position) {
        DepartmentUserBean userBean = getItem(position);
        if (userBean != null) {
            viewHolder.textUserName.setText(userBean.getUsername());
            viewHolder.textRealName.setText(userBean.getRealName());
            String avatar = userBean.getAvatar();
            if (avatar != null && !avatar.isEmpty()) {
                String faceurl = NetRequestURL.newApi_dyrq.getFormattedPicture(avatar);
                ImageLoader imageLoader = ImageLoader.getInstance();
                imageLoader.displayImage(faceurl, viewHolder.imageUserIcon);
            }else {
                viewHolder.imageUserIcon.setImageResource(R.mipmap.user_default_icon);
            }

            String userPhone = userBean.getMobile();
            if (userPhone != null && !userPhone.isEmpty()){
                viewHolder.textPhoneNumber.setText(userPhone);
                viewHolder.imagePhone.setEnabled(true);
            }else {
                viewHolder.textPhoneNumber.setText(R.string.no_data);
                viewHolder.imagePhone.setEnabled(false);
            }

            viewHolder.imagePhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemPhoneClickListener != null) {
                        mItemPhoneClickListener.onItemClick(v, position);
                    }
                }
            });

            viewHolder.imageMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemMessageClickListener != null) {
                        mItemMessageClickListener.onItemClick(v, position);
                    }
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_user_icon)
        CircleImageView imageUserIcon;
        @BindView(R.id.text_user_name)
        TextView textUserName;
        @BindView(R.id.text_real_name)
        TextView textRealName;
        @BindView(R.id.text_phone_number)
        TextView textPhoneNumber;
        @BindView(R.id.image_phone)
        ImageView imagePhone;
        @BindView(R.id.image_message)
        ImageView imageMessage;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
