package com.unis.sxzcl.widget.menu;


import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.sxzcl.R;

import java.util.HashSet;
import java.util.Set;

/**
 * 包名: com.unis.sxzcl.widget.menu
 * 类名: MenuMultipleListAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/27 10:24
 * 更新人:
 * 更新时间: 2020/11/27 10:24
 * 更新说明:
 * 版本号: 1.0
 */
public class MenuMultipleListAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

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

    public MenuMultipleListAdapter() {
        super(R.layout.menu_multiple_list_item_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.tv_content, item);
        int position = helper.getLayoutPosition();
        helper.itemView.setTag(position);
        helper.itemView.setOnClickListener(v -> {
            int index = (int) v.getTag();
            fillValue(helper, !mSelect.contains(index));
            if (mSelect.contains(index)) {
                mSelect.remove(position);
            } else {
                mSelect.add(position);
            }
        });

        fillValue(helper, mSelect.contains(position));
    }

    private void fillValue(BaseViewHolder helper, boolean isSel) {
        if (isSel) {
            helper.setTextColor(R.id.tv_content, ContextCompat.getColor(getContext(), R.color.colorWhite));
            helper.setBackgroundResource(R.id.tv_content, R.drawable.accent_radius_bg);
        } else {
            helper.setTextColor(R.id.tv_content, ContextCompat.getColor(getContext(), R.color.colorText1));
            helper.setBackgroundResource(R.id.tv_content, R.drawable.accent_radius_stroke_bg);
        }
    }

    public Set<Integer> getSelect() {
        return mSelect;
    }
}
