package com.tsi.navi.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.tsi.navi.R;
import com.tsi.navi.activity.model.NodeCrossItem;

import java.util.ArrayList;

public class NodeCrossItemAdapter extends RecyclerView.Adapter<NodeCrossItemAdapter.ViewHolder> implements Filterable {

    private static final String TAG = NodeCrossItemAdapter.class.getSimpleName();

    // FOR interface - start
    public interface OnItemClickListener {
        void onItemClick(View v, int pos, NodeCrossItem item);
    }
    private OnItemClickListener onItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }
    // FOR interface - end.

    private Context context;
    private ArrayList<NodeCrossItem> unFilteredList = new ArrayList<>();
    private ArrayList<NodeCrossItem> filteredList = new ArrayList<>();


    public NodeCrossItemAdapter(Context context, ArrayList<NodeCrossItem> list) {
        super();
        this.context = context;
        this.unFilteredList = list;
        this.filteredList = list;
    }

    @NonNull
    @Override
    public NodeCrossItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_node_cross, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NodeCrossItemAdapter.ViewHolder holder, int position) {
        holder.onBind(filteredList.get(position));
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setItems(ArrayList<NodeCrossItem> list){
        this.unFilteredList = list;
        this.filteredList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return this.filteredList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String findString = constraint.toString();
                if (findString.isEmpty()) {
                    filteredList = unFilteredList;
                }
                else {
                    ArrayList<NodeCrossItem> filteringList = new ArrayList<>();
                    //Log.e(TAG, "nodeCrossItems: " + unFilteredList.size() + " EA. ==> " + findString);
                    for (NodeCrossItem obj : unFilteredList) {
                        if (String.valueOf(obj.getNodeId()).contains(findString)) {
                            //Log.e(TAG, "match id: " + obj.getNodeId());
                            filteringList.add(obj);
                        }
                        else if (obj.getName().contains(findString)) {
                            //Log.e(TAG, "match name: " + obj.getName());
                            filteringList.add(obj);
                        }
                    }
                    filteredList = filteringList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filteredList;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filteredList = (ArrayList<NodeCrossItem>)results.values;
                notifyDataSetChanged();
            }
        };
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgStatus;
        TextView tvId;
        TextView tvName;
        ImageView imgSelect;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            this.imgStatus = (ImageView) itemView.findViewById(R.id.img_status);
            this.tvId = (TextView) itemView.findViewById(R.id.tv_id);
            this.tvName = (TextView) itemView.findViewById(R.id.tv_name);
            this.imgSelect = (ImageView) itemView.findViewById(R.id.img_select);

            this.imgSelect.setOnClickListener(v -> {
                int pos = getBindingAdapterPosition() ;
                if (pos != RecyclerView.NO_POSITION) {
                    NodeCrossItem item = filteredList.get(pos) ;
                    //Log.e(TAG, "selected: " + item.getNodeId());
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, pos, item);
                    }
                }
            });
        }

        void onBind(NodeCrossItem item){
            /*if (item.isOnline()) {
                this.imgStatus.setImageResource(R.drawable.ic_node_normal);
            }
            else {
                this.imgStatus.setImageResource(R.drawable.ic_node_error);
            }*/
            if (item.getType() == 3) {
                this.imgStatus.setImageResource(R.drawable.ic_cross_three);
            }
            else {
                this.imgStatus.setImageResource(R.drawable.ic_cross_four);
            }
            this.imgSelect.setImageResource(R.drawable.ic_location);

            this.tvId.setText(String.valueOf(item.getNodeId()));
            this.tvName.setText(item.getName());
        }
    }
}
