/*
 * 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.AdapterView;
import android.widget.ListView;

import com.aegir.demo.MapActivity;
import com.aegir.demo.R;
import com.aegir.demo.adapters.ItemAdapter;
import com.aegir.vmms.wayfinding.model.MapDirectionStep;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link TextDirectionsFragment.OnTextDirectionsFragmentInteractionListener} interface
 * to handle interaction events.
 *
 * This fragment displays a list of text directions and receives user interaction with the list view
 */
public class TextDirectionsFragment extends Fragment implements View.OnClickListener {

    // region Fields

    private OnTextDirectionsFragmentInteractionListener listener;
    public List<MapDirectionStep> directions;
    private MapActivity mapActivity;

    // endregion

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

    // region Public Methods

    /**
     * Set the adapter with directions stored in our DataManager object
     */
    public void setAdapter(List<MapDirectionStep> directions) {
        if (directions != null && !directions.isEmpty()) {

            final ListView listView = (ListView)getView().findViewById(R.id.lv_directions);
            ItemAdapter itemAdapter = new ItemAdapter(getActivity(), directions);
            listView.setAdapter(itemAdapter);
            itemAdapter.notifyDataSetChanged();

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3)
                {

                    if (listener != null) {
                        if (!view.isSelected()) {
                            view.setSelected(true);
                            listener.onTextDirectionsFragmentInteraction(position);
                        }
                    }
                }
            });
        }
    }

    /**
     * Called to create the view for the fragment
     * @param inflater the view inflater
     * @param container the view container
     * @param savedInstanceState the saved state if any
     * @return the view
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.fragment_text_directions, container, false);
        mapActivity = (MapActivity)getActivity();

        return v;
    }

    /**
     * Animate the fragment to cover the screen
     * @param view the view that was clicked
     */
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_show_directions) {
            TextDirectionsFragment textDirectionsFragment = (TextDirectionsFragment) getFragmentManager().findFragmentById(R.id.frag_text_directions);
            //btnStartDirections.bringToFront();
            //btnStartDirections.animate().translationX(0);
            float yPosition = -(mapActivity.getApplicationContext().getResources().getDisplayMetrics().heightPixels);
            float height = (yPosition * -1 + 60) / textDirectionsFragment.getView().getHeight();
            textDirectionsFragment.getView().animate().y(yPosition).setDuration(4000)
                    .withEndAction(new Runnable() {
                        @Override
                        public void run() {
                        }
                    });
        }
    }


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

    /**
     * Called when this fragment is detached
     */
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    // endregion

    // region Interface

    /**
     * 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 OnTextDirectionsFragmentInteractionListener {
        void onTextDirectionsFragmentInteraction(int pathIndex);
    }

    // endregion
}
