import { Client } from './Client';
import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { checkSuccessResponse } from './CheckPatten';
import { BLOCK_OPEATE } from '../app/consts';

describe('好友测试测试', function() {

  let pinusClient: PinusWSClient;
  let pinusClientT: PinusWSClient; // 用做测试队友
  let roleInfo;
  let roleInfoT;
  let applyCode;

  before(function(done) {
    const c = new Client();
    const cT = new Client('13121622738');
    const timer = setInterval(() => {
      if (c.client && cT.client) {
        pinusClient = c.client;
        roleInfo = c.roleInfo;
        pinusClientT = cT.client;
        roleInfoT = cT.roleInfo;
        clearInterval(timer);
        done();
      }
    }, 1000);
  });

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

  it('获取推荐列表', function(done) {
    pinusClient.request('role.friendHandler.getRecommend', {} , (res) => {
        checkSuccessResponse(res);
        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkFriendRecommendParams(cur);
        })
      done();
    });
  });

  it('查询好友', function(done) {
    pinusClient.request('role.friendHandler.searchUser', { value: roleInfoT.roleId } , (res) => {
        checkSuccessResponse(res);
        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkFriendRecommendParams(cur);
        })
        done();
    });
  });

  it('清空申请', function(done) {
    pinusClientT.request('role.friendHandler.getApplyList', {}, (res) => {

      checkSuccessResponse(res);
      expect(res.data.list).to.be.a('array');
      res.data.list.forEach(cur => {
        checkFriendApplyParams(cur);
      });

      const applyCodes = res.data.list.map(apply => {
        return apply.applyCode;
      });
      pinusClientT.request('role.friendHandler.handleApply', { applyCodeList: applyCodes, isPass: false }, (res) => {
        checkSuccessResponse(res);
        expect(res.data.isSuccess).to.be.a('boolean');
        expect(res.data.msg).to.be.a('string');

        expect(res.data.applyCodeList).to.be.a('array');
        res.data.applyCodeList.forEach(cur => {
          expect(cur).to.be.a('string');
        })

        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
          checkFriendListParam(cur);
        });

        expect(res.data.friendCnt).to.be.a('number');
        done();
      });
    });
  });

  it('申请好友', function(done) {
    console.log('#####', roleInfoT.roleId)
    pinusClient.request('role.friendHandler.applyFriend', { roleIds: [roleInfoT.roleId] } , (res) => {
        checkSuccessResponse(res);
        expect(res.data.isSuccess).to.be.a('boolean');
        expect(res.data.msg).to.be.a('string');
        expect(res.data.roleIds).to.be.a('array');
        res.data.roleIds.forEach(cur => {
            expect(cur).to.be.a('string');
        })
      done();
    });
  });

  it('获取申请列表', function(done) {
    pinusClientT.request('role.friendHandler.getApplyList', {} , (res) => {

        checkSuccessResponse(res);
        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkFriendApplyParams(cur);
        });

        let apply = res.data.list.find(cur => cur.roleId == roleInfo.roleId);
        applyCode = apply.applyCode;
      done();
    });
  });

  it('同意申请', function(done) {
    pinusClientT.request('role.friendHandler.handleApply', { applyCodeList: [applyCode], isPass: true } , (res) => {
        checkSuccessResponse(res);
        expect(res.data.isSuccess).to.be.a('boolean');
        expect(res.data.msg).to.be.a('string');
        expect(res.data.applyCodeList).to.be.a('array');
        res.data.applyCodeList.forEach(cur => {
            expect(cur).to.be.a('string');
        })

        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkFriendListParam(cur);
        });

        expect(res.data.friendCnt).to.be.a('number');
      done();
    });
  });

  it('获取好友列表', function(done) {
    pinusClient.request('role.friendHandler.getFriendList', {} , (res) => {
        checkSuccessResponse(res);

        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkFriendListParam(cur);
        });

        expect(res.data.todayReceiveCnt).to.be.a('number');
        expect(res.data.todaySendCnt).to.be.a('number');
        expect(res.data.friendCnt).to.be.a('number');
        expect(res.data.blockCnt).to.be.a('number');
      done();
    });
  });

  it('拉黑', function(done) {
    pinusClient.request('role.friendHandler.setBlackList', { roleId: roleInfoT.roleId, type: BLOCK_OPEATE.ADD } , (res) => {
        expect(res.data.frdRoleIds).to.be.a('array');
        res.data.frdRoleIds.forEach(cur => {
            expect(cur).to.be.a('string');
        });
        expect(res.data.blackRoleIds).to.be.a('array');
        res.data.blackRoleIds.forEach(cur => {
            expect(cur).to.be.a('string');
        })

        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkBlackListParam(cur);
        });

        expect(res.data.isSuccess).to.be.a('boolean');
        expect(res.data.msg).to.be.a('string');
        expect(res.data.friendCnt).to.be.a('number');
        expect(res.data.blockCnt).to.be.a('number');

      done();
    });
  });

  it('获取黑名单', function(done) {
    pinusClient.request('role.friendHandler.getBlackList', {} , (res) => {

        expect(res.data.list).to.be.a('array');
        res.data.list.forEach(cur => {
            checkBlackListParam(cur);
        });
        expect(res.data.friendCnt).to.be.a('number');
        expect(res.data.blockCnt).to.be.a('number');

      done();
    });
  });

  it('获得玩家详细信息', function(done) {
    pinusClient.request('role.friendHandler.getPlayerDetail', { roleId: roleInfoT.roleId } , (res) => {

        // 从黑名单移除
        pinusClient.request('role.friendHandler.setBlackList', { roleId: roleInfoT.roleId, type: BLOCK_OPEATE.REMOVE_BLACK } , (res) => {
            done();
        });
    });
  });
});

function checkFriendRecommendParams(data) {
    expect(data.roleId).to.be.a('string');
    expect(data.roleName).to.be.a('string');
    expect(data.lv).to.be.a('number');
    expect(data.head).to.be.a('number');
    expect(data.spine).to.be.a('number');
    expect(data.frame).to.be.a('number');
    expect(data.ce).to.be.a('number');
    expect(data.title).to.be.a('number');
    expect(data.recFrdApplyCnt).to.be.a('number');
    expect(data.type).to.be.a('number');
    expect(data.serverId).to.be.a('number');
    expect(data.serverName).to.be.a('string');
}

function checkFriendApplyParams(data) {
    expect(data.applyCode).to.be.a('string');
    expect(data.roleId).to.be.a('string');
    expect(data.roleName).to.be.a('string');
    expect(data.lv).to.be.a('number');
    expect(data.head).to.be.a('number');
    expect(data.spine).to.be.a('number');
    expect(data.frame).to.be.a('number');
    expect(data.ce).to.be.a('number');
    expect(data.title).to.be.a('number');
    expect(data.friendCnt).to.be.a('number');
    expect(data.type).to.be.a('number');
    expect(data.serverId).to.be.a('number');
    expect(data.serverName).to.be.a('string');
}

function checkFriendListParam(data) {
    expect(data.roleId).to.be.a('string');
    expect(data.roleName).to.be.a('string');
    expect(data.lv).to.be.a('number');
    expect(data.head).to.be.a('number');
    expect(data.spine).to.be.a('number');
    expect(data.frame).to.be.a('number');
    expect(data.ce).to.be.a('number');
    expect(data.title).to.be.a('number');
    expect(data.guildName).to.be.a('string');
    expect(data.isOnline).to.be.a('boolean');
    expect(data.quitTime).to.be.a('number');
    expect(data.friendValue).to.be.a('number');
    expect(data.friendLv).to.be.a('number');
    expect(data.canReceive).to.be.a('boolean');
    expect(data.canSend).to.be.a('boolean');
    expect(data.type).to.be.a('number');
    expect(data.serverId).to.be.a('number');
    expect(data.serverName).to.be.a('string');
}

function checkBlackListParam(data) {
    expect(data.roleId).to.be.a('string');
    expect(data.roleName).to.be.a('string');
    expect(data.lv).to.be.a('number');
    expect(data.head).to.be.a('number');
    expect(data.spine).to.be.a('number');
    expect(data.frame).to.be.a('number');
    expect(data.ce).to.be.a('number');
    expect(data.title).to.be.a('number');
    expect(data.guildName).to.be.a('string');
    expect(data.isOnline).to.be.a('boolean');
    expect(data.quitTime).to.be.a('number');
    expect(data.type).to.be.a('number');
    expect(data.serverId).to.be.a('number');
    expect(data.serverName).to.be.a('string');
}