import { _decorator, Component, Label, Node, Sprite, SpriteFrame, Toggle } from 'cc';
import { StorageManager } from '../framework/storageManager';
import { AudioManager } from '../framework/audioManager';
import { config } from '../config';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('setting')
export class setting extends Component {
    @property(Node) btn_back:Node;
    @property(Node) btn_check_user_id:Node;
    @property(Node) lab_user_id:Node;
    @property(Node) btn_sound:Node;
    @property(Node) btn_music:Node;
    @property(Node) spr_sound:Node;
    @property(Node) spr_music:Node;
    @property(SpriteFrame) sf_on:SpriteFrame;
    @property(SpriteFrame) sf_off:SpriteFrame;
    private m_cancel_back = null;
    private touch_number:number = 0;
    start() {
        let self = this;
        this.btn_back.on(Node.EventType.TOUCH_END,()=>{
            gameManager.playBtnSound()
            if(this.m_cancel_back!=null){
                this.m_cancel_back()
            }
            self.close();
        },this);
        this.btn_sound.on(Node.EventType.TOUCH_END,()=>{
            gameManager.playBtnSound()
            if(StorageManager.instance.getGlobalData('sound')=='true') {
                AudioManager.instance.closeSound()
            } else {
                AudioManager.instance.openSound()
            }
            this.updateStatus();
        },this);
        this.btn_music.on(Node.EventType.TOUCH_END,()=>{
            gameManager.playBtnSound()
            if(StorageManager.instance.getGlobalData('music')=='true'){
                AudioManager.instance.closeMusic()
            }else{
                AudioManager.instance.openMusic()
            }
            this.updateStatus();
        },this);
        this.btn_check_user_id.on(Node.EventType.TOUCH_START,()=>{
            this.touch_number+=1;
            if(this.touch_number===3){
                this.lab_user_id.active = true;
            }
        },this)
        this.lab_user_id.getComponent(Label).string = `id:${gameManager.userInfo.id}`
        this.lab_user_id.active = false;
        this.updateStatus();

    }
    updateStatus(){
        let state = StorageManager.instance.getGlobalData('sound');
        if(state==""||state==null||state==undefined){
            state = 'true'
        }
        if(state === 'true'){
            this.btn_sound.getComponent(Sprite).spriteFrame = this.sf_on
        }else{
            this.btn_sound.getComponent(Sprite).spriteFrame = this.sf_off
        }

        state = StorageManager.instance.getGlobalData('music');
        if(state==""||state==null||state==undefined){
            state = 'true'
        }
        if(state === 'true'){
            this.btn_music.getComponent(Sprite).spriteFrame = this.sf_on
        }else{
            this.btn_music.getComponent(Sprite).spriteFrame = this.sf_off
        }
    }
    close(){
        this.node.removeFromParent();
    }

    public show(cancel_back){
        this.m_cancel_back = cancel_back;
    }
}


