import { BackendSession, FrontendSession, pinus } from "pinus";
import { RoleModel } from "../db/Role";
import { resResult } from "../pubUtils/util";
import { STATUS, FUNC_OPT_TYPE } from "../consts";
import { eventOnPlayerLvUp } from '../services/playerEventService';
import { checkResetTrain } from '../services/guildTrainService';
import { getUserGuildWithRefActive } from '../services/guildService';
// 开启功能
export async function switchOnFunc(roleId: string, type: number, param: number, session: (BackendSession|FrontendSession)) {

    const dataFuncs = session.get('funcs');
    const sid = session.get('sid');
     
    let addFuncs = new Array<number>();
    switch (type) {
        case FUNC_OPT_TYPE.LEVEL_UP://等级提升，触发事件
            await eventOnPlayerLvUp(roleId, param, addFuncs, dataFuncs);
            break;
        case FUNC_OPT_TYPE.BATTLE_END://战斗结束，触发事件
            // 暂无
            break;
    }
    if (addFuncs.length) {
        pinus.app.get('channelService').pushMessageByUids('onFuncSwitchOn', resResult(STATUS.SUCCESS, {addFuncs}), [{
            uid: roleId,
            sid: sid
        }]);
        const recs = await RoleModel.pushFuncs(roleId, addFuncs);
        session.set('funcs', recs.funcs||[]);
        session.push('funcs', () => {});
        return recs;
    }
}
/**
 * 登录检查刷新
 * @param role 
 * @param session 
 */
export async function chackFunOpenWhenLogin(role: any, session: BackendSession|FrontendSession) {
    await switchOnFunc(role.roleId, FUNC_OPT_TYPE.LEVEL_UP, role.lv, session);
    
    await checkResetTrain(role.roleId, role.serverId);
    await getUserGuildWithRefActive(role.roleId, '', true)
}