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.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class MultiUserListAdapter extends ARecyclerBaseAdapter<MultiUserListAdapter.MyViewHolder, DepartmentUserBean> {

    private Set<Integer> selectedPostions = new HashSet<>();

    public MultiUserListAdapter(Context context, List<DepartmentUserBean> data) {
        super(context, data);
    }

    public void toggleSelection(int position) {
        if (selectedPostions.contains(position)) {
            selectedPostions.remove(position);
        } else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

    public List<DepartmentUserBean> getSelectedItems() {
        List<DepartmentUserBean> selectedItems = new ArrayList<>();
        for (int postion : selectedPostions) {
            selectedItems.add(getItem(postion));
        }
        return selectedItems;
    }

    public void allSelected() {
        selectedPostions.clear();
        for (int i = 0; i < mList.size(); i++) {
            selectedPostions.add(i);
        }
        this.notifyDataSetChanged();
    }

    public void cancelSelected() {
        selectedPostions.clear();
        this.notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        DepartmentUserBean userBean = getItem(position);
        holder.setData(userBean, 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.check_text)
        CheckedTextView checkText;
        @BindView(R.id.layout_user_select)
        LinearLayout layoutUserSelect;

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

        public void setData(final DepartmentUserBean userBean, final int position) {
            if (position % 2 == 1) {
                this.itemView.setBackgroundResource(R.color.scada_shadow);
            } else {
                this.itemView.setBackgroundResource(R.color.white);
            }

            if (userBean != null) {
                textUserName.setText(userBean.getUsername());
                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, imageUserIcon);
                }else {
                    imageUserIcon.setImageResource(R.mipmap.user_default_icon);
                }
            }

            if (selectedPostions.contains(position)){
                this.checkText.setChecked(true);
            }else {
                this.checkText.setChecked(false);
            }

            this.layoutUserSelect.setOnClickListener(view ->  {
                int adapterPostion = getAdapterPosition();
                toggleSelection(adapterPostion);
            });
        }
    }
}
