import { Client } from './Client';
import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { checkSuccessResponse, checkDisplayItems, checkHero, addItem } from './CheckPatten';
import { GACHA_TYPE } from '../../shared/consts';


describe('抽卡测试', function () {
    let pinusClient: PinusWSClient;
    let roleInfo;

    before(function (done) {
        const c = new Client();
        const timer = setInterval(() => {
            if (c.client) {
                pinusClient = c.client;
                roleInfo = c.roleInfo;
                clearInterval(timer);
                done();
            }
        }, 500);
    });

    after(function (done) {
        pinusClient.disconnect();
        // disconnect 后等待 500ms，供服务器清理环境、退出频道等
        setTimeout(() => {
            done();
        }, 500);
    });

    it('检查获取抽卡列表', function (done) {
        pinusClient.request('activity.gachaHandler.getGachaList', { }, (res) => {
            checkSuccessResponse(res);
            expect(res.data.list).to.be.an('array');
            res.data.list.forEach(gacha => {
                expect(gacha.gachaId).to.be.a('number');
                expect(gacha.isFree).to.be.a('boolean');
                expect(gacha.count).to.be.a('number');
                if(gacha.gachaId == GACHA_TYPE.NORMAL) {
                    checkHope(gacha.hope);
                    expect(gacha.point).to.be.a('number');
                    checkTurntable(gacha.turntable);
                }
                if(gacha.gachaId == GACHA_TYPE.ASSIGN) {
                    expect(gacha.pickHero).to.be.a('number');
                }
            });
            done();
        });
    });

    it('检查元宝招募', function (done) {
        let gachaId = GACHA_TYPE.ASSIGN;
        let hid1 = 1, hid2 = 2;
        checkSetHope(pinusClient, gachaId, hid1, hid2, function(res) {
            addItem(pinusClient, 22001, 10, function(res) {
                checkPull(pinusClient, done, GACHA_TYPE.NORMAL, 0, 1);
            });
        })
    });

    it('检查友情点招募', function (done) {
        addItem(pinusClient, 40003, 1000, function(res) {
            checkPull(pinusClient, done, GACHA_TYPE.FRDPOINT, 0, 1);
        });
    });

    it('检查求贤若渴招募', function (done) {
        let gachaId = GACHA_TYPE.ASSIGN;
        let pickHero = 1;
        addItem(pinusClient, 22002, 10, function(res) {
            checkSetPickHero(pinusClient, gachaId, 0, pickHero, (res) => {
                checkPull(pinusClient, done, GACHA_TYPE.ASSIGN, 0, 1);
            })
        });
    });
});

function checkFloor(floor) {
    expect(floor).to.be.an('array');
    floor.forEach(f => {
        expect(f.id).to.be.a('number');
        expect(f.count).to.be.a('number');
    })
}

function checkHope(hope) {
    expect(hope).to.be.an('array');
    hope.forEach(f => {
        expect(f.id).to.be.a('number');
        expect(f.hid).to.be.a('number');
        expect(f.hasGet).to.be.a('boolean');
    })
}

function checkTurntable(turntable) {
    expect(turntable).to.be.an('array');
    turntable.forEach(f => {
        expect(f.quality).to.be.a('number');
        expect(f.hasGet).to.be.a('boolean');
    })
}

function checkPull(pinusClient, done, gachaId, activityId, count) {

    pinusClient.request('activity.gachaHandler.pull', { gachaId, activityId, count }, (res) => {
        checkSuccessResponse(res);
        expect(res.data.gachaId).to.be.a('number');
        checkResult(res.data.result, count);
        expect(res.data.isFree).to.be.a('boolean');
        expect(Date.parse(res.data.refFreeTime)).to.be.a('number');
        expect(res.data.count).to.be.a('number');
        checkFloor(res.data.floor);
        expect(res.data.point).to.be.a('number');
        checkHope(res.data.hope);
        // checkHero(res.data.heroes);

        done()
    });
}

function checkResult(result, count) {
    expect(result).to.be.an('array');
    expect(result.length).equal(count);
    result.forEach(r => {
        expect(r.planId).to.be.a('number');
        expect(r.hid).to.be.a('number');
        expect(r.isTransfer).to.be.a('boolean');
        expect(r.id).to.be.a('number');
        expect(r.count).to.be.a('number');
    })
}

function checkSetPickHero(pinusClient, gachaId, activityId, pickHero, cb) {
    pinusClient.request('activity.gachaHandler.setPickHero', { gachaId, activityId, pickHero }, (res) => {
        checkSuccessResponse(res);
        expect(res.data.gachaId).equal(gachaId);
        expect(res.data.activityId).equal(activityId);
        expect(res.data.pickHero).equal(pickHero);

        cb(res);
    });
}

function checkSetHope(pinusClient, gachaId, hid1, hid2, cb) {
    pinusClient.request('activity.gachaHandler.setHope', { gachaId, hope: [{id: 1, hid: hid1}, {id:2, hid: hid2}] }, (res) => {
        checkSuccessResponse(res);
        expect(res.data.gachaId).equal(gachaId);
        checkHope(res.data.hope)
        cb(res);
    });
}