package com.mq.mylibrary.popupwindow.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mq.mylibrary.R;
import com.mq.mylibrary.popupwindow.SpinnerOption;

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

/**
 * 创建人 mQ
 * 创建时间 2019/6/21 0021 16:57
 * 文件名称 FlexSelAdapter
 * 说明:  流式布局选择适配器
 **/
public class FlexSelAdapter extends BaseQuickAdapter<SpinnerOption, BaseViewHolder> {

    private Set<SpinnerOption> mSelectList = new HashSet<>();
    private SelectListener mSelectListener;

    public FlexSelAdapter(@Nullable List<SpinnerOption> data) {
        super(R.layout.pop_flex_spinner_item, data);
    }

    public interface SelectListener {
        public void onSelectListener(Set<SpinnerOption> selectList);
    }

    public void setSelectListener(SelectListener selectListener) {
        mSelectListener = selectListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, SpinnerOption item) {
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.name);
        if (mSelectList != null && mSelectList.contains(item)) {
            tvName.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            tvName.setTextColor(ContextCompat.getColor(mContext, R.color.colorMainTextTab));
        }
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectList == null)
                    mSelectList = new HashSet<>();
                mSelectList.clear();
                mSelectList.add(item);
                if (mSelectListener != null) {
                    mSelectListener.onSelectListener(mSelectList);
                }
            }
        });
    }

    public Set<SpinnerOption> getSelectList() {
        return mSelectList;
    }
}
