import { _decorator, Button, Component, director, game, instantiate, Node, Sprite, tween, Vec3 } from 'cc';
import ResMgr from './ResMgr';
import { AssetType, Clips } from './Enums';
import { AudioMgr } from './AudioMgr';
import { Tools } from './Tools';
import { Global } from './Global';
import { PoolMgr } from './PoolMagr';
import { LevelAction } from './LevelAction';
import { WxPlatform } from './ad/WxPlatform';
import { LayerAction } from './LayerAction';
import { ElementAction } from './ElementAction';
const { ccclass, property } = _decorator;

@ccclass('HomeAction')
export class HomeAction extends Component {

    @property({ type: Node })
    logo: Node = null;
    @property(Sprite)
    load: Sprite = null;
    @property({ type: Node })
    btns_layer: Node = null;
    private isload = true;
    load_num = 0;

    @property({ type: String })
    ad_intersitial_id: string;
    @property({ type: String })
    ad_video_id: string;



    onLoad() {
        console.log("zh:homeAction.ts onLoad");
        game.frameRate = 60;
    }
    async start() {
        this.btns_layer.active = false;
        Global.ad_intersitial_id = this.ad_intersitial_id;
        Global.ad_video_id = this.ad_video_id;
        WxPlatform.ins.wechat_setting();
        await this.loadRes();
    }

    async loadRes() {
        director.preloadScene("main", () => {
            console.log(">>>main scene preload completed!!");
            this.show_btns();
        });
        await ResMgr.ins.loadBundle(1, 0.1);
        await ResMgr.ins.loadRes(1, AssetType.Prefab, 0.6)
        await ResMgr.ins.loadRes(1, AssetType.Sound, 0.32)
    }

    start_game() {
        AudioMgr.ins.playSound(Clips.btn_1);
        director.loadScene("main");
    }

    /**
     * 显示按钮
     */
    show_btns() {
        this.load_num++;
        if (this.load_num >= 2) {
            this.load.node.parent.parent.active = false;
            this.btns_layer.active = true;
        }

    }


    update(deltaTime: number) {
        if (!this.isload) return;
        this.load.fillRange = Global.LoadingRate;
        if (Global.LoadingRate >= 0.99) {
            this.isload = false;
            this.show_btns();
        }
    }

    check_lvl_data() {
        console.log("测试数据安全性");
        for (let lvl = 1; lvl <= 50; lvl++) {
            let level_prebab = PoolMgr.ins.getPrefab("lvl_" + lvl);
            if (!level_prebab) {
                console.log(lvl, "没有这个等级");
                break;
            }
            let lvl_node = instantiate(level_prebab);
            let pin_total = lvl_node.getComponent(LevelAction).get_hole_num();
            console.log(lvl, "关，钉子总数：", pin_total, "取模:", pin_total % 3);
            {
                lvl_node.children.forEach(layer_node => {
                    // console.log("init_level this.node>>>>>:", this.node);
                    let layer = layer_node.getComponent(LayerAction);
                    if (!layer_node.active) {
                        console.log("错误》〉", lvl, "关 ", layer_node.name, " layer 是隐藏的");
                    }
                    if (!layer) {
                        console.log("错误》〉", lvl, "关 ", layer_node.name, " layer action错误");
                    }
                    layer_node.children.forEach(ele_node => {
                        if (!ele_node.active) {
                            console.log("错误》〉", lvl, "关 ", layer_node.name, " layer 节点>>", ele_node.name, " 是隐藏的");
                        }
                        if (!ele_node.getComponent(ElementAction)) {
                            console.log("错误》〉", lvl, "关 ", layer_node.name, " layer 节点>>", ele_node.name, " action错误");
                        }
                    })
                    // console.log("获取钉子数量：",num,layer_node.active);
                });
            }
        }
    }
}

