package it.integry.zebrascannerlibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.view.KeyEvent;

import it.integry.barcode_base_android_library.exception.BarcodeAdapterNotFoundException;
import it.integry.barcode_base_android_library.extension.RunnableArgs;
import it.integry.barcode_base_android_library.interfaces.BarcodeReaderInterface;
import it.integry.barcode_base_android_library.model.BarcodeScanDTO;

public class ZebraBarcodeReader implements BarcodeReaderInterface {

    private final Context mContext;

    private static RunnableArgs<BarcodeScanDTO> mOnScanSuccessfull;
    private static RunnableArgs<Exception> mOnScanFailed;

    private static final String TAG = ZebraBarcodeReader.class.getName();
    private static final String INTENT_FILTER_STRING = "it.integry.scan_filter";

    public ZebraBarcodeReader(Context context) {
        this.mContext = context;
    }

    @Override
    public boolean isRightAdapter() {
        String model = Build.MODEL;
        if(model.equalsIgnoreCase("TC700H")) {
            return true;
        } else if(model.equalsIgnoreCase("TC200J")) {
            return true;
        } else if(model.equalsIgnoreCase("TC210K")) {
            return true;
        } else if(model.equalsIgnoreCase("TC21")) {
            return true;
        } else if(model.equalsIgnoreCase("TC20")) {
            return true;
        } else return false;
    }

    @Override
    public void init(Runnable onDeviceReady) throws BarcodeAdapterNotFoundException {
        if(isRightAdapter()) {
            IntentFilter filter = new IntentFilter();
            filter.addCategory(Intent.CATEGORY_DEFAULT);
            filter.addAction(INTENT_FILTER_STRING);
            mContext.registerReceiver(myBroadcastReceiver, filter);

            onDeviceReady.run();
        } else {
            throw new BarcodeAdapterNotFoundException(getAdapterName());
        }
    }

    @Override
    public void deinit() {
        mContext.unregisterReceiver(myBroadcastReceiver);
    }

    @Override
    public void register(RunnableArgs<BarcodeScanDTO> onScanSuccessfull, RunnableArgs<Exception> onScanFailed) {
        mOnScanSuccessfull = onScanSuccessfull;
        mOnScanFailed = onScanFailed;
    }


    @Override
    public String getAdapterName() {
        return "Zebra";
    }

    @Override
    public void onKeyEvent(KeyEvent keyEvent) {

    }


    private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(INTENT_FILTER_STRING)) {
                try {
                    dispatchEvent(intent);
                } catch (Exception e) {

                }
            }
        }
    };


    private void dispatchEvent(Intent initiatingIntent)
    {

        String decodedSource = initiatingIntent.getStringExtra(mContext.getResources().getString(R.string.datawedge_intent_key_source));
        String decodedData = initiatingIntent.getStringExtra(mContext.getResources().getString(R.string.datawedge_intent_key_data));
        String decodedLabelType = initiatingIntent.getStringExtra(mContext.getResources().getString(R.string.datawedge_intent_key_label_type));

        BarcodeScanDTO barcodeScanDTO = new BarcodeScanDTO()
                .setByteValue(decodedData.getBytes())
                .setStringValue(decodedData)
                .setType(ZebraBarcodeTypeMapper.map(decodedLabelType))
                .setName(decodedLabelType);
//                .setDecodingTime(mDecodeResult.decodeTimeMillisecond);

        mOnScanSuccessfull.run(barcodeScanDTO);
    }
}
