package com.tenlionsoft.baselib.core.widget.views;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.ConditionBean;
import com.tenlionsoft.baselib.core.widget.base.ConditionAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import razerdp.basepopup.BasePopupWindow;

/**
 * 作者: adam
 * 日期: 2021/9/30 - 2:58 下午
 * 邮箱: itgaojian@163.com
 * 描述: 条件选择
 */
public class ConditionPopup extends BasePopupWindow {
    private ConditionAdapter mAdapter;
    private List<ConditionBean> mDatas;

    public ConditionPopup(Context context, List<ConditionBean> datas) {
        super(context);
        View view = createPopupById(R.layout.popup_condition_layout);
        RecyclerView rlvCondition = view.findViewById(R.id.rlv_conditions);
        if (datas == null) {
            mDatas = new ArrayList<>();
        } else {
            mDatas = datas;
        }
        mAdapter = new ConditionAdapter(context, mDatas);
        rlvCondition.setLayoutManager(new LinearLayoutManager(context));
        rlvCondition.setAdapter(mAdapter);
        setContentView(view);

        setBackgroundColor(Color.parseColor("#11000000"));
        setPopupGravity(Gravity.BOTTOM | Gravity.CENTER);
        mAdapter.addOnitemCheckChange((bean, isCheck, pos) -> {
            if (isCheck) {
                mAdapter.mSelBeans.clear();
                mAdapter.mSelBeans.add(bean);
            } else {
                mAdapter.mSelBeans.remove(bean);
            }
            bean.setCheck(isCheck);
            mChangeListener.onChecked(bean, isCheck);
            if (isCheck) {
                if (true) {
                    for (int i = 0; i < mAdapter.getData().size(); i++) {
                        if (!mAdapter.getData().get(i).getId().equals(bean.getId())) {
                            mAdapter.getData().get(i).setCheck(false);
                        }
                    }
                }
            }
            mAdapter.notifyDataSetChanged();
        });
    }

    private OnCheckChangeListener mChangeListener;

    public void addOnCheckChange(OnCheckChangeListener listener) {
        this.mChangeListener = listener;
    }

    public interface OnCheckChangeListener {
        void onChecked(ConditionBean bean, boolean ischeck);
    }
}
