import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_TYPE, SERVER_OPEN_TIME, ITEM_CHANGE_REASON, } from '../../../consts';
import { canBuyVip, getPlayerSignInData } from '../../../services/activity/signInService';
import { handleCost } from '../../../services/role/rewardService';
import { SignInItem } from '../../../domain/activityField/signInField';
import { ActivitySignInModel } from '../../../db/ActivitySignIn';
import moment = require('moment');
import { RoleModel } from '../../../db/Role';
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';


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

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

    /************************签到活动****************************/

    /**
    * @description 获取普通签到活动数据
    * @param {BackendSession} session
    * @param {{ activityId: number}} msg
    * @memberof SignInHandler
    */
    async getSignInActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let curDate = moment(new Date()).valueOf();

        let playerData = await getPlayerSignInData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
        if (curDate < playerData.beginTime || curDate > playerData.endTime) {//未开启
            return resResult(STATUS.ACTIVITY_CLOSED);
        }

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 签到获得奖励
        * @param {{ activityId: number,  dayIndex: number}} msg
        * @param {BackendSession} session
        * @memberof SignInHandler
        */
    async getSignInReward(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 getPlayerSignInData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        if (!playerData.canShow()) {//未开启
            return resResult(STATUS.ACTIVITY_CLOSED);
        }

        if (dayIndex > playerData.todayIndex) {
            return resResult(STATUS.ACTIVITY_DAY_INDEX_OVER);
        }
        let isDelay = false;//补签
        if(dayIndex < playerData.todayIndex) {
            isDelay = true;
        }
        let roundIndex = playerData.roundIndex;

        //高级签到开启条件
        let { lv } = await RoleModel.findByRoleId(roleId);
        if (playerData.type == ACTIVITY_TYPE.SIGN_IN_VIP) {
            if (!canBuyVip(lv))
                return resResult(STATUS.ACTIVITY_SIGNIN_VIP_CONDITION);

            if (!playerData.isVip) {
                return resResult(STATUS.ACTIVITY_SIGNIN_VIP_CONDITION);
            }
        }


        let signinItemData: SignInItem = playerData.findDayItem(dayIndex);
        if (!signinItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (!signinItemData.canReceive()) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }
        if (isDelay) {
            let consume = stringToConsumeParam(playerData.consume)
            let consumeResult = await handleCost(roleId, sid, consume, ITEM_CHANGE_REASON.REPAIRE_SIGNIN);
            if (!consumeResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
        }

        await ActivitySignInModel.addSignInRecord(activityId, roleId, roundIndex, dayIndex);

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

        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, dayIndex },
        }));
    }

}
