package lc.ris.android;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.fragment.app.Fragment;
import lc.esp.eo.sdk.EO;
import lc.ris.client.RapidInventoryClient;

;

/**
 * This class implements the item read & update flow for the RIS application.
 *
 * @author Alex Leigh
 * @since mk1
 */

public class ItemFragment extends Fragment implements BarcodeListener {

    public ItemFragment() {
        // Required empty public constructor
    }

    public static ItemFragment newInstance() {
        return new ItemFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_item, 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) {
        RapidInventoryClient client = ((MainActivity) getActivity()).getRisClient();
        EO entity = client.getInventoryStore().getEntity(barcode);
        final EditText et = getView().findViewById(R.id.formEntityNameInput);
        et.setText(entity.getValueString("id"));
        final EditText at = getView().findViewById(R.id.formEntityDescInput);
        at.setText(entity.getValueString("description"));
    }
}