import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts';
import { getPlayerDailyGKData } from '../../../services/activity/dailyGKService';
import { DailyGKItem } from '../../../domain/activityField/dailyGKField';


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

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

    /************************每日关卡；按日开启，每日开启一关****************************/

    /**
    * @description 获取每日关卡活动数据
    * @param {{ activityId: number}} msg
    * @param {BackendSession} session
    * @memberof DailyGKHandler
    */
    async getDailyGKActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let playerData = await getPlayerDailyGKData(activityId, serverId, roleId)

        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 获取今日挑战的单个奖励
        * @param {{ activityId: number, dayIndex: number}} msg
        * @param {BackendSession} session
        * @memberof DailyGKHandler
        */
    async getGK(msg: { activityId: number, dayIndex: number }, session: BackendSession) {
        const { activityId, dayIndex } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

        let playerData = await getPlayerDailyGKData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        if (dayIndex > playerData.todayIndex) {
            return resResult(STATUS.ACTIVITY_DAY_INDEX_OVER);
        }

        let dailyItemData: DailyGKItem = playerData.findDailyGKItem(dayIndex);
        if (!dailyItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        // if (dailyItemData.isSuccess) {//已经达成
        //     return resResult(STATUS.ACTIVITY_REWARDED);
        // }

        // await ActivityDailyGKModel.addRecord(serverId, activityId, roleId, dayIndex);

        // let rewardParamArr: Array<RewardParam> = stringToRewardParam(dailyItemData.reward);
        // let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr)

        // dailyItemData.isSuccess = true;
        return resResult(STATUS.SUCCESS, {
            gkId: dailyItemData.gk
        });
    }

}
