import SoundManager = Laya.SoundManager;
import { WXSdk } from "../game_module/WXSdk";
import GameTool from "../Util/GameTool";
export enum SoundType {
    hit,
    click,
    win,
    defeat
}
export class SettingController {
    private static instasnce: SettingController;
    public vibrate = true;
    public sound = true;

    public static get Instance(): SettingController {
        return this.instasnce || (this.instasnce = new SettingController());
    }
    public Init() {
        var v = Laya.LocalStorage.getItem("vibrate");
        this.vibrate = Number(v) ? v == "1" ? true : false : true;
        var s = Laya.LocalStorage.getItem("sound")
        this.sound = Number(s) ? s == "1" ? true : false : true;
    }
    public SettingSound(value?: number) {
        if (this.soundChannel)
            this.soundChannel.volume = value;
    }

    private soundChannel: Laya.SoundChannel;
    public _playMusic(on?) {
        if (on == null) on = this.sound;
        if (on) {
            if (this.soundChannel == null) {
                this.soundChannel = SoundManager.playMusic(this.bg, 0, new Laya.Handler(this, () => {
                    this.soundChannel.play();
                }));

            } else {
                this.soundChannel.resume();
            }
        } else {
            if (this.soundChannel) {
                this.soundChannel.pause();
            }
        }
    }

    public SetSoundV(value) {
        // if (this.soundChannel)
        //     this.soundChannel.volume = value
    }


    public PlaySound(type: SoundType) {
        if (!this.sound) return;
        var _name = SoundType[type];
        SoundManager.playSound(this.SoundS[_name]);
      
    }
    public StopSound(type: SoundType) {
        if (!this.sound) return;
        var _name = SoundType[type];
        SoundManager.stopSound(this.SoundS[_name]);
    }


    public SoundS = {
        click: "subpackage1/sound/click.mp3",
        win: "subpackage1/sound/vector.mp3",
        defeat: "subpackage1/sound/defeat.mp3",
        car_crush_1: "subpackage1/sound/car_crush_1.mp3",
        car_crush_2: "subpackage1/sound/car_crush_2.mp3",
        car_crush_3: "subpackage1/sound/car_crush_3.mp3",
        explotion1: "subpackage1/sound/explotion1.mp3",
        explotion2: "subpackage1/sound/explotion2.mp3",
        gun_fire_1: "subpackage1/sound/gun_fire_1.mp3",
        gun_fire_2: "subpackage1/sound/gun_fire_2.mp3",
        gun_fire_3: "subpackage1/sound/gun_fire_3.mp3",
        reload_gun_1: "subpackage1/sound/reload_gun_1.mp3",
        reload_gun_2: "subpackage1/sound/reload_gun_2.mp3",
        hitcar: "subpackage1/sound/hitcar.mp3",
        hitrole: "subpackage1/sound/hitrole.mp3",
        hit:"subpackage1/sound/hit.mp3"
    }

    private bg = "subpackage1/sound/bgm.mp3"

}