import { _decorator, Component, Label, Node, Toggle } from 'cc';
import { base_ui } from '../fw/base_ui';
import { audioManager } from '../manager/audioManager';
import { GameManager } from '../GameManager';
import { userDataManager } from '../manager/userDataManager';
import { config } from '../config';
const { ccclass, property } = _decorator;

@ccclass('setting')
export class setting extends base_ui {
    @property(Node) btn_close:Node = null
    @property(Node) yinyue_btn_on:Node = null;
    @property(Node) yinyue_btn_off:Node = null;
    @property(Node) shengyin_btn_on:Node = null;
    @property(Node) shengyin_btn_off:Node = null;
    @property(Node) zhendong_btn_on:Node = null;
    @property(Node) zhendong_btn_off:Node = null;
    @property(Node) btn_user:Node = null;
    @property(Node) lab_user:Node = null;
    @property(Node) btn_clear_local_data:Node = null;

    protected start(): void {
        this.onButtonListen(this.btn_close, ()=>{
            this.close()
        })
        this.onButtonListen(this.yinyue_btn_on, ()=>{
            this.updateYinyueStatus(false)
            audioManager.Instance().pauseHomeBgm()
        })
        this.onButtonListen(this.yinyue_btn_off, ()=>{
            this.updateYinyueStatus(true)
            audioManager.Instance().playHomeBgm()
        })
        this.onButtonListen(this.shengyin_btn_on, ()=>{
            this.updateShengyinStatus(false)
        })
        this.onButtonListen(this.shengyin_btn_off, ()=>{
            this.updateShengyinStatus(true)
        })
        this.onButtonListen(this.zhendong_btn_on, ()=>{
            this.updateZhendongStatus(false)
        })
        this.onButtonListen(this.zhendong_btn_off, ()=>{
            this.updateZhendongStatus(true)
        })

        // 彩蛋
        if(config.debug) {
            this.btn_clear_local_data.active = true
        } else{
            this.btn_clear_local_data.active = false
        }

        this.onButtonListen(this.btn_user, ()=>{
            this.lab_user.active = true
            this.lab_user.getComponent(Label).string = 'id:' + userDataManager.user_data.id
        })
        this.onButtonListen(this.btn_clear_local_data, ()=>{
            userDataManager.clearUserFreeAdsData()
        })

        this.initData()
    }

    initData() {
        let setting_data = GameManager.getSettingData()
        this.updateYinyueStatus(setting_data.isOpenYinYue)
        this.updateShengyinStatus(setting_data.isOpenYinXiao)
        this.updateZhendongStatus(setting_data.isOpenZhendong)
    }

    updateYinyueStatus(open:boolean) {
        if(open) {
            this.yinyue_btn_on.active = true
            this.yinyue_btn_off.active = false
        } else {
            this.yinyue_btn_on.active = false
            this.yinyue_btn_off.active = true
        }
        let setting_data = GameManager.getSettingData()
        if(open==setting_data.isOpenYinYue) {
            return
        }
        setting_data.isOpenYinYue = open
        GameManager.saveSettingData(setting_data)
    }

    updateShengyinStatus(open:boolean) {
        if(open) {
            this.shengyin_btn_on.active = true
            this.shengyin_btn_off.active = false
        } else {
            this.shengyin_btn_on.active = false
            this.shengyin_btn_off.active = true
        }
        let setting_data = GameManager.getSettingData()
        if(open==setting_data.isOpenYinXiao) {
            return
        }
        setting_data.isOpenYinXiao = open
        GameManager.saveSettingData(setting_data)
    }

    updateZhendongStatus(open:boolean) {
        if(open) {
            this.zhendong_btn_on.active = true
            this.zhendong_btn_off.active = false
        } else {
            this.zhendong_btn_on.active = false
            this.zhendong_btn_off.active = true
        }
        let setting_data = GameManager.getSettingData()
        if(open==setting_data.isOpenZhendong) {
            return
        }
        setting_data.isOpenZhendong = open
        GameManager.saveSettingData(setting_data)
    }
}


