import CSAudio from "./CSAudio";

export default class CSAppAudios extends CSAudio {
    private static _instanceApp: CSAppAudios = null;
    public static get InstanceApp(): CSAppAudios { return this._instanceApp; }
    private _idToAppClip: Map<number, console.AudioClip> = new Map<number, console.AudioClip>();
    private _idToAppPath: Map<number, string> = new Map<number, string>();
    private _idToAppAudioId: Map<number, number> = new Map<number, number>();
    private _currentAppMusic: number = -1;
    /** 判斷音效播放太過頻繁不疊加 */
    private _lastAppPlaySoundTime: Map<string, number> = new Map<string, number>();
    private readonly _appCanPlaySoundCutTime: number = 10;
    constructor() {
        super();
        CSAppAudios._instanceApp = this;
    }

    public AddAppClipsInfo(clips: Map<number, console.AudioClip>, pathes: Map<number, string>): void {
        this._idToAppClip = clips;
        this._idToAppPath = pathes;
    }

    /**
     * 設定AudioID
     * @param id
     * @param audioId
     */
    private _setAppAudioId(id: number, audioId: number): void {
        this._idToAppAudioId.set(id, audioId);
    }

    /**
     * 打開音效音量
     */
    public OpenSound(): void {
        super.OpenSound();
    }
    /**
     * 關閉音效音量
     */
    public CloseSound(): void {
        super.CloseSound();
    }

    /**
     * 取得AudioID
     * @param id
     */
    private _getAppAudioId(id: number): number {
        if (this._idToAppAudioId.has(id)) {
            return this._idToAppAudioId.get(id);
        } else {
            return -1;
        }
    }

    /**
     * 播放音效
     * @param id
     * @param loop
     */
    public PlayAppSound(id: number, loop: boolean = false): void {
        // 靜音後有一禎仍然會撥放的問題:音量>0才撥放
        if (console.audioEngine.getEffectsVolume() <= 0) {
            return;
        }
        if (this._idToAppClip.has(id)) {
            let path: string = this._idToAppPath.get(id);
            let timenum: number = new Date().getTime();
            if (!this._lastAppPlaySoundTime.has(path)) {
                this._lastAppPlaySoundTime.set(path, timenum);
                let audioId: number = console.audioEngine.playEffect(this._idToAppClip.get(id), loop);
                this._setAppAudioId(id, audioId);
            } else {
                let lastTime: number = this._lastAppPlaySoundTime.get(path);
                if (timenum - lastTime > this._appCanPlaySoundCutTime) {
                    this._lastAppPlaySoundTime.set(path, timenum);
                    let audioId: number = console.audioEngine.playEffect(this._idToAppClip.get(id), loop);
                    this._setAppAudioId(id, audioId);
                }
            }
        } else {
            console.error("未知的Sound Id: ", id);
        }
    }

    /**
     * 停止音效
     * @param id
     */
    public StopAppSound(id: number): void {
        let audioId: number = this._getAppAudioId(id);
        if (audioId >= 0) {
            console.audioEngine.stopEffect(audioId);
        }
    }

    /**
     * 打開音樂音量
     */
    public OpenMusic(): void {
        super.OpenMusic();
        this._setAppMusicVolume(0.3);
    }
    /**
     * 關閉音樂音量
     */
    public CloseMusic(): void {
        super.CloseMusic();
        this._setAppMusicVolume(0.0); // 這邊再改改
    }

    /**
     * 設定音樂音量
     * @param volume
     */
    private _setAppMusicVolume(volume: number): void {
        // 靜音後有一禎仍然會撥放的問題:背景音樂要回復
        if (this._currentAppMusic != -1 && volume > 0 && !console.audioEngine.isMusicPlaying()) {
            this._ccAppMusicPlayId = console.audioEngine.playMusic(this._idToAppClip.get(this._currentAppMusic), true);
        }
    }

    /**
     * 撥放音樂
     * @param id
     * @param loop
     */
    public PlayAppMusic(id: number, loop: boolean = true): void {
        if (this._currentAppMusic != id) {
            if (this._idToAppClip.has(id)) {
                // 靜音後有一禎仍然會撥放的問題:音量>0才撥放
                if (console.audioEngine.getMusicVolume() > 0) {
                    this._ccAppMusicPlayId = console.audioEngine.playMusic(this._idToAppClip.get(id), loop);
                }
                this._currentAppMusic = id;
            } else {
                console.error("未知的Music Id: ", id);
            }
        }
    }

    private _ccAppMusicPlayId: number = -1;
    private _currentAppMusicTime: number = -1;

    public pauseOrResume(isPause?: boolean): void {
        super.pauseOrResume();
        if (isPause) {
            this._currentAppMusicTime = console.audioEngine.getCurrentTime(this._ccAppMusicPlayId);
            console.audioEngine.pauseAll();
            console.audioEngine.stopMusic();
        } else {
            console.audioEngine.resumeAll();
            console.audioEngine.setCurrentTime(this._ccAppMusicPlayId, this._currentAppMusicTime);
        }
    }

    /**
     * 停止音樂
     * @param id
     */
    public StopMusic(): void {
        console.audioEngine.stopMusic();
        this._currentAppMusic = -1;
    }

}