import { _decorator, Component, Label, Node, Sprite, 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;
    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( this.btn_sound.getComponent(Toggle).isChecked){
                AudioManager.instance.openSound()
            }else{
                AudioManager.instance.closeSound()
            }
            this.updateStatus();
        },this);
        this.btn_music.on(Node.EventType.TOUCH_END,()=>{
            gameManager.playBtnSound()
            if( this.btn_music.getComponent(Toggle).isChecked){
                AudioManager.instance.openMusic()
            }else{
                AudioManager.instance.closeMusic()
            }
            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('music');
        if(state==""||state==null||state==undefined){
            state = 'true'
        }
        if(state === 'true'){
            this.btn_music.getComponent(Toggle).isChecked = true;
            // this.spr_music.getComponent(Sprite).grayscale = false;
        }else{
            this.btn_music.getComponent(Toggle).isChecked = false;
            // this.spr_music.getComponent(Sprite).grayscale = true;
        }
        state = StorageManager.instance.getGlobalData('sound');
        if(state==""||state==null||state==undefined){
            state = 'true'
        }
        if(state === 'true'){
            this.btn_sound.getComponent(Toggle).isChecked = true;
            // this.spr_sound.getComponent(Sprite).grayscale = false;
        }else{
            this.btn_sound.getComponent(Toggle).isChecked = false;
            // this.spr_sound.getComponent(Sprite).grayscale = true;
        }
    }
    close(){
        this.node.removeFromParent();
    }


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


