import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { Client } from './Client';
import { checkDisplayItems, checkSuccessResponse, checkTimeStamp } from './CheckPatten';
import { getRandSingleEelm } from './pureUtil';
import { DEBUG_MAGIC_WORD } from '../app/consts';
describe('军团测试', function() {
  let pinusClient: PinusWSClient;
  let guildList;

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

  after(function(done) {
    pinusClient.disconnect();
    done();
  });

  it('获取军团列表', function(done) {
    pinusClient.request('guild.guildHandler.getGuildList', {page:1, showPeopleMax: true, name: ""} , (res) => {
      // console.log(JSON.stringify(res))
      // 消息回调
      checkSuccessResponse(res);
      expect(res.data).to.have.deep.property('list').that.is.an('array');
      expect(res.data).to.have.deep.property('quitTime').that.is.an('number');
      guildList = res.data.list;
      res.data.list.forEach(list => {
        expect(list).to.have.deep.property('code').that.is.a('string');
        expect(list).to.have.deep.property('name').that.is.a('string');
        expect(list).to.have.deep.property('icon').that.is.a('number');
        expect(list).to.have.deep.property('lv').that.is.a('number');
        expect(list).to.have.deep.property('memberCnt').that.is.a('number');
        expect(list).to.have.deep.property('leader').that.is.a('string');
        expect(list).to.have.deep.property('ceLimit').that.is.a('number');
        expect(list).to.have.deep.property('hasApply').that.is.a('boolean');
      });
      done();
    });
  });

  let guildCode;

  it('获取个人军团', function (done) {
    pinusClient.request('guild.guildHandler.getMyGuildInfo', { }, (res) => {
      // 消息回调
      checkSuccessResponse(res);
      expect(res.data).to.have.deep.property('hasGuild').that.is.an('boolean');
      if(res.data.hasGuild) {
        expect(res.data).to.have.deep.property('code').that.is.an('string');
        guildCode = res.data.code;
        expect(res.data).to.have.deep.property('name').that.is.an('string');
        expect(res.data).to.have.deep.property('icon').that.is.an('number');
        expect(res.data).to.have.deep.property('notice').that.is.an('string');
        expect(res.data).to.have.deep.property('introduce').that.is.an('string');
        expect(res.data).to.have.deep.property('rank').that.is.an('number');
        expect(res.data).to.have.deep.property('lv').that.is.an('number');
        expect(res.data).to.have.deep.property('fund').that.is.an('number');
        expect(res.data).to.have.deep.property('memberCnt').that.is.an('number');
        expect(res.data).to.have.deep.property('managerCnt').that.is.an('number');
        expect(res.data).to.have.deep.property('activeDaily').that.is.an('number');
        expect(res.data).to.have.deep.property('activeWeekly').that.is.an('number');
        expect(res.data).to.have.deep.property('ceLimit').that.is.an('number');
        expect(res.data).to.have.deep.property('isAuto').that.is.an('boolean');
        
        expect(res.data).to.have.deep.property('leader').that.is.an('object');
        expect(res.data.leader).to.have.deep.property('roleName').that.is.an('string');
        expect(res.data.leader).to.have.deep.property('frame').that.is.an('number');
        expect(res.data.leader).to.have.deep.property('spine').that.is.an('number');
        expect(res.data.leader).to.have.deep.property('head').that.is.an('number');
        expect(res.data.leader).to.have.deep.property('lv').that.is.an('number');
        expect(res.data.leader).to.have.deep.property('ce').that.is.an('number');
        // expect(res.data.leader).to.have.deep.property('loginTime').that.is.an('number');
        
        expect(res.data).to.have.deep.property('myInfo').that.is.an('object');
        // expect(res.data.myInfo).to.have.deep.property('honourWeekly').that.is.an('number');
        expect(res.data.myInfo).to.have.deep.property('job').that.is.an('number');
        expect(res.data.myInfo).to.have.deep.property('auth').that.is.an('number');
        
        expect(res.data).to.have.deep.property('structure').that.is.an('array');
        res.data.structure.forEach(list => {
          expect(list).to.have.deep.property('id').that.is.a('number');
          expect(list).to.have.deep.property('lv').that.is.a('number');
        });
      }
      done();
    });
  });

  it('获取申请列表', function (done) {
    pinusClient.request('guild.guildHandler.getApplyList', { code: guildCode }, (res) => {
      // 消息回调
      checkSuccessResponse(res);
      expect(res.data).to.have.deep.property('list').that.is.an('array');
      res.data.list.forEach(list => {
        expect(list).to.have.deep.property('applyCode').that.is.a('string');
        expect(list).to.have.deep.property('roleId').that.is.a('string');
        expect(list).to.have.deep.property('roleName').that.is.a('string');
        expect(list).to.have.deep.property('ce').that.is.a('number');
        expect(list).to.have.deep.property('head').that.is.a('number');
        expect(list).to.have.deep.property('spine').that.is.a('number');
        expect(list).to.have.deep.property('frame').that.is.a('number');
        expect(list).to.have.deep.property('lv').that.is.a('number');
        expect(list).to.have.deep.property('title').that.is.a('number');
        expect(list).to.have.deep.property('loginTime').that.is.a('number');
      });
      done();
    });
  });

  it('团长查看可邀请列表', function (done) {
    pinusClient.request('guild.guildHandler.getInviteMemberList', { }, (res) => {
      // 消息回调
      checkSuccessResponse(res);
      expect(res.data).to.have.deep.property('list').that.is.an('array');
      res.data.list.forEach(list => {
        expect(list).to.have.deep.property('roleId').that.is.a('string');
        expect(list).to.have.deep.property('roleName').that.is.a('string');
        expect(list).to.have.deep.property('ce').that.is.a('number');
        expect(list).to.have.deep.property('head').that.is.a('number');
        expect(list).to.have.deep.property('spine').that.is.a('number');
        expect(list).to.have.deep.property('frame').that.is.a('number');
        expect(list).to.have.deep.property('lv').that.is.a('number');
        expect(list).to.have.deep.property('title').that.is.a('number');
        // expect(list).to.have.deep.property('loginTime').that.is.a('number');
      });
      done();
    });
  });
  
  it('查看军团成员列表', function (done) {
    let index = Math.floor(Math.random() * guildList.length);
    if(guildList[index]) {
      let code = guildList[index].code;

      pinusClient.request('guild.guildHandler.getGuildMember', { code }, (res) => {
        // 消息回调
        checkSuccessResponse(res);
        expect(res.data).to.have.deep.property('list').that.is.an('array');
        res.data.list.forEach(list => {
          expect(list).to.have.deep.property('roleId').that.is.a('string');
          expect(list).to.have.deep.property('roleName').that.is.a('string');
          expect(list).to.have.deep.property('ce').that.is.a('number');
          expect(list).to.have.deep.property('head').that.is.a('number');
          expect(list).to.have.deep.property('spine').that.is.a('number');
          expect(list).to.have.deep.property('frame').that.is.a('number');
          expect(list).to.have.deep.property('lv').that.is.a('number');
          expect(list).to.have.deep.property('title').that.is.a('number');
          // expect(list).to.have.deep.property('loginTime').that.is.a('number');
          expect(list).to.have.deep.property('job').that.is.a('number');
          // expect(list).to.have.deep.property('auth').that.is.a('number');
          // expect(list).to.have.deep.property('honourWeekly').that.is.a('number');
        });
        done();
      });
    }
  });

  it('获得演武台', function (done) {
    pinusClient.request('guild.guildBossHandler.getBossInstance', { }, (res) => {
      checkSuccessResponse(res);
      expect(res.data.status).to.be.a('number');
      if(res.data.bossInfo) checkBossInfo(res.data.bossInfo);
      checkBossCnt(res.data);
      done();
    });
  });

  it('开启演武场', function (done) {
    pinusClient.request('guild.guildHandler.debugAdddFund', { magicWord: 'zyz666server518', code: guildCode, count: 100000  }, res => {
      checkSuccessResponse(res, false);
      pinusClient.request('guild.guildBossHandler.debugAddOpenTimes', { magicWord: 'zyz666server518' }, res => {
        checkSuccessResponse(res, false);
        pinusClient.request('guild.guildBossHandler.openBossInstance', { }, (res) => {
          checkSuccessResponse(res);
          expect(res.data.status).to.equal(1);
          checkBossInfo(res.data.bossInfo);
          checkBossCnt(res.data);
          done();
        });
      });
    })
  });

  let battleCode;
  let bossCode;
  let bossHp;

  it('挑战boss', function (done) {
    pinusClient.request('guild.guildBossHandler.debugAddBattleTimes', { magicWord: 'zyz666server518' }, res => {
      checkSuccessResponse(res, false);
      pinusClient.request('guild.guildBossHandler.battleBoss', { }, (res) => {
        checkSuccessResponse(res);
        expect(res.data.battleCode).to.be.a('string');
        expect(res.data.bossCode).to.be.a('string');
        expect(res.data.myChallengeCnt).to.be.a('number');
        battleCode = res.data.battleCode;
        bossCode = res.data.bossCode;
        done();
      });
    });
  });
  
  it('同步血条', function (done) {
    pinusClient.request('guild.guildBossHandler.action', { damage: bossHp, battleCode, bossCode }, (res) => {
      checkSuccessResponse(res);
      expect(res.data.bossHp).to.equal(0);
      done();
    });
  });
  
  it('战斗结算', function (done) {
    pinusClient.request('guild.guildBossHandler.battleBossEnd', { battleCode, bossCode }, (res) => {
      checkSuccessResponse(res);
      expect(res.data.status).to.equal(0);
      checkBossInfo(res.data.bossInfo);
      checkBossCnt(res.data);
      checkDisplayItems(res.data.battleGoods);
      done();
    });
  });

  function checkBossInfo(bossInfo) {
    expect(bossInfo).to.be.an('object');
    expect(bossInfo.warId).to.be.a('number');
    expect(bossInfo.encourageCnt).to.be.a('number');
    expect(bossInfo.encourageMax).to.be.a('number');
    checkBossRanks(bossInfo.ranks);
    checkBossRank(bossInfo.myRank);
    expect(bossInfo.bossHp).to.be.a('number');
    bossHp = bossInfo.bossHp;
    expect(bossInfo.bossLv).to.be.a('number');
  }

  function checkBossRanks(ranks) {
    expect(ranks).to.be.an('array');
    ranks.forEach(rank => {
      checkBossRank(rank);
    })
  }

  function checkBossRank(rank) {
    if(JSON.stringify(rank) == '{}') return false
    expect(rank.roleId).to.be.a('string');
    expect(rank.score).to.be.a('number');
    expect(rank.rankLv).to.be.a('number');
  }

  function checkBossCnt(data) {
    expect(data.leaderOpenCnt).to.be.a('number');
    expect(data.myChallengeCnt).to.be.a('number');
    expect(data.myEncourageCnt).to.be.a('number');
  }

  it('获得捐献所数据', function (done) {
    pinusClient.request('guild.donateHandler.getDonation', {}, (res) => {
      checkSuccessResponse(res);
      expect(res.data.receiveBoxs).to.be.an('array');
      res.data.receiveBoxs.forEach(box => {
        expect(box).to.be.a('number');
      });
      expect(res.data.donateFund).to.be.a('number');
      expect(res.data.reports).to.be.an('array');
      res.data.reports.forEach(report => {
        expect(report.id).to.be.a('number');
        expect(report.roleName).to.be.a('string');
        checkTimeStamp(report.time, 10, true);
      });
      expect(res.data.donateCnt).to.be.a('number');
      expect(res.data.donationLv).to.be.a('number');
    done();
    });
  });

  it.skip('捐献', function (done) {
    done();
  });

  let trainInstances;
  let trainId;
  it('获取练兵场信息', function (done) {
    pinusClient.request('guild.guildTrainHandler.getTrainInstance', {}, (res) => {
      checkSuccessResponse(res);
      checkGuildTrain(res.data.guildTrain, 'main');
      checkTrainCount(res.data)
      expect(res.data.buyTrainCount).to.be.a('number');
      expect(res.data.trainLv).to.be.a('number');
      done();
    });
  });

  function checkTrainCount(data) {
    expect(data.trainCount).to.be.a('number');
    expect(data.trainRewards).to.be.an('array');
    data.trainRewards.forEach(reward => {
      expect(reward).to.be.a('number');
    })
  }
  function checkGuildTrain(guildTrain, from) {
    expect(guildTrain.trainId).to.be.a('number');
    trainId = guildTrain.trainId;
    expect(guildTrain.isComplete).to.be.a('boolean');
    checkTrainInstances(guildTrain.trainInstances);
    if(guildTrain.reports) checkTrainReports(guildTrain.reports, from)
    if(guildTrain.trainBoxs) checkTrainBoxs(guildTrain.trainBoxs);
  }

  function checkTrainInstances(data) {
    expect(data).to.be.an('array');
    data.forEach(trainInstance => {
      expect(trainInstance.hid).to.be.a('number');
      expect(trainInstance.progress).to.be.a('number');
      expect(trainInstance.endTime).to.be.a('number');
      expect(trainInstance.isComplete).to.be.a('boolean');
    });
    trainInstances = data;
  }

  function checkTrainReports(reports, from) {
    expect(reports).to.be.an('array');
    reports.forEach(report => {
      expect(report.roleName).to.be.a('string');
      expect(report.trainId).to.be.a('number');
      expect(report.hid).to.be.a('number');
      expect(report.score).to.be.a('number');
      expect(report.type).to.be.a('number');
      expect(report.time).to.be.a('number');
      if(from == 'battleEnd') {
        expect(report.isSuccessed).to.be.a('boolean');
      } else if (from == 'getreport') {
        expect(report.difficulty).to.be.a('number');
      }
    });
  }

  function checkTrainBoxs(boxs) {
    expect(boxs).to.be.an('array');
    boxs.forEach(box => {
      expect(box.hid).to.be.a('number');
      expect(box.recordBoxs).to.be.an('array');
      box.recordBoxs.forEach(obj => {
        expect(obj.roleId).to.be.a('string');
        expect(obj.good).to.be.an('object');
        expect(obj.good.id).to.be.a('number');
        expect(obj.good.count).to.be.a('number');
        expect(obj.good.index).to.be.a('number');
      })

      expect(box.endTime).to.be.a('number');
      expect(box.trainId).to.be.a('number');
      expect(box.isComplete).to.be.a('boolean');
    });
  }
  it('进入练兵战场', function (done) {
    let trainInstance: any = getRandSingleEelm(trainInstances.filter(cur => !cur.isComplete));
    if(!trainInstances) {
      pinusClient.request('guild.guildTrainHandler.debugResetTrainInstance', { magicWord: DEBUG_MAGIC_WORD }, (res) => {
        checkSuccessResponse(res, false);
        trainBattleStart()
      });
    } else {
      trainBattleStart()
    }
    function trainBattleStart() {
      pinusClient.request('guild.guildTrainHandler.debugAddTrainCount', { magicWord: DEBUG_MAGIC_WORD }, (res) => {
        checkSuccessResponse(res, false);
        pinusClient.request('guild.guildTrainHandler.trainBattleStart', { hid: trainInstance.hid, difficulty: 1, trainId, battleId: 8501 }, (res) => {
          checkSuccessResponse(res);
          expect(res.data.battleCode).to.be.a('string');
          battleCode = res.data.battleCode;
          done();
        });
      });
    }
  });

  it('战斗结算', function (done) {
    pinusClient.request('guild.guildTrainHandler.trainBattleEnd', { battleCode, isSuccess: true }, (res) => {
      checkSuccessResponse(res);
      checkDisplayItems(res.data.battleGoods);
      checkGuildTrain(res.data.guildTrain, 'battleEnd');
      done();
    });
  });

  it('获得练兵场战报', function (done) {
    pinusClient.request('guild.guildTrainHandler.getTrainReports', {}, (res) => {
      checkSuccessResponse(res);
      checkTrainReports(res.data.reports, 'getreport');
      done();
    });
  });

  it('获得炼器堂', function (done) {
    pinusClient.request('guild.guildRefineHandler.getRefine', {}, (res) => {
      checkSuccessResponse(res);
      checkScienceTrees(res.data.scienceTrees);
      done();
    });
  });

  function checkScienceTrees(scienceTrees) {
    expect(scienceTrees).to.be.an('array');
    scienceTrees.forEach(scienceTree => {
      expect(scienceTree.assistRoleIds).to.be.an('array');
      scienceTree.assistRoleIds.forEach(roleId => {
        expect(roleId).to.be.a('string');
      })
      expect(scienceTree.id).to.be.a('number');
      checkTimeStamp(scienceTree.endTime, 10, true);
    })
  }

  it.skip('点亮研发', function (done) {
    done();
  });

  it.skip('炼器', function (done) {
    done();
  });

  it('获得许愿池数据', function (done) {
    pinusClient.request('guild.wishPoolHandler.getWishPool', {}, (res) => {
      checkSuccessResponse(res);
      expect(res.data.list).to.be.an('array');
      res.data.list.forEach(pool => {
        expect(pool.type).to.be.a('number');
        expect(pool.goodId).to.be.a('number');
        expect(pool.count).to.be.a('number');
        expect(pool.receiveCnt).to.be.a('number');
        expect(pool.drawCnt).to.be.a('number');
        expect(pool.id).to.be.a('string');
        expect(pool.roleId).to.be.a('number');
      });
      expect(res.data.wishDntCnt).to.be.a('number');
      checkWishGoods(res.data.wishGoods);
      done();
    });
  });

  function checkWishGoods(wishGoods) {
    expect(wishGoods).to.be.an('array');
    wishGoods.forEach(good => {
      expect(good.type).to.be.a('number');
      expect(good.goodId).to.be.a('number');
      expect(good.count).to.be.a('number');
      expect(good.receiveCnt).to.be.a('number');
      expect(good.drawCnt).to.be.a('number');
      expect(good.id).to.be.a('string');
    })
  }

  it.skip('许愿', function (done) {
    done();
  });
});
