package it.integry.pointmobilescannerlibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import device.common.DecodeResult;
import device.common.ScanConst;
import device.sdk.ScanManager;
import it.integry.plugins.barcode_base_library.exception.BarcodeAdapterNotFoundException;
import it.integry.plugins.barcode_base_library.interfaces.BarcodeReaderInterface;
import it.integry.plugins.barcode_base_library.model.BarcodeScanDTO;
import it.integry.plugins.barcode_base_library.extension.RunnableArgs;
import it.integry.plugins.barcode_base_library.model.BarcodeType;

public class PointMobileBarcodeReader implements BarcodeReaderInterface {

    private Context mContext;
    private static ScanManager mScanManager;
    private static DecodeResult mDecodeResult;

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

    private static String TAG = PointMobileBarcodeReader.class.getName();


    private int mBackupResultType = ScanConst.ResultType.DCD_RESULT_COPYPASTE;

    public PointMobileBarcodeReader(Context context) {
        this.mContext = context;

        mScanManager = new ScanManager();
        mDecodeResult = new DecodeResult();
    }

    @Override
    public boolean isRightAdapter() {
        try {
            mScanManager.aDecodeGetResultType();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public void init() throws BarcodeAdapterNotFoundException {
        if(isRightAdapter()){
            try{

                mBackupResultType = mScanManager.aDecodeGetResultType();
                mScanManager.aDecodeSetResultType(ScanConst.ResultType.DCD_RESULT_USERMSG);
            } catch (Exception ex) {
                throw new BarcodeAdapterNotFoundException(getAdapterName());
            }
        } else {
            throw new BarcodeAdapterNotFoundException(getAdapterName());
        }
    }

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

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





    public static class ScanResultReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mScanManager != null) {
                if (ScanConst.INTENT_USERMSG.equals(intent.getAction())) {
                    mScanManager.aDecodeGetResult(mDecodeResult.recycle());

                    if (mOnScanSuccessfull != null) {

                        //try {
                            if(mDecodeResult.symName.equalsIgnoreCase("READ_FAIL")){
                                //if(mOnScanFailed != null) mOnScanFailed.run(new Exception("Barcode non riconosciuto"));
                                return;
                            }

                            if(mDecodeResult != null) {
                                BarcodeScanDTO barcodeScanDTO = new BarcodeScanDTO()
                                        .setByteValue(mDecodeResult.decodeValue)
                                        .setStringValue(mDecodeResult.toString().replaceAll("\n", "").replaceAll("\r", "").trim())
                                        .setType(BarcodeType.fromInt(mDecodeResult.symType))
                                        .setName(mDecodeResult.symName)
                                        .setDecodingTime(mDecodeResult.decodeTimeMillisecond);

                                mOnScanSuccessfull.run(barcodeScanDTO);
                            }

                        //} catch (Exception ex) {
                        //    Log.e(TAG, ex.getMessage());

                        //    if(mOnScanFailed != null) mOnScanFailed.run(ex);
                        //}
                    }
                }
            }
        }
    }


}
