
const DWTool = require("./DWTool");

class ThemeManager {

    static getSpriteFrameByPath(path) {
        let uuid = cc.loader._getResUuid(path.slice(10), cc.Texture2D, true);
    
        let result = Global.buildRes.filter((item) => { return item._uuid === uuid });
        if (result != null && result.length != undefined) {
            let texture = result[0];
            return new cc.SpriteFrame(texture);
        }
        return null;
    }

    // LevelHomeItem 样式变化
    static setItemBuildSpriteFrame(cityId, buildSprite, index) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            buildSprite.spriteFrame = this.getSpriteFrameByPath(`resources/building/${cityId}_${index}`);
        } else {
            DWTool.loadResSpriteFrame(`./building/${cityId}_${index}`)
            .then((spriteFrame) => {
                buildSprite.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }
    }

    static setItemPillarSpriteFrame(cityId, pillars) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`resources/building/${cityId}_pillar`);
            for (let i = 0; i < pillars.length; i++) {
                let sprite = pillars[i];
                sprite.spriteFrame = spriteFrame;
            }
        } else {
            DWTool.loadResSpriteFrame(`./building/${cityId}_pillar`)
            .then((spriteFrame) => {
                for (let i = 0; i < pillars.length; i++) {
                    let sprite = pillars[i];
                    sprite.spriteFrame = spriteFrame;
                }
            }).catch((err) => {
                console.log(err);
            });
        }   
    }

    static setItemDownSpriteFrame(cityId, bottomBg) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            bottomBg.spriteFrame = this.getSpriteFrameByPath(`resources/building/${cityId}_down`);
        } else {
            DWTool.loadResSpriteFrame(`./building/${cityId}_down`)
            .then((spriteFrame) => {
                bottomBg.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        } 
    }

    static setItemLockDownSpriteFrame(cityId, lockBottomBg) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            lockBottomBg.spriteFrame = this.getSpriteFrameByPath(`resources/building/${cityId}_down_lock`);
        } else {
            DWTool.loadResSpriteFrame(`./building/${cityId}_down_lock`)
            .then((spriteFrame) => {
                lockBottomBg.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }   
    }


    // LevelHomeBottom 样式变化
    static setBottomBuildSpriteFrame(cityId, buildSprite) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            buildSprite.spriteFrame = this.getSpriteFrameByPath(`resources/building/${cityId}_bottom`);
        } else {
            DWTool.loadResSpriteFrame(`./building/${cityId}_bottom`)
            .then((spriteFrame) => {
                buildSprite.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        } 
    }

    // LevelHomeTop 样式变化
}

module.exports = ThemeManager;