import { Client } from './Client';
import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { checkSuccessResponse, checkDisplayItems } from './CheckPatten';
import { DEBUG_MAGIC_WORD, TASK_FUN_TYPE } from '../app/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();
            }
        }, 1000);
    });

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

    it('检查entery中返回的任务状态', function (done) {
        checkMainTask(roleInfo.mainTask);
        checkDailyTask(roleInfo.dailyTask);
        checkAchievement(roleInfo.achievement);
        checkPvpTask(roleInfo.pvpTask);
        done();
    });

    it('检查领取主线任务', function (done) {
        const type = TASK_FUN_TYPE.MAIN;
        // 完成所有主线任务
        pinusClient.request('role.taskHandler.debugRefTask', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
            checkSuccessResponse(res);
            pinusClient.request('role.taskHandler.debugCompleteMainStage', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
                checkSuccessResponse(res);
                expect(res.data.mainTask.taskList).to.be.an('array');
                expect(res.data.mainTask.stage).to.be.a('number');
                let promiseFuns = res.data.mainTask.taskList.map(task => {
                    return receiveTaskPromise(pinusClient, { type, id: task.id });
                });
                Promise.all(promiseFuns).then(() => {
                    pinusClient.request('role.taskHandler.receiveMainStage', { stage: res.data.mainTask.stage }, (res) => {
                        checkSuccessResponse(res);
                        checkMainTask(res.data);
                        checkDisplayItems(res.data.goods);
                        done();
                    })
                })
            });

        });
    });
    it('检查领取每日任务', function (done) {
        checkReceiveTask(pinusClient, done, TASK_FUN_TYPE.DAILY);
    });
    it('检查领取成就', function (done) {
        checkReceiveTask(pinusClient, done, TASK_FUN_TYPE.ACHIEVEMENT);
    });
    it('领取每日活跃奖励', function (done) {
        const type = TASK_FUN_TYPE.DAILY;
        // 完成所有主线任务
        pinusClient.request('role.taskHandler.debugRefTask', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
            checkSuccessResponse(res);
            pinusClient.request('role.taskHandler.debugIncPoint', { magicWord: DEBUG_MAGIC_WORD, type, point: 500 }, (res) => {
                checkSuccessResponse(res);
                pinusClient.request('role.taskHandler.receiveActiveReward', {}, (res) => {
                    checkSuccessResponse(res);
                    expect(res.data.point).to.be.a('number');
                    checkDisplayItems(res.data.goods);
                    done();
                })
            });

        });
    });
    it('检查领取pvp任务', function (done) {
        checkReceiveTask(pinusClient, done, TASK_FUN_TYPE.PVP);
    });

    it('领取每周活跃奖励', function (done) {
        checkReceiveBox(pinusClient, done, TASK_FUN_TYPE.DAILY)
    });

    it('领取成就积分宝箱', function (done) {
        checkReceiveBox(pinusClient, done, TASK_FUN_TYPE.ACHIEVEMENT)
    });
});

function receiveTaskPromise(pinusClient, param) {
    return new Promise(resolve => {
        receiveTask(pinusClient, param, resolve)
    })
}

function receiveTask(pinusClient, param, resolve) {
    pinusClient.request('role.taskHandler.receiveTask', param, (res) => {
        checkSuccessResponse(res);
        expect(res.data.type).to.be.a('number');
        expect(res.data.id).to.be.a('number');
        expect(res.data.count).to.be.a('number');
        expect(res.data.received).equal(true);
        expect(res.data.point).to.be.a('number');
        expect(res.data.weeklyPoint).to.be.a('number');
        checkDisplayItems(res.data.goods);
        resolve();
    });
}

// 检查领取
function checkReceiveTask(pinusClient, done, type: number) {
    // 清空任务
    pinusClient.request('role.taskHandler.debugRefTask', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
        checkSuccessResponse(res);
        // 完成一个任务
        pinusClient.request('role.taskHandler.debugCompleteRandTask', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
            checkSuccessResponse(res);
            expect(res.data.task).to.be.an('object');
            expect(res.data.task.id).to.be.a('number');
            receiveTask(pinusClient, { type, id: res.data.task.id }, done);

        });
    });
}


// 检查主线任务数据
export function checkMainTask(mainTask) {
    if (!mainTask) return;
    expect(mainTask).to.be.an('object');
    expect(mainTask.stage).to.be.a('number');
    expect(mainTask.taskList).to.be.an('array');
    mainTask.taskList.forEach(task => checkSingleTask(task));
}

// 检查日常任务数据
export function checkDailyTask(dailyTask) {
    if (!dailyTask) return;
    expect(dailyTask).to.be.an('object');
    expect(dailyTask.point).to.be.a('number');
    expect(dailyTask.weeklyPoint).to.be.an('number');
    dailyTask.taskList.forEach(task => checkSingleTask(task));
    dailyTask.box.forEach(id => {
        expect(id).to.be.a('number');
    })
}

// 检查成就
export function checkAchievement(achievement) {
    if (!achievement) return;
    expect(achievement).to.be.an('object');
    expect(achievement.point).to.be.a('number');
    achievement.taskList.forEach(task => checkSingleTask(task));
    achievement.box.forEach(id => {
        expect(id).to.be.a('number');
    })
}

// 检查pvp任务数据
export function checkPvpTask(pvpTask) {
    if (!pvpTask) return;
    expect(pvpTask.taskList).to.be.an('array');
    pvpTask.taskList.forEach(task => checkSingleTask(task));
}

// 检查在taskList中的某一个任务数据
function checkSingleTask(task) {
    expect(task).to.be.an('object');
    expect(task.type).to.be.a('number');
    expect(task.id).to.be.a('number');
    expect(task.count).to.be.a('number');
    expect(task.received).to.be.a('boolean');
}

function checkReceiveBox(pinusClient, done, type: number) {
    // 完成所有主线任务
    pinusClient.request('role.taskHandler.debugRefTask', { magicWord: DEBUG_MAGIC_WORD, type }, (res) => {
        checkSuccessResponse(res);
        pinusClient.request('role.taskHandler.debugIncPoint', { magicWord: DEBUG_MAGIC_WORD, type, point: 500 }, (res) => {
            checkSuccessResponse(res);
            pinusClient.request('role.taskHandler.receiveBox', { type, id: 1 }, (res) => {
                checkSuccessResponse(res);
                expect(res.data.type).to.be.a('number');
                expect(res.data.point).to.be.a('number');
                expect(res.data.weeklyPoint).to.be.a('number');
                res.data.box.forEach(id => {
                    expect(id).to.be.a('number');
                })
                checkDisplayItems(res.data.goods);
                done();
            })
        });

    });
}