let soundOn = true;
// let bgChannel: egret.SoundChannel;
// let loading = false;
export const getSoundOn = () => {
    return soundOn
}
//document.getElementById('bgmusic').duration
export const toggleSound = () => {
    soundOn = !soundOn;
    if(soundOn) {
        document.getElementById('bgmusic')['play']();
    }else {
        document.getElementById('bgmusic')['pause']();
    }
    // if (bgChannel) {
    //     if (!soundOn) {
    //         bgChannel.stop();
    //         bgChannel = null;
    //     }
    // } else {
    //     if (soundOn) {
    //         playBg();
    //     }
    // }
}

// export const playBg = async () => {
//     if (bgChannel) return;
//     if (loading) return;
//     loading = true;
//     RES.getResByUrl(window['musciURL'], (bg) => {
//         bgChannel = bg.play();
//         loading = false;
//     }, this, RES.ResourceItem.TYPE_SOUND);

// }

export enum SoundType {
    click = 'click',
    bounce = 'bounce',
    good = 'good',
    recover = 'recover',
    shoot = 'shoot',
}

export const playSound = (type: SoundType) => {
    if (!soundOn) return;
    const sound: egret.Sound = RES.getRes(type + "_mp3");
    if (sound)
        sound.play(0, 1);
    else
        RES.getResAsync(type + "_mp3")
}