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.NodeAdapter;
import com.dahe.mylibrary.bean.NodeBean;
import com.dahe.mylibrary.net.JsonUtils;
import com.dahe.mylibrary.utils.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;

import org.jetbrains.annotations.NotNull;

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

public class NodePopupView extends CenterPopupView {


    private Context context;
    private List<NodeBean> data;
    public NodePopupView(Context context,String data) {
        super(context);
        this.context = context;
        ArrayList<NodeBean> nodeBeans = JsonUtils.getInstance().jsonToList(data, NodeBean.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);
        NodeAdapter nodeAdapter = new NodeAdapter(-1,data);
        recyclerView.setAdapter(nodeAdapter);


        nodeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                NodeBean nodeSelectBean = data.get(position);
                if (!"0".equals(nodeSelectBean.getState())){
                    ToastUtils.showToast(context,"该节点不可操作");
                    return;
                }
                nodeSelectBean.setSelect("1".equals(nodeSelectBean.isSelect())?"0":"1");
                nodeAdapter.notifyItemChanged(position);
            }
        });

        btnOK.setOnClickListener(v -> {
            if (listener!=null){
                ArrayList<NodeBean> temp = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    NodeBean 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(data));
                temp = null;
                dismiss();
            }
        });
        llCha.setOnClickListener(v -> {
            dismiss();
        });
    }


    private OnOkListener listener;

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

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