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.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.AreaCrossItem;

import java.util.ArrayList;

public class AreaCrossItemAdapter extends RecyclerView.Adapter<AreaCrossItemAdapter.ViewHolder> {

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

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

    private Context context;
    private ArrayList<AreaCrossItem> lists = new ArrayList<>();


    public AreaCrossItemAdapter(Context context, ArrayList<AreaCrossItem> list) {
        super();
        this.context = context;
        this.lists = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull AreaCrossItemAdapter.ViewHolder holder, int position) {
        if (lists != null) {
            holder.onBind(lists.get(position));
        }
    }

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

    @Override
    public int getItemCount() {
        if (this.lists != null) return this.lists.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvDir;
        TextView tvDistance;
        ImageView imgSelect;

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

            this.tvName = (TextView) itemView.findViewById(R.id.tv_name);
            this.tvDir = (TextView) itemView.findViewById(R.id.tv_dir);
            this.tvDistance = (TextView) itemView.findViewById(R.id.tv_distance);
            this.imgSelect = (ImageView) itemView.findViewById(R.id.img_select);

            this.imgSelect.setOnClickListener(v -> {
                int pos = getBindingAdapterPosition() ;
                if (pos != RecyclerView.NO_POSITION) {
                    AreaCrossItem item = lists.get(pos) ;
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, pos, item);
                    }
                }
            });
        }

        @SuppressLint("SetTextI18n")
        void onBind(AreaCrossItem item){
            this.imgSelect.setImageResource(R.drawable.ic_cross_signal);
            this.tvName.setText(item.getName().trim() + item.getIsReal());
            this.tvDir.setText(item.getDirCode());
            this.tvDistance.setText(String.valueOf(item.getDistance()));
        }
    }
}
