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

class ThemeManager {

    static setBuildItemColor(cityId, node) {
        // 暂时除了2以外的城市都默认用第一阶的背景色
        if (cityId === 2) {
            node.color = new cc.color('#81f9c8');
        } else {
            node.color = new cc.color('#41b3e9');
        }
    }

    static getSpriteFrameByPath(path) {

        let filePath = `resources/building/${path}`;
        let uuid = cc.loader._getResUuid(filePath.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(`30${cityId}0${index}`);
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}0${index}`)
            .then((spriteFrame) => {
                buildSprite.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }
    }

    // 外墙顶部
    static setItemPillarTopSpriteFrame(cityId, pillar) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`30${cityId}08`);
            pillar.spriteFrame = spriteFrame;
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}08`)
            .then((spriteFrame) => {
                pillar.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }   
    }

    // 外墙底部
    static setItemPillarBottomSpriteFrame(cityId, pillar) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`30${cityId}09`);
            pillar.spriteFrame = spriteFrame;
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}09`)
            .then((spriteFrame) => {
                pillar.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }   
    }

    // 外墙右边
    static setItemPillarRightSpriteFrame(cityId, pillar) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`30${cityId}10`);
            pillar.spriteFrame = spriteFrame;
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}10`)
            .then((spriteFrame) => {
                pillar.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }   
    }

    // 外墙左边
    static setItemPillarLeftSpriteFrame(cityId, pillar) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`30${cityId}11`);
            pillar.spriteFrame = spriteFrame;
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}11`)
            .then((spriteFrame) => {
                pillar.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        }   
    }

    static setItemPillarSpriteFrame(cityId, pillars) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            let spriteFrame = this.getSpriteFrameByPath(`${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 setItemLockDownSpriteFrame(cityId, lockBottomBg) {
        cityId = cityId >= 3 ? 1 : cityId
        if (Global.buildRes) {
            lockBottomBg.spriteFrame = this.getSpriteFrameByPath(`30${cityId}12`);
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}12`)
            .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(`30${cityId}13`);
        } else {
            DWTool.loadResSpriteFrame(`./building/30${cityId}13`)
            .then((spriteFrame) => {
                buildSprite.spriteFrame = spriteFrame;
            }).catch((err) => {
                console.log(err);
            });
        } 
    }

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

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

module.exports = ThemeManager;