package com.tsi.navi.activity.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tsi.navi.R;
import com.tsi.navi.activity.adapter.AreaCrossItemAdapter;
import com.tsi.navi.activity.model.AreaCrossItem;

import java.util.ArrayList;

public class AreaCrossFragment extends Fragment {
    private static final String TAG = AreaCrossFragment.class.getSimpleName();

    private Context context;

    private RecyclerView recyclerView;
    private AreaCrossItemAdapter areaCrossItemAdapter;

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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_area_cross, container, false);
        this.areaCrossItemAdapter = new AreaCrossItemAdapter(context, null);
        this.recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
        this.recyclerView.setAdapter(this.areaCrossItemAdapter);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(context));

        this.areaCrossItemAdapter.setOnItemClickListener(new AreaCrossItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos, AreaCrossItem item) {
                if (item != null) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(item);
                    }
                }
            }
        });
        return v;
    }

    public void update(ArrayList<AreaCrossItem> list) {
        this.areaCrossItemAdapter.setItems(list);
    }

}
