import { assetManager, Node, ProgressBar, TextAsset, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
import { constants } from '../data/constants';
import { audioManager } from '../manager/audioManager';
import { designManager } from '../manager/designManager';
import { iconManager } from '../manager/iconManager';
import { layerManager } from '../manager/layerManager';
import { levelManager } from '../manager/levelManager';
import { resManager } from '../manager/resManager';
import { sdkManager } from '../manager/sdkManager';
import { playerModel } from '../model/playerModel';
const { ccclass, property } = _decorator;

@ccclass('LoadingLayer')
export class LoadingLayer extends BaseLayer {

    progressBar: ProgressBar;
    progress: number = 0;
    percentNode: Node;

    flagNode: Node;
    progressLength: number = 500;
    startGap: number = 250;

    designResProgress: number = 0;
    designResPercent: number = 0.3;

    audioResProgress: number = 0;
    audioResPercent: number = 0.3;

    commonResLayerProgress: number = 0;
    commonResLayerPercent: number = 0.1;

    commonResIconProgress: number = 0;
    commonResIconPercent: number = 0.1;

    commonResLevelProgress: number = 0;
    commonResLevelPercent: number = 0.2;



    onLoad() {

        sdkManager.instance.init(()=>{
            sdkManager.instance.sendEvent("进入加载页");
        });

        super.onLoad();

        this.percentNode = this.getNodeByPath("progressBar/percent");
        this.setString(this.percentNode, "0%");

        this.progressBar = this.getNodeByPath("progressBar").getComponent(ProgressBar);
        this.setProgressBar(this.progressBar, this.progress);

        this.flagNode = this.getNodeByPath("progressBar/flag");
        let pos = this.flagNode.getPosition();
        pos.x = -this.startGap;
        this.flagNode.setPosition(pos);

    }

    onEnable() {
        this.loadDesignRes();
        this.loadAudioRes();
        assetManager.loadBundle(constants.bundles.common.name, (err, retBundle) => {
            this.loadCommonIconRes();
            this.loadCommonLevelRes();
            this.loadCommonLayerRes();
        });
    }

    onDisable() {

    }

    loadAudioRes() {
        // 加载音频
        audioManager.instance.loadAudio((finish: number, total: number) => {
            this.audioResProgress = finish / total;
        }, () => {
            this.audioResProgress = 1;
        });
    }

    loadDesignRes() {
        // 加载策划表格数据
        resManager.instance.loadAssetByBundleDir(constants.bundles.design.name, constants.bundles.design.dir.common, TextAsset,
            (finish: number, total: number, item: any) => {
                this.designResProgress = finish / total;
            }, (assetArr) => {
                this.designResProgress = 1;
                for (let i in assetArr) {
                    let asset = assetArr[i];
                    designManager.instance.initTable(asset.name, asset.text.split("\n"));
                    resManager.instance.releaseAsset(asset);
                }
            }
        );

    }

    loadCommonLayerRes() {
        // 加载layer资源
        layerManager.instance.loadLayers((finish: number, total: number) => {
            this.commonResLayerProgress = finish / total;
        }, () => {
            this.commonResLayerProgress = 1;
        });
    }

    loadCommonIconRes() {
        // 加载需要动态替换的图标资源
        iconManager.instance.loadAllIcons((finish: number, total: number, item: any) => {
            this.commonResIconProgress = finish / total;
        }, () => {
            this.commonResIconProgress = 1;
        });
    }

    loadCommonLevelRes() {
        // 加载当前关卡数据
        levelManager.instance.loadLevel(playerModel.instance.getLevel(), (finish, total) => {
            this.commonResLevelProgress = finish / total;
        }, () => {
            this.commonResLevelProgress = 1;
        });
    }

    openStartLayer() {
        // 额外加载
        // skillManager.instance.loadAllSkillEffect();

        this.openLayer(constants.layers.StartLayer);
        this.closeLayer();
    }

    onButtonClick(node: Node, name: String) {
        switch (name) {
            default:
                break;
        }
    }

    update() {
        let progress = this.designResProgress * this.designResPercent
            + this.audioResProgress * this.audioResPercent
            + this.commonResLayerProgress * this.commonResLayerPercent
            + this.commonResIconProgress * this.commonResIconPercent
            + this.commonResLevelProgress * this.commonResLevelPercent;
        if (progress > this.progressBar.progress) {
            this.setProgressBar(this.progressBar, progress);
            this.setString(this.percentNode, Math.ceil(progress * 100) + "%");

            let pos = this.flagNode.getPosition();
            pos.x = progress * this.progressLength - this.startGap;
            this.flagNode.setPosition(pos);
        }
        if (progress >= 1) {
            this.openStartLayer();
        }
    }

}

