import { _decorator, AudioClip, AudioSource, ccenum, Component, Node } from 'cc';
import { ManagerType, TopLevelManager } from '../data-flow/TopLevelManager';
import { MainManager } from '../data-flow/MainManager';
import { UISoundHandler } from './UISoundHandler';
import { AudioSettingsData, SettingsData } from '../../data/DataModel';
import { UserDataBehaviour } from '../../data/UserDataBehaviour';
import { GameSettingsManager } from '../data-flow/settings/GameSettingsManager';
import { UIManager } from '../ui/UIManager';
import { MenuSettingsPanel } from '../../../UI/menu/MenuSettingsPanel';
import { PopupType } from '../../../UI/popup/PopupType';
const { ccclass, property } = _decorator;

export enum CardClipType
{
    FLIP = 0,
    UNFLIP = 1,
    SHOW_ALL = 2,
    HIDE_ALL = 3,
    MATCH = 4,
    NO_MATCH = 5
}
ccenum(CardClipType);

@ccclass('SoundData')
export class ClipData
{
    @property({type: CardClipType})
    public type: CardClipType;

    @property({type: AudioClip})
    public clip: AudioClip;
}

@ccclass('SoundManager')
export class SoundManager extends TopLevelManager
{
    @property({type: AudioClip})
    private buttonClip: AudioClip;
    
    @property({type: [ClipData]})
    private clipDatas: ClipData[] = [];

    @property({type: AudioSource})
    private source: AudioSource;

    @property({type: AudioSource})
    private bgSource: AudioSource;

    public mainManager: MainManager;
    private currentClipData: ClipData;
    private isSoundOn: boolean;

    private static m_Instance: SoundManager;
    
    public static get m_GetInstance() : SoundManager
    {
        return SoundManager.m_Instance;
    }

    protected onLoad(): void 
    {
        console.log("Play bg source");

        this.bgSource.loop = true;
        if (!SoundManager.m_Instance)
        {
            SoundManager.m_Instance = this;
            return;
        }
        
        this.destroy(); 
    }

    public fn_Init()
    {
        this.fn_LoadSavedToggleStates();
        this.fn_PlayMusicOnUserSession();
    }

    public fn_LoadSavedToggleStates()
    {
        let uiManager = this.mainManager.fn_GetMainManager<UIManager>(ManagerType.UI);
        let gameSettingsManager = this.mainManager.fn_GetMainManager<GameSettingsManager>(ManagerType.GAME_SETTINGS);
        let settingsData = gameSettingsManager.fn_LoadSavedData();

        if (settingsData)
        {
            console.log("### SoundManager: Loading saved data");
            uiManager.fn_InitMenuSettingsData(settingsData);
            this.isSoundOn = settingsData.audioSettingsData.soundToggle;
        }
        else 
        {
            console.log("### SoundManager: Loading new data");
            settingsData = new SettingsData();
            settingsData.audioSettingsData = new AudioSettingsData();
            settingsData.audioSettingsData.musicToggle = true;
            settingsData.audioSettingsData.soundToggle = true;
            this.isSoundOn = true;

            gameSettingsManager.gameSettingData = settingsData;
            uiManager.fn_InitMenuSettingsData(settingsData);
        }
    }

    private fn_PlayMusicOnUserSession() 
    {
        let userDataBehaviour = this.mainManager.fn_GetMainManager<UserDataBehaviour>(ManagerType.USER_DATA_BEHAVIOUR);
        console.log("IsFirstTimerUser: " + userDataBehaviour.saveManager.fn_IsFirstTimeUser());
        if (!userDataBehaviour.saveManager.fn_IsFirstTimeUser())
            this.fn_CheckAndPlayMusic();
        else 
            this.fn_PlayBGMusic();
    }

    private fn_CheckAndPlayMusic()
    {
        let gameSettingsManager = this.mainManager.fn_GetMainManager<GameSettingsManager>(ManagerType.GAME_SETTINGS);
        this.fn_UpdateAudios(gameSettingsManager.gameSettingData.audioSettingsData);
    }

    private fn_PlayBGMusic() 
    {
        console.log(":::: BG MUSIC being played: " + this.bgSource.playing);
        if (!this.bgSource.playing) // First time user
        {
            // this.bgSource.loop = true;
            console.log(":::: Play again ");
            this.bgSource.play();
        }
    }

    private fn_StopBGMusic()
    {
        return;
        console.log("### Stop bg music: " + this.bgSource.playing);
        if (this.bgSource.playing)
            this.bgSource.stop();
    }

    public fn_InitMainManager(mainManager: MainManager) 
    {
        this.mainManager = mainManager;
    }    
    
    public fn_PlayClip(type: CardClipType)
    {
        if (!this.isSoundOn) return;

        if (this.source.playing)
        {
            console.log("IsPlaying");
            console.log("bgSource.playing: " + this.bgSource.playing);
            this.source.stop();
        }

        if (this.currentClipData == null || this.currentClipData.type != type)
            this.currentClipData = this.GetClipData(type);
            
        console.log("this.currentClipData.clip: " + this.currentClipData.clip.name);
        this.source.clip = this.currentClipData.clip;
        // this.source.play();
        this.source.playOneShot(this.currentClipData.clip, this.source.volume);
    }

    public fn_PlayBtnClip()
    {
        if (!this.isSoundOn) return;

        if (this.source.playing)
            this.source.stop();

        this.source.clip = this.buttonClip;
        // this.source.play();
        this.source.playOneShot(this.buttonClip, this.source.volume);
    }

    private GetClipData(type: CardClipType) 
    {
        return this.clipDatas.find(clipData => clipData.type == type);
    }

    public fn_UpdateAudios(data: AudioSettingsData)
    {
        if (data.musicToggle)
        {
            console.log("fn_UpdateAudios: Play BG Music");
            this.fn_PlayBGMusic();
        }
        else 
        {
            console.log("fn_UpdateAudios: Stop BG Music");
            this.fn_StopBGMusic(); 
        }

        this.isSoundOn = data.soundToggle;
    }
}

