import { Application, BackendSession, HandlerService } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { BIND_PHONE_STATUS, ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerDailyChallengesData } from '../../../services/activity/dailyChallengesService';
import { DailyItem } from '../../../domain/activityField/dailyChallengesField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { ActivityDailyChallengesModel } from '../../../db/ActivityDailyChallenges';
import { getBindPhoneData } from '../../../services/activity/bindPhoneService';
import { ActivityBindPhoneRewardModel } from '../../../db/ActivityBindPhoneReward';


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

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

    /************************绑定手机****************************/

    /**
        * @description 绑定手机数据
        * @param {{ activityId: number}} msg
        * @param {BackendSession} session
        * @memberof BindPhoneHandler
        */
     async bind(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        const uid = session.get('userid');
        
        let playerData = await getBindPhoneData(activityId, roleId, serverId, uid);
        if(!playerData) return resResult(STATUS.ACTIVITY_ID_ERROR);
        if(playerData.status != BIND_PHONE_STATUS.WAIT_BIND) return resResult(STATUS.ACTIVITY_HAS_BIND);

        await ActivityBindPhoneRewardModel.addRecord(activityId, uid, BIND_PHONE_STATUS.HAS_BIND, { roleId, roleName, serverId });

        return resResult(STATUS.SUCCESS, {
            activityId,
            status: BIND_PHONE_STATUS.HAS_BIND
        });
    }

    /**
        * @description 领取奖励
        * @param {{ activityId: number}} msg
        * @param {BackendSession} session
        * @memberof BindPhoneHandler
        */
    async receiveReward(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        const uid = session.get('userid');
        
        let playerData = await getBindPhoneData(activityId, roleId, serverId, uid);
        if(!playerData) return resResult(STATUS.ACTIVITY_ID_ERROR);
        if(playerData.status == BIND_PHONE_STATUS.WAIT_BIND) return resResult(STATUS.ACTIVITY_BIND_ERR);
        if(playerData.status == BIND_PHONE_STATUS.RECEIVED) return resResult(STATUS.ACTIVITY_BIND_RECEIVED);

        await ActivityBindPhoneRewardModel.addRecord(activityId, uid, BIND_PHONE_STATUS.RECEIVED, { roleId, roleName, serverId });
        let rewardArray = stringToRewardParam(playerData.rewards)
        let { goods, addHeros } = await addReward(roleId, roleName, sid, serverId, rewardArray, ITEM_CHANGE_REASON.ACT_BIND_PHONE);

        return resResult(STATUS.SUCCESS, {
            activityId,
            status: BIND_PHONE_STATUS.RECEIVED,
            goods, addHeros
        });
    }

}
