package com.dahe.mylibrary.cuspop;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
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.SelectDicAdapter;
import com.dahe.mylibrary.bean.DictBean;
import com.dahe.mylibrary.bean.StateBean;
import com.lxj.xpopup.core.AttachPopupView;

import java.util.List;

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

    public CustomDicPopView(@NonNull Context context, List<StateBean> wordList, int selectPos) {
        super(context);
        this.ctx = context;
        this.wordList= wordList;
        this.selectPos = selectPos;
    }

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


    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ctx, RecyclerView.VERTICAL, false);
//        recyclerView.addItemDecoration(new RecycleViewDivider(ctx,RecyclerView.VERTICAL));
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new SelectDicAdapter(-1, wordList,selectPos);
        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);
                }
                ((SelectDicAdapter)adapter).setSelect(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 CustomDicPopView setOnSelectChangeListener(OnSelectChangeListener listener){
        this.listener = listener;
        return this;
    }
    public interface OnSelectChangeListener{
        void selectChange(StateBean item ,int postion);
    }
}
