
import { _decorator,Node, Sprite, SpriteFrame } from 'cc';
import platformSystem from '../platform/platformSystem';
import BasePanel from '../core/component/BasePanel';
import { audioMgr } from '../core/manager/AudioManager';
import Data from '../core/manager/Data';
import { settingData } from '../user/SettingData';
import { GameConst } from '../core/common/GameConst';

const { ccclass, property } = _decorator;

@ccclass('SettingLayer')
export class SettingLayer extends BasePanel {
    @property({type:SpriteFrame,tooltip:"选中的勾"})
    public gou_seleted: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"未选中的勾"})
    public not_gou_seleted: SpriteFrame = null!;

    @property({type:SpriteFrame,tooltip:"背景开图片"})
    public bg_music_open: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"背景关图片"})
    public bg_music_close: SpriteFrame = null!;
    @property({type:Sprite,tooltip:"背景节点精灵"})
    public bg_music_sp: Sprite = null!;
    @property({type:Sprite,tooltip:"背景勾节点精灵"})
    public bg_music_gou: Sprite = null!;

    @property({type:SpriteFrame,tooltip:"音效开图片"})
    public sound_open: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"音效关图片"})
    public  sound_close: SpriteFrame = null!;
    @property({type:Sprite,tooltip:"音效节点精灵"})
    public sound_sp: Sprite = null!;
    @property({type:Sprite,tooltip:"音效勾节点精灵"})
    public sound_gou_sp: Sprite = null!;

    start(){
        platformSystem.platform.showInterstitialAd();
        this.updateUI();
    }

    updateUI(){
        this.bg_music_sp.spriteFrame = settingData.data.bgMusic ? this.bg_music_open : this.bg_music_close;
        this.bg_music_gou.spriteFrame = settingData.data.bgMusic ? this.gou_seleted : this.not_gou_seleted;

        this.sound_sp.spriteFrame = settingData.data.soundFx ? this.sound_open : this.sound_close;
        this.sound_gou_sp.spriteFrame = settingData.data.soundFx ? this.gou_seleted : this.not_gou_seleted;
    }

    onBtnClicked(event:any, customEventData:any) {
       audioMgr.playOneShot(GameConst.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.close();
                break;
            case "gou_sound":
                settingData.data.soundFx = !settingData.data.soundFx;
                settingData.saveToCache();
                Data.user.soundVolume = Data.user.soundVolume == 0 ? 1:0;
                this.updateUI();
                break;
            case "gou_music":
                let state: boolean = settingData.data.bgMusic;
                settingData.data.bgMusic = !state;
                if(state){
                   audioMgr.stop(GameConst.audios.bg_music);
                }else{
                   audioMgr.play(GameConst.audios.bg_music,true);
                }
                settingData.saveToCache();
                //Data.user.vibration = Data.user.vibration == 0?1:0;
                this.updateUI();
                break;
        }
    }
}
