
import { _decorator,Node } 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 { autoBind } from '../core/extend/AutoBind';
import { GameConst } from '../core/common/GameConst';

const { ccclass, property } = _decorator;

@ccclass('SettingLayer')
export class SettingLayer extends BasePanel {
    @autoBind sound_on_icon: Node = null;
    @autoBind sound_off_icon: Node = null;
    @autoBind vibration_on_icon: Node = null;
    @autoBind vibration_off_icon: Node = null;

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

    updateUI(){
        let sound:boolean = settingData.data.soundFx;
        this.sound_on_icon.active = sound;
        this.sound_off_icon.active = !sound;
        let vibration:boolean = settingData.data.vibration;
        this.vibration_on_icon.active = vibration;
        this.vibration_off_icon.active = !vibration;
    }

    onBtnClicked(event:any, customEventData:any) {
       audioMgr.playOneShot(GameConst.audios.btnclick);
        var btnName = event.target.name;
        switch (btnName) {
            case "btn_close":
                this.close();
                break;
            case "btn_sound":
                settingData.data.soundFx = !settingData.data.soundFx;
                settingData.saveToCache();
                Data.user.soundVolume = Data.user.soundVolume == 0 ? 1:0;
                this.updateUI();
                break;
            case "btn_vibration":
                settingData.data.vibration = !settingData.data.vibration;
                settingData.saveToCache();
                Data.user.vibration = Data.user.vibration == 0?1:0;
                this.updateUI();
                break;
        }
    }
}
