package com.neptune.modules;

import com.neptune.base.NeptuneAppMgmt;
import com.neptune.utils.MyLog;
import com.pax.dal.IScanner;
import com.pax.dal.entity.EScannerType;

public class Scanner {

    private static Scanner scanner;

    private IScanner iScanner = null;
    private EScannerType scannerType = null;

    private Scanner() {
    }

    public static Scanner getInstance() {
        if (scanner == null) {
            scanner = new Scanner();
        }
        return scanner;
    }

    public boolean open(EScannerType type) {
        if (iScanner == null || type != scannerType) {
            iScanner = NeptuneAppMgmt.getDal().getScanner(type);
            scannerType = type;
        }
        boolean res = iScanner.open();
        MyLog.logV("open " + type.name() + ": " + res);
        return res;
    }

    public void setTimeOut(int timeout) {
        iScanner.setTimeOut(timeout);
        MyLog.logV("setTimeOut " + timeout);
    }

    public boolean setFlashOn(boolean isOn) {
        boolean res = iScanner.setFlashOn(isOn);
        MyLog.logV("setFlashOn " + isOn + ": " + res);
        return res;
    }

    public void start(IScanner.IScanListener listener) {
        iScanner.start(listener);
        MyLog.logV("start");
    }

    public void close() {
        if (iScanner != null) {
            iScanner.close();
            MyLog.logV("close");
        }
    }
}
