import 'mocha';
import { PinusWSClient } from 'pinus-robot-plugin';
import { expect } from 'chai';
import { Client } from './Client';
import { checkSuccessResponse } from './CheckPatten';
import { ACTIVITY_TYPE, CURRENCY_BY_TYPE, CURRENCY_TYPE, DEBUG_MAGIC_WORD, STATUS } from '../app/consts';
describe('活动测试', function () {
  let pinusClient: PinusWSClient;
  let playerGroupArray: [] = [];
  let activityId: number = 0;

  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('activity.activityHandler.getAllOpenActivity', {}, (res) => {
      checkSuccessResponse(res);
      playerGroupArray = res.data.playerGroupArray;
      console.log(`${playerGroupArray.length}个合集数据`)
      done();
    });
  });

  it('签到活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.signInHandler.getSignInActivity', ACTIVITY_TYPE.SIGN_IN, done);
  });

  it('签到活动(高级)-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.signInHandler.getSignInActivity', ACTIVITY_TYPE.SIGN_IN_VIP, done);
  });

  it('主线成长基金-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_MAIN, done);
  });

  it('主线成长基金高阶-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP, done);
  });

  it('镇念塔成长基金-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_TOWER, done);
  });

  it('镇念塔成长基金高阶-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP, done);
  });

  it('精英成长基金-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE, done);
  });

  it('精英成长基金高阶-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.growthFundHandler.getGrowthFundActivity', ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP, done);
  });

  it('30日目标活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.thirtyDaysHandler.getThirtyDaysActivity', ACTIVITY_TYPE.THIRTY_DAYS, done);
  });

  it.skip('自选商店-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.selfServiceShopHandler.getSelfServiceShopActivity', ACTIVITY_TYPE.SELF_SERVICE_SHOP, done);
  });

  it('首充礼包-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.firstGiftHandler.getFirstGiftActivity', ACTIVITY_TYPE.FIRST_GIFT, done);
  });

  it.skip('新手限定RMB购买礼包-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.limitPackageHandler.getNewPlayerLimitPackageActivity', ACTIVITY_TYPE.NEW_PLAYER_LIMIT_PACKAGE, done);
  });

  it('每日限购礼包-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.limitPackageHandler.getLimitPackageActivity', ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_DAILY, done);
  });

  it('每周限购礼包-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.limitPackageHandler.getLimitPackageActivity', ACTIVITY_TYPE.LIMIT_PACKAGE_SHOP_WEEKLY, done);
  });

  it('元宝充值商店-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.yuanbaoShopHandler.getShopActivity', ACTIVITY_TYPE.YUAN_BAO_SHOP, done);
  });

  it('月卡1（普通）-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.monthlyTicketHandler.getMonthlyTicketActivity', ACTIVITY_TYPE.MONTHLY_TICKET_1, done);
  });

  it('月卡2（至尊）-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.monthlyTicketHandler.getMonthlyTicketActivity', ACTIVITY_TYPE.MONTHLY_TICKET_2, done);
  });

  it('累计充值RMB活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.rechargeMoneyHandler.getRechargeMoneyActivity', ACTIVITY_TYPE.RECHARGE_MONEY, done);
  });

  it('每日特惠RMB购买-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.dailyRMBGiftsHandler.getDailyRMBGiftsActivity', ACTIVITY_TYPE.DAILY_RMB_GIFTS, done);
  });

  it.skip('寻宝骑兵活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.treasureHuntHandler.getTreasureHuntActivity', ACTIVITY_TYPE.TREASURE_HUNT, done);
  });

  it('弹出商店-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.popUpShopHandler.getPopUpShopActivity', ACTIVITY_TYPE.POP_UP_SHOP, done);
  });

  it('新手签到活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.signInHandler.getSignInActivity', ACTIVITY_TYPE.NEW_PLAYER_SIGN_IN, done);
  });

  it('vip累计充值RMB活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.vipRechargeMoneyHandler.getVipRechargeMoneyActivity', ACTIVITY_TYPE.VIP_RECHARGE_MONEY, done);
  });

  it('通用七天乐活动-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.sevenDaysHandler.getSevenDaysActivity', ACTIVITY_TYPE.COMMON_SEVEN_DAY, done);
  });

  it('每日领取免费午饭、晚饭-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.dailyMealHandler.getDailyMealActivity', ACTIVITY_TYPE.DAILY_MEAL, done);
  });

  it('每日兑换铜钱-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.dailyCoinHandler.getDailyCoinActivity', ACTIVITY_TYPE.DAILY_COIN, done);
  });

  it('每日关卡-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.dailyGKHandler.getDailyGKActivity', ACTIVITY_TYPE.DAILY_GK, done);
  });

  it('通用的刷新商店-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.refreshShopHandler.getRefreshShopActivity', ACTIVITY_TYPE.REFRESH_SHOP, done);
  });

  it('通用的刷新任务-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.refreshTaskHandler.getRefreshTaskActivity', ACTIVITY_TYPE.REFRESH_TASK, done);
  });

  it('大富翁-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.activityMonopolyHandler.getMonopolyActivity', ACTIVITY_TYPE.MONOPOLY, done);
  });

  it('大富翁-移动', function (done) {
    if (activityId) {
      requestActivity(pinusClient, 'role.heroHandler.addItem', { id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.NORMAL_DICE), count: 1 })
        .then((data: any) => {
          checkSuccessResponse(data);
          return requestActivity(pinusClient, 'activity.activityMonopolyHandler.move', { activityId: activityId, step: 0 })
        })
        .then((data: any) => {
          checkSuccessResponse(data);
          done()
        })
    } else {
      noActivityData(done);
    }
  });

  it('通用签到-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.signInHandler.getSignInActivity', ACTIVITY_TYPE.COMMON_SIGN_IN, done);
  });

  it('新将好礼-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.newHeroGiftsHandler.getNewHeroGiftsActivity', ACTIVITY_TYPE.NEW_HERO_GIFTS, done);
  });

  it('新将演绎-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.newHeroGKHandler.getNewHeroGKActivity', ACTIVITY_TYPE.NEW_HERO_GK, done);
  });

  // 未找到该接口，暂时skip
  it.skip('新将擢迁（新武将抽卡）-活动数据', function (done) {
    checkActivityData(pinusClient, 'activity.newHeroGachaHandler.getNewHeroGachaActivity', ACTIVITY_TYPE.NEW_HERO_GACHA, done);
  });

  function checkActivityData(pinusClient, url, type, done) {
    try {
      requestActivity(pinusClient, 'activity.activityHandler.debugActivityMemory', { magicWord: DEBUG_MAGIC_WORD })
        .then((data: any) => {
          if (data.data) {
            checkSuccessResponse(data);
            expect(data.data.activities).to.be.an('array');
            let activitiesByType = data.data.activities.filter(activity => {
              return activity && activity[1] && activity[1].type == type;
            });
            return activitiesByType
          } else {
            return null;
          }
        })
        .then((activityData: any) => {
          expect(activityData).to.be.an('array');
          if (activityData.length > 0) {
            return requestActivity(pinusClient, url, { activityId: activityData[0][1].activityId });
          } else {
            noActivityData(done);
          }
        })
        .then((data: any) => {
          if (data.code == STATUS.ACTIVITY_CLOSED.code || data.code == STATUS.ACTIVITY_NEW_PLAYER_GIFT_END.code) {//活动正常关闭
            done()
          } else {
            checkSuccessResponse(data);
            done()
          }
        })
    } catch (error) {
      console.log('error', error)
    }
  }


  function noActivityData(done) {
    console.warn('没有此活动数据');
    activityId = 0;
    done()
  }

  function requestActivity(pinusClient, route: string, msg: any) {
    return new Promise(resolve => {
      pinusClient.request(route, msg, (res) => {
        resolve(res);
      });
    })
  }

});
