import { expect } from 'chai';

export function checkSuccessResponse(response, withData = true) {
    if (response.code != 0) {
        console.log('checkSuccessResponse info:', JSON.stringify(response))
    }
    expect(response).to.be.an('object');
    expect(response.code).equal(0);
    if (withData) {
        expect(response.data).to.be.an('object');
    }
}

export function checkDisplayItems(items) {
    expect(items).to.be.an('array');
    items.forEach(item => {
        expect(item.id).to.be.a('number');
        expect(item.count).to.be.a('number');
    })
}

export function checkBattleGoods(items) {
    expect(items).to.be.an('array');
    items.forEach(item => {
        if(item.dropType) expect(item.dropType).to.be.a('number');
        if(item.seqId) expect(item.seqId).to.be.a('number');
        expect(item.id).to.be.a('number');
        expect(item.count).to.be.a('number');
    })
}

export function checkHero(heroes) {
    expect(heroes).to.be.an('array');
    heroes.forEach(hero => {
        expect(hero.seqId).to.be.a('number');
        expect(hero.hid).to.be.a('number');
        expect(hero.hName).to.be.a('string');
        expect(hero.lv).to.be.a('number');
        expect(hero.exp).to.be.a('number');
        expect(hero.star).to.be.a('number');
        expect(hero.starStage).to.be.a('number');
        expect(hero.colorStar).to.be.a('number');
        expect(hero.colorStarStage).to.be.a('number');
        expect(hero.quality).to.be.a('number');
        expect(hero.job).to.be.a('number');
        expect(hero.jobStage).to.be.a('number');
        expect(hero.connections).to.be.an('array');
        hero.connections.forEach(connection => {
            expect(connection.shipId).to.be.a('number');
            expect(connection.level).to.be.a('number');
        });
        expect(hero.connections).to.be.an('array');
        hero.skins.forEach(skin => {
            expect(skin.id).to.be.a('number');
            expect(skin.enable).to.be.a('boolean');
        });
    })
}

export function addItem(pinusClient, id: number, count: number, cb) {
    pinusClient.request('role.heroHandler.addItem', { id, count }, res => {
        checkSuccessResponse(res);
        cb(res);
    });
}

export function addItemsIfNotEnough(roleInfo, pinusClient, id: number, count: number, cb) {
    if(id == 31001) {
        if(roleInfo.coin < count) {
            return addItem(pinusClient, id, count, () => {
                roleInfo.coin += count;
                cb();
            });
        }
    } else if (id == 31002) {
        if(roleInfo.gold < count) {
            return addItem(pinusClient, id, count, () => {
                roleInfo.gold += count;
            });
        }
    } else {
        let consumeGoods = roleInfo.consumeGoods||[];
        let cur = consumeGoods.find(cur => cur.id == id);
        if(!cur || cur.count < count) {
            return addItem(pinusClient, id, count, () => {
                if(!cur) {
                    consumeGoods.push({ id, count });
                } else {
                    cur.count += count;
                }
                roleInfo.consumeGoods = consumeGoods;
            });
        }
    }
    cb();
}

export function checkTimeStamp(time, len, canBeZero) {
    if(canBeZero) {
        if(time == 0) return true;
    } else {
        expect(time).to.be.a('number');
        expect(time.toString().length).to.equal(len);
    }
}

export function checkWarJson(heroes, cb?) {
    expect(heroes).to.be.an('array');
    heroes.forEach(hero => {
        expect(hero.actorId).to.be.a('number');
        expect(hero.skinId).to.be.a('number');
        expect(hero.actorName).to.be.a('string');
        expect(hero.dataId).to.be.a('number');
        expect(hero.relation).to.be.a('number');
        expect(hero.dirction).to.be.a('number');
        expect(hero.outIndex).to.be.a('number');
        expect(hero.x).to.be.a('number');
        expect(hero.y).to.be.a('number');
        expect(hero.var).to.be.a('number');
        expect(hero.lv).to.be.a('number');
        expect(hero.hide).to.be.a('number');
        expect(hero.initial_ai).to.be.a('number');
        expect(hero.attribute).to.be.a('string');
        expect(hero.star).to.be.a('number');
        expect(hero.skill).to.be.a('string');
        expect(hero.seid).to.be.a('string');
        expect(hero.spine).to.be.a('string');
        if(cb) cb(hero);
    })
}

export function checkRoleInfo(roleInfo) {
    expect(roleInfo.hasInit).to.be.a('boolean');
    expect(roleInfo.roleId).to.be.a('string');
    expect(roleInfo.roleName).to.be.a('string');
    expect(roleInfo.serverId).to.be.a('number');
    expect(roleInfo.ce).to.be.a('number');
    expect(roleInfo.topLineupCe).to.be.a('number');
    expect(roleInfo.lv).to.be.a('number');
    expect(roleInfo.exp).to.be.a('number');
    expect(roleInfo.gold).to.be.a('number');
    expect(roleInfo.coin).to.be.a('number');
    checkRoleHero(roleInfo.heros);
    checkConsumeGoods(roleInfo.consumeGoods);
    expect(roleInfo.title).to.be.a('number');
    checkTeraphs(roleInfo.teraphs);
    checkShowLineup(roleInfo.showLineup);
    checkFigure(roleInfo.heads);
    expect(roleInfo.head).to.be.a('number');
    checkFigure(roleInfo.frames);
    expect(roleInfo.frame).to.be.a('number');
    checkFigure(roleInfo.spines);
    expect(roleInfo.spine).to.be.a('number');
    expect(roleInfo.hasGuild).to.be.a('boolean');
    if(roleInfo.hasGuild) {
        expect(roleInfo.guildCode).to.be.a('string');
    }
    // checkTimeStamp(roleInfo.todayZeroPoint, 10, false);
    checkApJson(roleInfo.apJson);
    expect(roleInfo.totalPay).to.be.a('number')
    expect(roleInfo.guide).to.be.an('array');
    roleInfo.guide.forEach(guide => {
        expect(guide).to.be.a('number');
    });
}
export const ENTERY_ROLE_PICK = [   'todayZeroPoint', 'apJson', 'skins', 'totalPay', 'guide', 'hasInit', 'renameCnt'];


function checkRoleHero(heros) {
    expect(heros).to.be.an('array');
    heros.forEach(hero => {
        expect(hero.hid).to.be.a('number');
        expect(hero.seqId).to.be.a('number');
        expect(hero.exp).to.be.a('number');
        expect(hero.lv).to.be.a('number');
        expect(hero.ce).to.be.a('number');
        expect(hero.star).to.be.a('number');
        expect(hero.starStage).to.be.a('number');
        expect(hero.colorStar).to.be.a('number');
        expect(hero.colorStarStage).to.be.a('number');
        expect(hero.quality).to.be.a('number');
        expect(hero.scrollActive).to.be.a('boolean');
        expect(hero.scrollId).to.be.a('number');
        expect(hero.scrollStar).to.be.a('number');
        expect(hero.scrollColorStar).to.be.a('number');
        expect(hero.scrollQuality).to.be.a('number');
        expect(hero.job).to.be.a('number');
        expect(hero.jobStage).to.be.a('number');
        expect(hero.skinId).to.be.a('number');
        expect(hero.connections).to.be.an('array');
        hero.connections.forEach(connect => {
            expect(connect.shipId).to.be.a('number');
            expect(connect.level).to.be.a('number');
        });
        expect(hero.skins).to.be.an('array');
        hero.skins.forEach(skin => {
            expect(skin.id).to.be.a('number');
            expect(skin.enable).to.be.a('boolean');
            expect(skin.skinId).to.be.a('number');
        });
        expect(hero.ePlace).to.be.an('array');
        hero.ePlace.forEach(eplace => {
            expect(eplace.id).to.be.a('number');
            expect(eplace.lv).to.be.a('number');
        });
    })
}

function checkConsumeGoods(consumes) {
    expect(consumes).to.be.an('array');
    consumes.forEach(consume => {
        expect(consume.id).to.be.a('number');
        expect(consume.count).to.be.a('number');
        expect(consume.type).to.be.a('number');
    })
}

function checkTeraphs(teraphs) {
    expect(teraphs).to.be.an('array');
    teraphs.forEach(teraph => {
        expect(teraph.id).to.be.a('number');
        expect(teraph.grade).to.be.a('number');
        expect(teraph.hp).to.be.a('number');
        expect(teraph.atk).to.be.a('number');
        expect(teraph.def).to.be.a('number');
        expect(teraph.mdef).to.be.a('number');
        expect(teraph.count).to.be.a('number');
        expect(teraph.criCount).to.be.a('number');
    })
}

function checkShowLineup(lineup) {
    expect(lineup).to.be.an('array');
    lineup.forEach(hid => {
        expect(hid).to.be.a('number');
    })
}

function checkFigure(figures) {
    expect(figures).to.be.an('array');
    figures.forEach(figure => {
        expect(figure.id).to.be.a('number');
        expect(figure.enable).to.be.a('boolean');
        expect(figure.status).to.be.a('boolean');
    })
}

function checkApJson(apJson) {
    expect(apJson).to.be.an('object');
    expect(apJson.ap).to.be.a('number');
    expect(apJson.maxAp).to.be.a('number');
    expect(apJson.apRemainTime).to.be.a('number');
    expect(apJson.apMaxRemainTime).to.be.a('number');
    expect(apJson.buyTimes).to.be.a('number');
}

function checkSkins(skins) {
    expect(skins).to.be.an('array');
    skins.forEach(skin => {
        expect(skin.id).to.be.a('number');
        expect(skin.skinId).to.be.a('number');
        expect(skin.skinName).to.be.a('string');
        expect(skin.hid).to.be.a('number');
    })
}