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

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.mgq.base.utils.customview.CircleImageView;
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.module.user.domain.DepartmentUserBean;

import java.util.List;

/**
 * Created by Amu on 2017/5/22.
 */

public class SingleUserListAdapter extends ArrayAdapter<DepartmentUserBean> {
    private Context mContext;

    private List<DepartmentUserBean> mData;

    private int selectedIndex = -1;

    protected ARecyclerBaseAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(ARecyclerBaseAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SingleUserListAdapter(Context context, List<DepartmentUserBean> data) {
        super(context, R.layout.my_simple_spinner_item, data);
        mContext = context;
        mData = data;
    }

    public void setSelection(int position) {
        if (position >= 0 && position < mData.size()) {
            selectedIndex = position;
        }
    }

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

    @Override
    public DepartmentUserBean getItem(int position) {
        return mData.get(position);
    }

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

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MyViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.listview_user_item_1, null);
            viewHolder = new MyViewHolder();
            viewHolder.layoutRoot = convertView.findViewById(R.id.layout_user_select);
            viewHolder.imageUserIcon = convertView.findViewById(R.id.image_user_icon);
            viewHolder.textUserName = convertView.findViewById(R.id.text_user_name);
            viewHolder.textRealName = convertView.findViewById(R.id.text_real_name);
            viewHolder.checkText = convertView.findViewById(R.id.check_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }

        if (position % 2 == 1) {
            viewHolder.layoutRoot.setBackgroundResource(R.color.scada_shadow);
        } else {
            viewHolder.layoutRoot.setBackgroundResource(R.color.white);
        }

        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);
            }
        }

        viewHolder.checkText.setChecked(position == selectedIndex);
        return convertView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.my_simple_spinner_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textUserName = convertView.findViewById(R.id.text1);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        DepartmentUserBean userBean = getItem(position);
        if (userBean != null) {
            viewHolder.textUserName.setText(userBean.getRealName());
        }
        return convertView;
    }

    class MyViewHolder {
        CircleImageView imageUserIcon;
        TextView textUserName;
        TextView textRealName;
        CheckedTextView checkText;
        LinearLayout layoutRoot;
    }

    class ViewHolder {
        TextView textUserName;
    }
}
