package com.dahe.mylibrary.cuspop;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dahe.mylibrary.R;
import com.dahe.mylibrary.adapter.CenterSimAdapter;
import com.dahe.mylibrary.bean.StateBean;
import com.dahe.mylibrary.wapper.SelectSimAdapter;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.lxj.xpopup.impl.PartShadowPopupView;

import java.util.List;

/**
 * Description: 自定义局部阴影弹窗
 * Create by dance, at 2018/12/21
 */
public class CenterPopupView extends PartShadowPopupView {
    private Context ctx;
    private RecyclerView recyclerView;
    private List<StateBean> wordList;
    private CenterSimAdapter adapter;
    private StateBean selectString;

    public CenterPopupView(@NonNull Activity context, List<StateBean> wordList, StateBean selectString) {
        super(context);
        this.wordList= wordList;
        this.selectString = selectString;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_part_shadow_popup;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(ctx, 4);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, 20, false));
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new CenterSimAdapter(-1, wordList,selectString);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (listener!=null){
                    listener.selectChange(wordList.get(position),position);
                }
                ((CenterSimAdapter)adapter).setSelect(wordList.get(position));
                adapter.notifyDataSetChanged();
                dismiss();
            }
        });

    }

    @Override
    protected void onShow() {
        super.onShow();
        Log.e("tag", "CustomPartShadowPopupView onShow");
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        Log.e("tag", "CustomPartShadowPopupView onDismiss");
    }


    private OnSelectChangeListener listener;
    public CenterPopupView setOnSelectChangeListener(OnSelectChangeListener listener){
        this.listener = listener;
        return this;
    }
    public interface OnSelectChangeListener{
        void selectChange(StateBean item ,int postion);
    }
}
