package com.mq.mylibrary.popupwindow;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.mq.mylibrary.R;

/**
 * 创建人 mQ
 * 创建时间 2019/6/20 0020 16:20
 * 文件名称 ListPopupwindow
 * 说明:
 **/
public class ListPopupwindow extends BasePopupWindow {

    private RecyclerView mRecyclerView;
    private BaseQuickAdapter mAdapter;
    private boolean isFlex;
    private int spanCount;

    public ListPopupwindow(Context context, @NonNull BaseQuickAdapter adapter) {
        super(context);
        mAdapter = adapter;
        isFlex = true;
        initPopupWindow(context);
    }

    public ListPopupwindow(Context context, int spanCount, @NonNull BaseQuickAdapter adapter) {
        super(context);
        mAdapter = adapter;
        this.spanCount = spanCount;
        initPopupWindow(context);
    }

    @Override
    protected View creatView() {
        mRecyclerView = new RecyclerView(mContext);
        mRecyclerView.setAdapter(mAdapter);
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_empty, null);
        mAdapter.setEmptyView(view);

        if (isFlex) {
            FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(mContext);
            layoutManager.setFlexWrap(FlexWrap.WRAP);
            layoutManager.setFlexDirection(FlexDirection.ROW);
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
            mRecyclerView.setLayoutManager(layoutManager);
        } else {
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, spanCount));
            mAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    try {
                        SpinnerOption option = (SpinnerOption) mAdapter.getData().get(position);
                        switch (option.itemType) {
                            case SpinnerOption.match_parent:
                                return spanCount;
                            case SpinnerOption.wrap_content:
                                return 1;
                        }
                    } catch (Exception e) {
                    }
                    return 1;
                }
            });
        }
        return mRecyclerView;
    }


}
