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

package com.aegir.demo.fragments;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aegir.demo.MapActivity;
import com.aegir.demo.R;
import com.aegir.vmms.wayfinding.model.Waypoint;


/**
 * Fragment that handles user interation for finding directions.
 * Contains text fields for start/end points
 * Receives actions to cancel/clear/find directions
 */
public class DirectionsFragment extends Fragment implements View.OnFocusChangeListener {

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

    public MapActivity mapActivity;

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

    // region Public Methods

    /**
     * This method determines which text field needs to have focus
     */
    public void setFocusForTextFields() {
//        if (!mapActivity.walkingPathOverlay.isSelectingStartEndPoints()) return;
//
//        final AutoCompleteTextView etEnd = (AutoCompleteTextView) getView().findViewById(R.id.act_end_point);
//        if (mapActivity.walkingPathOverlay.isSelectingStartPoint() && mapActivity.walkingPathOverlay.getStartingWaypoint() != null) {
//            etEnd.setFocusable(true);
//            etEnd.requestFocus();
//        }
    }

    /**
     * This method updates the UI by populating the text fields with the appropriate values
     */
    public void updateUI(Waypoint starting, Waypoint ending) {
        String endText = "";
        if (ending != null) {
            if (!ending.getPublicDescription().isEmpty()) {
                endText = ending.getPublicDescription();
            } else {
                endText = ending.getId();
            }
        }
        setEndField(endText);

        String startText = "";
        if (starting != null) {

            if (!starting.getPublicDescription().isEmpty()) {
                startText = starting.getPublicDescription();
            } else {
                startText = starting.getId();
            }
        }
        setStartField(startText);
    }

    public void setEndField(String endText) {
        TextView textView = ((TextView) getView().findViewById(R.id.tv_end_point));
        textView.setText(endText);
    }

    public void setStartField(String startText) {
        TextView textView = ((TextView) getView().findViewById(R.id.tv_start_point));
        textView.setText(startText);
    }

    /**
     * Called when the fragment is first created
     * @param inflater the layout inflator
     * @param container the parent container
     * @param savedInstanceState the saved state if any
     * @return an initialized view
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.d(TAG, "Creating directions fragment");
        View v = inflater.inflate(R.layout.fragment_directions, container, false);

        //Get a reference to our parent activity
        mapActivity = (MapActivity) getActivity();

        return v;

    }

    /**
     * Called after the view is created
     * @param view the view
     * @param savedInstanceState the saved instance if any
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView etStart = (TextView) getView().findViewById(R.id.tv_start_point);
        TextView etEnd = (TextView) getView().findViewById(R.id.tv_end_point);

//        etStart.setOnFocusChangeListener(this);
//        etEnd.setOnFocusChangeListener(this);
    }

    /**
     * Called when the focus changes
     * @param view the view
     * @param hasFocus true if focus is on this view
     */
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
//        if (view.getId() == R.id.act_start_point) {
//            if (hasFocus) {
//                mapActivity.walkingPathOverlay.setSelectingStartPoint(true);
//                mapActivity.walkingPathOverlay.setSelectingEndPoint(false);
//            } else {
//                mapActivity.walkingPathOverlay.setSelectingStartPoint(false);
//            }
//        } else if (view.getId() == R.id.act_end_point) {
//            if (hasFocus) {
//                mapActivity.walkingPathOverlay.setSelectingEndPoint(true);
//                mapActivity.walkingPathOverlay.setSelectingStartPoint(false);
//            } else {
//                mapActivity.walkingPathOverlay.setSelectingEndPoint(false);
//            }
//        }
    }

    // endregion
}
