"use strict";
cc._RFpush(module, 'e7247jWIZJADatqvKkyKgPg', 'hello');
// Script/hello.js

var _ = require('underscore');

cc.Class({
    "extends": cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function onLoad() {
        this.assets = [];
    },

    /**
     * @function add
     * @description 资源加载
     */
    add: function add() {
        cc.loader.loadResAll("Poker", (function (err, assets) {
            if (err) {
                cc.log("资源加载失败");
            }

            cc.log("资源加载成功");
            this._parseRes(assets);
        }).bind(this));
    },

    /**
     * @function del
     * @description 释放资源
     */
    del: function del() {

        //releaseAll() 释放所有资源
        //cc.loader.releaseAll();

        //releaseAsset
        //cc.loader.releaseAsset("Poker/mahjong_poker");
        //cc.loader.releaseAsset("mahjong_poker");

        //releaseRes(url)
        //cc.loader.releaseRes("Poker/mahjong_poker");
        //cc.loader.releaseRes("mahjong_poker");

        //release asset/rawasset/string/array
        //cc.loader.release('poker_bg_down');
        //cc.loader.release('Poker/mahjong_poker');
        cc.loader.release('mahjong_poker');
    },

    remove: function remove() {
        this.node.getChildByName("Sprite").getComponent(cc.Sprite).spriteFrame = null;
    },

    /**
     * @function showImage
     * @description 显示内存中的一张图片
     */
    showImage: function showImage() {
        this.node.getChildByName("Sprite").getComponent(cc.Sprite).spriteFrame = this.assets.POKER_BG_DOWN;
        var a = this.assets;
        //debugger;
    },

    _parseRes: function _parseRes(list, func) {
        list.forEach((function (item, index) {
            //cc.log("_parseRes >>> loop index:%s", index);
            if (item instanceof cc.Texture2D) {
                var key = "";
                cc.log("_parseRes >> item.url:%s getName():%s item.name:%s", item.url, item.getName(), item.name);
                if (cc.sys.isNative) {
                    key = item.getName(); //getPath();
                } else {
                        var url = item.url;
                        var split_url = url.split('/');
                        var sub_url = split_url[split_url.length - 1].split('.');
                        key = sub_url[0].toUpperCase();
                    }

                this.assets[key] = item;
            }
            if (item instanceof cc.SpriteFrame) {
                //cc.log("_parseRes >> SpriteFrame item.name:%s",item.name);

                this.assets[item.name.toUpperCase()] = item;
            }

            if (item instanceof cc.Prefab) {
                //cc.log("_parseRes >> SpriteAtlas item.name:%s",item.name);
                this.assets[item.name.toUpperCase()] = item;
            }

            if (item instanceof cc.BitmapFont) {
                //cc.log("_parseRes >> SpriteAtlas item.name:%s",item.name);
                this.assets[item.name.toUpperCase()] = item;
            }

            if (item instanceof cc.SpriteAtlas) {
                //cc.log("_parseRes >> SpriteAtlas item.name:%s",item.name);
                var split_list = item.name.split('.');
                var atlas_list = {};
                _.forEach(item._spriteFrames, function (item) {
                    atlas_list[item.name.toUpperCase()] = item;
                });
                this.assets[split_list[0].toUpperCase()] = atlas_list;
            }

            if (item instanceof cc.AnimationClip) {
                //cc.log("_parseRes >> SpriteAtlas item.name:%s",item.name);
                this.assets[item.name.toUpperCase()] = item;
            }
        }).bind(this));

        //debugger;
        cc.log("资源解析完成");
        this.count++;
    }
});
// called every frame, uncomment this function to activate update callback
// update: function (dt) {

// },

cc._RFpop();