import Common5 from "../../Platform/th/Common5";
import SoundMgr from "../../FrameWork/Mgr/SoundMgr";
import VibrateMgr from "../../FrameWork/Mgr/VibrateMgr";
import User from "../../FrameWork/User/User";
import EventMgr from "../../FrameWork/Event/EventMgr";
import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import InterfaceManager from "../Manager/InterfaceManager";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import UiBase from "../GameBase/UiBase";
import CommonDefine from "../../Platform/th/CommonDefine";
import TaskManager from "../JuQingChat/TaskManager";
const { ccclass, property } = cc._decorator;

@ccclass
export default class Setting extends UiBase {

    @property(cc.Node)
    resetNode: cc.Node = null;

    @property(cc.Node)
    headIconLayout: cc.Node = null;

    @property(cc.Node)
    selectIcon: cc.Node = null;

    protected headImgList: Array<string> = ["headImg1", "headImg2", "headImg3", 'headImg4'];
    onLoad() {

    }

    start() {
        this.updateShow();
        this.updateHeadImg()
        if (Common5.ISSHOWGM) {
            this.node.getChildByName('GMBtn').active = true
        }


    }
    updateShow() {
        this.node.getChildByName("setNode").getChildByName("yinXiao").getChildByName("off").active = Common5.isNoMusic;
        this.node.getChildByName("setNode").getChildByName("yinXiao").getChildByName("on").active = !Common5.isNoMusic;

        this.node.getChildByName("setNode").getChildByName("zhenDong").getChildByName("off").active = Common5.isNoZD;
        this.node.getChildByName("setNode").getChildByName("zhenDong").getChildByName("on").active = !Common5.isNoZD;
    }


    updateHeadImg() {
        let curImg = User.getHeadImg()
        let imgs = User.getHeadImgUnlock();
        for (var i = 0; i < this.headImgList.length; i++) {
            imgs.forEach((ele) => {
                if (ele == this.headImgList[i]) {
                    this.headIconLayout.getChildByName("icon_" + i).getChildByName("adIcon").active = false;
                }
            })

            Common5.addUrlSprite_custom("head/" + this.headImgList[i], this.headIconLayout.getChildByName("icon_" + i).getChildByName("img").getComponent(cc.Sprite));

            if (this.headImgList[i] == curImg) {
                Common5.setNodeToTargetPos(this.selectIcon, this.headIconLayout.getChildByName("icon_" + i))



            }
        }

    }
    onHeadImgClick(event, customData) {
        //Common5.playEffect("sound/按键点击")
        let imgName = this.headImgList[customData]
        if (this.headIconLayout.getChildByName("icon_" + customData).getChildByName("adIcon").active) {
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-设置更换头像`)
                        // Common5.ReportDY("inLevel", "设置-AD-更换头像" + customData);
                        User.setHeadImg(imgName);
                        User.setHeadImgUnlock(imgName);
                        this.headIconLayout.getChildByName("icon_" + customData).getChildByName("adIcon").active = false;

                        EventMgr.emitEvent_custom(ryw_Event.setHeadImg, imgName);
                        Common5.setNodeToTargetPos(this.selectIcon, this.headIconLayout.getChildByName("icon_" + customData))
                    }
                    else {
                        Common5.showTips_custom("广告未观看完");
                    }

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)
        } else {
            User.setHeadImg(imgName);
            EventMgr.emitEvent_custom(ryw_Event.setHeadImg, imgName);
            Common5.setNodeToTargetPos(this.selectIcon, this.headIconLayout.getChildByName("icon_" + customData))
        }


    }
    onBtnCloseClick() {

        //Common5.playEffect("sound/按键点击")
        let name = this.node.name

        this.node.removeFromParent()
        this.node.destroy()

        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, name);
    }
    onBtnMusicClick() {
        //Common5.playEffect("sound/按键点击")
        // //Common5.playEffect("sound/按键点击")
        Common5.isNoMusic = !Common5.isNoMusic;
        Common5.isNoEffect = Common5.isNoMusic;
        SoundMgr.setSoundEnabled_custom(!Common5.isNoMusic);

        if (Common5.isNoMusic == true) {
            cc.audioEngine.stopMusic();
        } else {
            Common5.playRemoteAudioMusic('sound/common/背景音')

        }
        this.updateShow();
    }
    onBtnVibrateClick() {
        //Common5.playEffect("sound/按键点击")
        // //Common5.playEffect("sound/按键点击")
        Common5.isNoZD = !Common5.isNoZD;
        VibrateMgr.isEnable_custom = !Common5.isNoZD;
        this.updateShow();
    }
    onBtnRestartClick() {
        //Common5.playEffect("sound/按键点击")
        this.resetNode.active = true
    }
    onBtnYesClick() {

        console.log('onBtnYesClick')
        //Common5.playEffect("sound/按键点击")
        if (!AppPlatform.is_TT_GAME_custom()) {
            Common5.isVideoAdStopTime = true

            cc.sys.localStorage.clear();
            User.restartGame()
            this.node.removeFromParent()
            cc.director.loadScene("MainScene", function (err, scene) {
                console.log("跳转游戏场景 MainScene");
                Common5.isVideoAdStopTime = false

            });
        } else {
            Common5.isVideoAdStopTime = true

            cc.sys.localStorage.clear();
            User.restartGame()
            this.node.removeFromParent()
            cc.director.loadScene("MainScene", function (err, scene) {
                console.log("跳转游戏场景 MainScene");
                Common5.isVideoAdStopTime = false
            });
        }
    }
    onBtnNoClick() {
        //Common5.playEffect("sound/按键点击")
        this.resetNode.active = false
    }
    onBtnSaveClick() {
        //Common5.playEffect("sound/按键点击")

        CommonDefine.saveData(true)
        PrefabManage.showTextTips('数据存档成功!')
    }

    gmClickEvent() {
        PrefabManage.loadPrefabByType(GameType.GM)
    }
    // update (dt) {}
}