package lc.ris.android;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.google.android.material.textfield.TextInputEditText;
import lc.ris.client.RapidInventoryClient;

/**
 * The purpose of this fragment is to implement the transfer workflow. The base implementation is essentially
 * as a pick/put functionality. Strictly speaking this is not a transfer, but the workflow is so-named because
 * support for true transfers (location to location transfer) is intended for the future.
 *
 * @author Alex Leigh
 * @since mk1
 */
public class TransferFragment extends Fragment implements BarcodeListener {
    public TransferFragment() {
        // Required empty public constructor
    }

    public static TransferFragment newInstance() {
        TransferFragment fragment = new TransferFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_transfer, container, false);
    }

    public void onResume() {
        super.onResume();
        ((MainActivity) getActivity()).getBarcodeBroker().addListener(this);
    }

    public void onPause() {
        super.onPause();
        ((MainActivity) getActivity()).getBarcodeBroker().removeListener(this);
    }

    @Override
    public void receiveBarcode(String barcode) {
        Log.i("TransferFragment", "Received barcode: " + barcode);

        TextInputEditText location = getView().findViewById(R.id.formLocationInput);
        String currentLocation = location.getText().toString();
        RapidInventoryClient client = ((MainActivity) getActivity()).getRisClient();

        if (currentLocation.isEmpty()) {
            location.setText(barcode);
        } else {
            location.setText("Updated!");
            // bleep bloop!
        }
    }
}