package acquire.sdk.sound;

import androidx.annotation.IntRange;

import com.morefun.yapi.device.beeper.Beeper;

import acquire.base.utils.thread.ThreadPool;
import acquire.sdk.DeviceHelper;

/**
 * POS beeper
 *
 * @author Janson
 * @date 2021/6/10 16:30
 */
public class BBeeper {

    public static final int NORMAL = 0;
    public static final int SUCCESS = 1;
    public static final int FAIL = 2;
    public static final int INTERVAL = 3;
    public static final int ERROR = 4;

    public static Beeper beeper = null;

    /**
     * Beep several times
     * @param count beep count
     */
    public static void beep(int count) {
        try {
            if (beeper == null) {
                beeper = DeviceHelper.getDeviceService().getBeeper();
            }
            beeper.beep(count);
            if (count != SUCCESS) {
                ThreadPool.executeDelay(() -> {
                    try {
                        beeper.beep(NORMAL);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, 300L);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * beeps
     *
     * @param frequency Set beep frequency in Hz
     * @param duration  Set how long to beep in ms.
     */
/*    public static void beep(@IntRange(from = 1, to = 4000) int frequency, @IntRange(from = 1) int duration) {
        try {
            Beeper beeper = (Beeper) NSDKModuleManagerImpl.getInstance().getModule(ModuleType.BEEPER);
            beeper.beep(frequency, duration);
        } catch (NSDKException e) {
            e.printStackTrace();
        }
    }*/
    public static void beep(@IntRange(from = 1, to = 4000) int frequency, @IntRange(from = 1) int duration) {
        try {
            if (beeper == null) {
                beeper = DeviceHelper.getDeviceService().getBeeper();
            }
            // Use INTERVAL type for reminders (Remove Card, etc.) to restore urgent hardware sound
            beeper.beep(INTERVAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 1 Beep for success
     */
    public static void beepSuccess() {
        beep(SUCCESS);
    }

    /**
     * 2 Beeps for decline
     */
    public static void beepDecline() {
        beep(FAIL);
    }

    /**
     * 2 Beeps when card is tapped or inserted
     */
    public static void beepCardDetected() {
        // According to client requirement: 2 Beep sound when Card Tap/Inserted
        beep(NORMAL);
    }

} 
