/*
 * Copyright (c) 2016, Aegir Labs LLC. All rights reserved.
 */

package com.aegir.demo.fragments;

import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.aegir.demo.model.BaseFloorItem;
import com.aegir.demo.R;
import com.aegir.demo.adapters.FloorItemAdapter;
import com.aegir.demo.model.FloorItem;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFloorFragmentInteractionListener} interface
 * to handle interaction events.
 *
 * This fragment displays the floors available to select
 */
public class FloorSelectorFragment extends Fragment {

    // region Fields

    private List<BaseFloorItem> floors;
    private OnFloorFragmentInteractionListener listener;

    // endregion

    /**
     * Default initializer
     */
    public FloorSelectorFragment() {
        // Required empty public constructor
    }

    // region Public Methods

    /**
     * Set the adapter for the List View and set up listeners for item clicks
     */
    public void setAdapter(List<BaseFloorItem> providedFloors) {
        floors = providedFloors;
        final ListView listView = getView().findViewById(R.id.lv_floors);
        FloorItemAdapter itemAdapter = new FloorItemAdapter(getActivity(), floors, floorItem -> {
            listener.onFloorInfoClickListener(floorItem.getKey());
        });
        listView.setAdapter(itemAdapter);
        itemAdapter.notifyDataSetChanged();

        listView.setOnItemClickListener((arg0, view, position, arg3) -> {
            if (view.isSelected()) {
                view.setSelected(false);
            } else {
                view.setSelected(true);
            }
            if (listener != null && !(floors.get(position).isBuilding())) {
                listener.onFloorFragmentInteraction((FloorItem) floors.get(position));
            }
        });
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     *
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFloorFragmentInteractionListener {
        void onFloorFragmentInteraction(FloorItem floor);
        void onFloorInfoClickListener(String itemKey);
    }

    /**
     *
     * @return a list of all the floors
     */
    public List<BaseFloorItem> getFloors() {
        return floors;
    }

    /**
     * Called when the fragmentis getting created
     * @param inflater the inflater
     * @param container the parent view
     * @param savedInstanceState the saved instance if any
     * @return newly inflated View for this fragment
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_floor_selector, container, false);
    }

    /**
     * Called when the fragment is attached to the activity's view
     * @param context the activity context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFloorFragmentInteractionListener) {
            listener = (OnFloorFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFloorFragmentInteractionListener");
        }
    }

    /**
     * Called when the fragment is removed from the activity's view
     */
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    // endregion
}
