package lc.ris.android;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
 * This {@link Fragment} implements the barcode input for the RIS application. The input supports two entry methods:
 * the traditional keyboard-based entry, or when used on a Zebra device running DataWedge, from DataWedge intents.
 *
 * @author Alex Leigh
 * @since mk1
 */
public class ScanFragment extends Fragment implements BarcodeListener {
    private DataWedgeReceiver dataWedgeReceiver;

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

    public void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
        dataWedgeReceiver = new DataWedgeReceiver(this);
        ((MainActivity) getActivity()).getBarcodeBroker().addListener(this);
        getActivity().registerReceiver(dataWedgeReceiver, filter);
    }

    public void onPause() {
        super.onPause();
        ((MainActivity) getActivity()).getBarcodeBroker().removeListener(this);
        getActivity().unregisterReceiver(dataWedgeReceiver);
        dataWedgeReceiver = null;
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final EditText et = view.findViewById(R.id.formBarcodeInput);

        // We want the input box to automatically clear whenever it is selected manually
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b == true) {
                    et.getText().clear();
                    et.clearComposingText();
                }

            }
        });

        et.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    BarcodeBroker barcodeBroker = ((MainActivity) getActivity()).getBarcodeBroker();
                    barcodeBroker.publish(String.valueOf(et.getText()));
                    //        et.getText().clear();
                    //        et.clearComposingText();
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
    }

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

    @Override
    public void receiveBarcode(String barcode) {
        final EditText et = getView().findViewById(R.id.formBarcodeInput);
        et.setText(barcode);
    }
}