package com.unis.sxzcl.widget.menu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.MenuMultipleLayoutBinding;

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

/**
 * 包名: com.unis.sxzcl.widget.menu
 * 类名: MenuMultipleListView
 * 作用描述: 多选菜单列表
 * 创建人: mQ
 * 创建时间: 2020/11/27 10:17
 * 更新人:
 * 更新时间: 2020/11/27 10:17
 * 更新说明:
 * 版本号: 1.0
 */
public class MenuMultipleListView {

    private Context mContext;
    private MenuMultipleLayoutBinding mBinding;
    private MenuMultipleListAdapter mAdapter;
    private Callback mCallback;

    public MenuMultipleListView(Context mContext) {
        this.mContext = mContext;
        init();
    }

    private void init() {
        mBinding = MenuMultipleLayoutBinding.inflate(LayoutInflater.from(mContext));

        mAdapter = new MenuMultipleListAdapter();
        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.list_empty_layout, null));
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        mBinding.recyclerView.setAdapter(mAdapter);

        mBinding.menuEnter.setOnClickListener(v -> {
            //确定
            if (mCallback != null)
                mCallback.onMenuMultipleListener(mBinding.getRoot(), mAdapter.getSelect());
        });
        mBinding.menuClear.setOnClickListener(v -> {
            //清空
            mAdapter.getSelect().clear();
            mAdapter.notifyDataSetChanged();
        });

    }

    public View getView() {
        return mBinding.getRoot();
    }

    public MenuMultipleListView setData(List<String> list) {
        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();
        return this;
    }

    public MenuMultipleListView addData(String item) {
        mAdapter.addData(item);
        return this;
    }


    public MenuMultipleListView setCallback(Callback callback) {
        mCallback = callback;
        return this;
    }

    public interface Callback {
        void onMenuMultipleListener(View root, Set<Integer> positions);
    }
}
