package com.dahe.mylibrary.cuspop;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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.CarAdapter;
import com.dahe.mylibrary.bean.CarBean;
import com.dahe.mylibrary.net.JsonUtils;
import com.lxj.xpopup.core.CenterPopupView;

import org.jetbrains.annotations.NotNull;

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

public class CarSelPopupView extends CenterPopupView {


    private Context context;
    private List<CarBean> data;
    public CarSelPopupView(Context context, String data) {
        super(context);
        this.context = context;
        ArrayList<CarBean> nodeBeans = JsonUtils.getInstance().jsonToList(data, CarBean.class);
        this.data = nodeBeans;
    }

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

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
        ImageView ivCha = (ImageView) findViewById(R.id.ivCha);
        LinearLayout llCha = (LinearLayout) findViewById(R.id.llCha);
        Button btnOK = (Button) findViewById(R.id.btnOK);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context,RecyclerView.VERTICAL,false));
        recyclerView.setHasFixedSize(true);
        CarAdapter nodeAdapter = new CarAdapter(-1,data);
        recyclerView.setAdapter(nodeAdapter);

        tvTitle.setText("关联司机");
        nodeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                CarBean nodeSelectBean = data.get(position);
                nodeSelectBean.setSelect(!nodeSelectBean.isSelect());
                nodeAdapter.notifyItemChanged(position);
            }
        });

        btnOK.setOnClickListener(v -> {
            if (listener!=null){
//                ArrayList<CarBean> temp = new ArrayList<>();
//                for (int i = 0; i < data.size(); i++) {
//                    CarBean nodeSelectBean = data.get(i);
//                    if ("1".equals(nodeSelectBean.isSelect())){
//                        temp.add(nodeSelectBean);
//                    }
//
//                }
//                if (temp.size()<=0){
//                    ToastUtils.showToast(context,"至少存在一个节点");
//                    return;
//                }
                listener.okClick(JsonUtils.getInstance().toJson(nodeAdapter.getData()));
//                temp = null;
                dismiss();
            }
        });
        llCha.setOnClickListener(v -> {
            dismiss();
        });
    }


    private OnOkListener listener;

    public CarSelPopupView setOnOkListener(OnOkListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnOkListener {
        void okClick(String data);
    }
}
