/*
 * Copyright 2017 .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dinect.checker.zxing;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.dinect.checker.AbstractScannerActivity;
import com.dinect.checker.R;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;

import java.util.ArrayList;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * Created by anonymous
 */
public class ScannerActivity extends AbstractScannerActivity
        implements ZXingScannerView.ResultHandler {

    private static final int SCAN_INTERVAL_PERIOD = 500;

    private ZXingScannerView scannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        if (!init(R.layout.a_zxing)) {
            return;
        }

        initToolbar(getIntent());

        scannerView = (ZXingScannerView) initScanner();
        addScanner(scannerView, R.id.zxingRoot);
    }

    @Override
    protected View initScanner() {
        return new ZXingScannerView(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        scannerView.setResultHandler(this);
        scannerView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        scannerView.stopCamera();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        finish();
    }

    @Override
    public void handleResult(Result raw) {
        handleBarcode(raw.getText());
        scannerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                scannerView.resumeCameraPreview(ScannerActivity.this);
            }
        }, SCAN_INTERVAL_PERIOD);
    }
}
