import { Application, BackendSession, HandlerService, } from 'pinus';
import { aesEncrypt, aesEncryptcfb, resResult } from '../../../pubUtils/util';
import { ENCRYPT_IV, ENCRYPT_KEY, STATUS, TASK_TYPE } from '../../../consts';
import { checkActivityTask } from '../../../services/taskService';
import { ActivityGroupModel } from '../../../db/ActivityGroup';
import { ServerlistModel } from '../../../db/Serverlist';
import { getActivity, getActivityById } from '../../../services/activity/activityService';
import { ActivityModel } from '../../../db/Activity';

export default function (app: Application) {
    new HandlerService(app, {});
    return new ActivityHandler(app);
}

export class ActivityHandler {
    constructor(private app: Application) {
    }


    /**
    * @description 活动是否开启总接口
    * @param {{ }} msg
    * @param {BackendSession} session
    * @memberof ActivityHandler
    */
    async getAllOpenActivity(msg: {}, session: BackendSession) {

        const { } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid: string = session.get('sid');
        

        let playerActivityArray = [];

        let { activityGroupId } = await ServerlistModel.findByServerId(serverId);
        let groups = await ActivityGroupModel.findGroupsData(activityGroupId);
        let playerGroupArray = [];
        for (let groupData of groups) {
            let playerGroupActivityArray = [];
            for (let activityId of groupData.activities) {
                let activityData = await getActivityById(activityId);
                if (activityData) {
                    let playerActivityData = await getActivity(serverId, roleId, activityId, activityData.type)
                    if (playerActivityData) playerGroupActivityArray.push(playerActivityData);
                }
            }
            playerGroupArray.push({
                type: groupData.type,
                activities: playerGroupActivityArray,
            });
            // console.log(`${roleId} 活动合集  groupId:${groupData.groupId}, 合集类型${groupData.type}, 一共${playerGroupActivityArray.length}个`)
        }

        return resResult(STATUS.SUCCESS, { playerActivityArray, playerGroupArray });
    }

    //测试活动任务数据
    async testActivityTask(msg: { hid: number, lv: number }, session: BackendSession) {
        const { lv } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid: string = session.get('sid');
        

        let heroNum = 1;
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.HERO_QUALITY, heroNum, { heroes: [{ quality: 3 }] })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.HERO_QUALITY_TO_QUALITY_COUNT, 1, { oldQuality: 1, quality: 2 })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.HERO_WAKE_UP_STAR_UP_COUNT, 1, { quality: 1, colorStar: 1 })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_MAIN_ELITE, 1, { mainEliteWarId: 9001 })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_DUNGEON_WAR, 1, { warId: 5001 })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_EXPEDITION_BOX, 1)
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.ROLE_LV, 100)
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.PVP, 1)

        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.BATTLE_TOWER_LV, 1, { towerLv: lv })
        // await checkActivityTask(serverId, sid, roleId, TASK_TYPE.GASHA, 1)

        // if (lv == 2) {
        //     await GTCreateListMessage('批量推送测试11', 259200000, '哈哈哈哈哈11', '噢噢噢噢噢噢噢噢11')
        // }
        // if (lv == 3) {
        //     await GTPushListCidMessage('RASL_0630_da707a0c484d4ee39f2d462d5f52984', ['ba64ee9a9d516bbd341267d685baceb4'], true);
        // }
        // if (lv == 1) {
        //     await GTPushSingleCidMessage('ba64ee9a9d516bbd341267d685baceb4', 259200000, '哈哈哈哈哈', '噢噢噢噢噢噢噢噢');
        // }
        // let beginTime = 1624050000000;
        // let interval = 86400;
        // console.log('ddddddbbbbbbbbbbbbbb', moment(new Date).valueOf(), (moment(new Date).valueOf() - beginTime), ((moment(new Date).valueOf() - beginTime) % (interval * 1000)), 24 * 60 * 60 * 1000, ((moment(new Date).valueOf() - beginTime) % (interval * 1000)) / (24 * 60 * 60 * 1000))
        // let aaa = Math.ceil(((moment(new Date).valueOf() - beginTime) / (24 * 60 * 60 * 1000)));
        // console.log('xxxxxxxxxxxxxxxxxxx', aaa);
        //aesEncryptcfb
        console.log('13121622738', await aesEncryptcfb("13121622738", ENCRYPT_KEY, ENCRYPT_IV))
        //18612532385:cc80b189dc03cff31fe75d
        //13636354764:cc8bb18bd805c9f51be95c,Z6ArLdom2c
        //13121622738:cc8bb68adf00cef31bec50
        return resResult(STATUS.SUCCESS,);

    }

    async debugActivityByType(msg: { type: number }, session: BackendSession) {
        const { type } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid: string = session.get('sid');
        
        let activityData = await ActivityModel.debugFindActivityByType(type);
        return resResult(STATUS.SUCCESS, activityData);
    }
}
