package com.sjkj.base_lib.camera.dh.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.sjkj.base_lib.R;
import com.sjkj.base_lib.camera.dh.bean.CameraBean;
import com.sjkj.base_lib.utils.AppTools;

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

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder> {
    protected Context context;
    private List<CameraBean> showList = new ArrayList<>();
    protected List<CameraBean> list;

    public DeviceAdapter(Context context) {
        this.context = context;
    }

    public void refreshList(List<CameraBean> list) {
        this.list = list;
        fillter("");
        notifyDataSetChanged();
    }

    private OnDeviceSelectListener onDeviceSelectListener;

    public void setOnDeviceSelectListener(OnDeviceSelectListener onDeviceSelectListener) {
        this.onDeviceSelectListener = onDeviceSelectListener;
    }

    public void fillter(String query) {
        showList.clear();
        query = query.toLowerCase(Locale.getDefault());
        if (query.length() == 0) {
            showList.addAll(list);
        } else {
            for (CameraBean item : list) {
                if (item.getCameraName().toLowerCase(Locale.getDefault()).contains(query)) {
                    showList.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(AppTools.isGhb() ? R.layout.item_device_list_layout_big : R.layout.item_device_list_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (showList != null && showList.size() > 0) {
            if (showList.get(position).isCheck()) {
                holder.cb.setChecked(true);
            } else {
                holder.cb.setChecked(false);
            }
        }
        holder.nameTv.setText(showList.get(position).getCameraName());
        holder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showList.get(position).isCheck()) {
                    showList.get(position).setCheck(false);
                    if (onDeviceSelectListener != null) {
                        onDeviceSelectListener.onSelectUnCheckListener(showList.get(position));
                        if (list != null && list.size() > 0) {
                            for (int i = 0; i < list.size(); i++) {
                                if (TextUtils.equals(list.get(i).getId(), showList.get(position).getId())) {
                                    list.get(i).setCheck(false);
                                }
                            }
                        }
                    }
                } else {
//                    if (list != null && list.size() > 0) {
//                        for (int i = 0; i < list.size(); i++) {
//                            list.get(i).setCheck(false);
//                        }
//                    }
                    showList.get(position).setCheck(true);
                    if (onDeviceSelectListener != null) {
                        onDeviceSelectListener.onSelectCheckListener(showList.get(position));
                        if (list != null && list.size() > 0) {
                            for (int i = 0; i < list.size(); i++) {
                                if (TextUtils.equals(list.get(i).getId(), showList.get(position).getId())) {
                                    list.get(i).setCheck(true);
                                }
                            }
                        }
                    }
                }

                notifyDataSetChanged();
            }
        });
        if (showList != null && showList.size() > 0) {
            if (position == showList.size() - 1) {
                holder.divider.setVisibility(View.INVISIBLE);
            } else {
                holder.divider.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return showList == null ? 0 : showList.size();
    }

    public interface OnDeviceSelectListener {
        void onSelectCheckListener(CameraBean bean);

        void onSelectUnCheckListener(CameraBean bean);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private CheckBox cb;
        private TextView nameTv;
        private LinearLayout itemLayout;
        private View divider;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cb = itemView.findViewById(R.id.cb);
            nameTv = itemView.findViewById(R.id.name_tv);
            itemLayout = itemView.findViewById(R.id.item_layout);
            divider = itemView.findViewById(R.id.divider);

        }
    }
}
