package com.mq.mylibrary.popupwindow.adapter;

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

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 17:41
 * 文件名称 SelAdapter
 * 说明:  选择适配器
 **/
public class SelAdapter extends BaseQuickAdapter<SpinnerOption, BaseViewHolder> {

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

    private boolean isShowCheck = true;

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

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

    public SelAdapter setSelectListener(SelectListener selectListener) {
        mSelectListener = selectListener;
        return this;
    }

    @Override
    protected void convert(BaseViewHolder helper, SpinnerOption item) {
        if (item.logo == 0) {
            helper.setGone(R.id.iv_logo, false);
        } else {
            helper.setGone(R.id.iv_logo, true);
            helper.setImageResource(R.id.iv_logo, item.logo);
        }
        helper.setText(R.id.tv_name, item.name);
        if (mSelectList != null && mSelectList.contains(item)) {
            helper.setTextColor(R.id.tv_name, ContextCompat.getColor(mContext, R.color.colorPrimary))
                    .setImageResource(R.id.iv_img, R.drawable.ic_radio_button_checked_black_24dp);
        } else {
            helper.setTextColor(R.id.tv_name, ContextCompat.getColor(mContext, R.color.colorMainTextTab))
                    .setImageResource(R.id.iv_img, R.drawable.ic_radio_button_unchecked_black_24dp);
        }
        helper.itemView.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(mView, mSelectList);
                }
            }
        });
        helper.setGone(R.id.iv_img, isShowCheck);
    }

    public void setView(View view) {
        mView = view;
    }

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

    public SelAdapter setHideSelView() {
        isShowCheck = false;
        return this;
    }
}
