import { AssetPath } from "../Config/AssetPath";


/*
* name;
*/
export class AudioControl extends Laya.EventDispatcher
{
    private static ins:AudioControl;
    private _isbgOpen:boolean = true;
    private open:boolean = true;
    private _isAllOpen:boolean = true;
    private _nowBgName:string = "";
    private _isPlaybging:boolean = false;
    /**
     * 声音开关引用索引
     */
    private _soundIndex:number = 0;

    constructor(){
        super();
        AudioControl.ins = this;
    }

    public static get Instance():AudioControl
    {
        if( AudioControl.ins == null )
        {
            return new AudioControl();
        }
        return AudioControl.ins;
    }

    public init():void
    {
        Laya.SoundManager.autoReleaseSound = false;
        Laya.SoundManager.autoStopMusic = true;
    }

    public set gamesoundOpen(value:boolean)
    {
        this.open = value;
        Laya.SoundManager.soundMuted = !value;
        if( value == true )
        {
            fairygui.UIConfig.buttonSoundVolumeScale = 1;
        }
        else
        {
            fairygui.UIConfig.buttonSoundVolumeScale = 0;
        }
    }

    /**
     * 音效是否开启
     */
    public get GetSoundSwitch():boolean
    {
        return this.open;
    }

    public set isbgOpen(value:boolean)
    {
        this._isbgOpen = value;
        Laya.SoundManager.musicMuted = !value;
    }

    /**
     * 背景音乐是否开启
     */
    public get isbgOpen():boolean
    {
        return this._isbgOpen;
    }

    public set isAllOpen( value:boolean )
    {
        if( value )
        {
            this._soundIndex--;
            if( this._soundIndex < 0 )
            {
                this._soundIndex = 0;
            }
            if(!this._isPlaybging&&this._soundIndex==0 )
            {
                this._isAllOpen = true;
                this.setGameVolume( 1 );
                this.playbgSound( this._nowBgName );
                fairygui.UIConfig.buttonSoundVolumeScale = 1;
            }
            else
            {
                this._isAllOpen = false;
            }
        }
        else
        {
            this._isAllOpen = false;
            if( this._soundIndex < 0 )
            {
                this._soundIndex = 0;
            }
            this._soundIndex++;
            if(this._isPlaybging)
            {
                this.stopbgSound();
            }
            this.setGameVolume( 0 );
            fairygui.UIConfig.buttonSoundVolumeScale = 0;
        }
    }

    /**
     * 停止播放音效
     */
    public stopGameSound():void
    {
        Laya.SoundManager.stopAllSound();
    }

    /**
     * 停止所有声音
     */
    public ClearAllSOUND():void
    {
        this.stopGameSound();
        this.stopbgSound();
    }

    /**
     * 播放背景音乐
     * @param soundName 声音名
     */
    public playbgSound( soundName:string ):void
    {
        this._nowBgName = soundName;
        if( this._isAllOpen && this._nowBgName != "" && this._soundIndex<= 0 && !this._isPlaybging )
        {
            this._isPlaybging = true;
            Laya.SoundManager.playMusic( AssetPath.getsound( this._nowBgName ) );
            this.bgSoundVolume = 0.4;
        }
    }

    /**
     * 停止背景音乐
     */
    public stopbgSound():void
    {
        this._isPlaybging = false;
        Laya.SoundManager.stopMusic();
    }

    /**
     * 所有音乐是否开启
     */
    public get isAllOpen():boolean
    {
        return this._isAllOpen;
    }

    /**
     * 设置背景音乐音量
     * @param value 音量范围从 0（静音）至 1（最大音量）。
     */
    public set bgSoundVolume( value:number )
    {
        Laya.SoundManager.setMusicVolume( value );
    }

    public get bgSoundVolume():number
    {
        return Laya.SoundManager.musicVolume;
    }

    /**
     * 设置背景音乐音量
     * @param value 音量范围从 0（静音）至 1（最大音量）。
     */
    public set soundVolume( value:number )
    {
        Laya.SoundManager.setSoundVolume( value );
    }

    public get soundVolume():number
    {
        return Laya.SoundManager.soundVolume;
    }

    /**
     * 设置所有声音音量
     * @param value 音量范围从 0（静音）至 1（最大音量）。
     */
    public setGameVolume(value:number):void
    {
        this.bgSoundVolume = value;
        this.soundVolume = value;
    }

    

    /**
     * 播放音效
     * @param soundName 音效名
     * @param loop 播放次数
     */
    public playGameSound( soundName:string, loop:number = 1 ):void
    {
        if( this._isAllOpen && soundName != "" && Laya.SoundManager.soundVolume != 0 )
        {
            Laya.SoundManager.playSound( AssetPath.getsound( soundName ),loop );
        }
    }

    
}