/**
 * 体力系统
 */

import { pinus } from 'pinus';
import { STATUS } from '../consts/statusCode';

import { resResult, reduceCe } from '../pubUtils/util';
import { calPlayerCeAndSave as pubCalPlayerCeAndSave,  reCalAllHeroCe } from '../pubUtils/playerCe';
import { HeroType, HeroUpdate } from '../db/Hero';

import { RoleUpdate, RoleType } from '../db/Role';
import { Rank } from './rankService';
import { REDIS_KEY } from '../consts';
import { updateUserInfo } from './redisService';
import { GuildType } from '../db/Guild';

interface calPlayerReturn {
    serverId: number;
    role: RoleType;
    guild?: GuildType;
    hero?: HeroType;
    pushHeros: {hid: number, ce: number, incHeroCe: number}[];
    topLineupCe: number;
    heros?: HeroType[]
}

//修改并下发战力
export async function calPlayerCeAndSave(type: number, sid: string, roleId: string, originHero: HeroType, update: HeroUpdate, args?: Array<number>) {
    let result = await pubCalPlayerCeAndSave(type, roleId, originHero, update, args);
    return await pushCalPlayerCe(roleId, sid, result);
}

// 修改后战力的推送
export async function pushCalPlayerCe(roleId: string, sid: string, calResult: calPlayerReturn) {
    let {role, pushHeros, topLineupCe, hero, guild, serverId} = calResult;
    // console.log(JSON.stringify(pushHeros))
    //下发战力
    let uids = [{ uid: roleId, sid }];
    pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: reduceCe(role.ce) , heros: pushHeros, topLineupCe:  reduceCe(topLineupCe) }), uids);
    if(guild) {
        await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
    }
    updateRank(roleId, serverId, topLineupCe, role, pushHeros);

    return hero;
}

// 修改全局战力并下发
export async function calAllHeroCe(type:number, sid: string, roleId: string, update: RoleUpdate, args?:Array<number>) {
    let result = await reCalAllHeroCe(type, roleId, update, args);
    if(result.pushHeros.length > 0) {
        return await pushCalAllHeroCe(roleId, sid, result);
    } else {
        return result;
    }
}

// 修改全局战力的推送
export async function pushCalAllHeroCe(roleId: string, sid: string, calResult: calPlayerReturn) {
    let {role, pushHeros, topLineupCe, guild, serverId }  = calResult;
    let uids = [{ uid: roleId, sid }];
    pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: reduceCe(role.ce), heros: pushHeros, topLineupCe: reduceCe(topLineupCe) }), uids);    

    if(guild) {
        await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
    }
    updateRank(roleId, serverId, topLineupCe, role, pushHeros);

    return calResult;
}


// 更新排行榜数据
async function updateRank(roleId: string, serverId: number, topLineupCe: number, role: RoleType, pushHeros: {hid: number, ce: number}[]) {
    
    // 最强阵容
    let r = new Rank(REDIS_KEY.TOP_LINEUP_RANK, { serverId });
    await r.setRankWithRoleInfo(roleId, reduceCe(topLineupCe), 0, role);

    // 最强武将
    for(let { hid, ce } of pushHeros) {
        let r2 = new Rank(REDIS_KEY.TOP_HERO_RANK, { serverId });
        await r2.setRankWithHeroInfo(roleId, hid, ce, 0);

        let r4 = new Rank(REDIS_KEY.HERO_RANK, { serverId, hid });
        await r4.setRankWithHeroInfo(roleId, hid, ce, 0);
    }

    // 总战力
    let r3 = new Rank(REDIS_KEY.SUM_CE_RANK, { serverId });
    await r3.setRankWithRoleInfo(roleId, reduceCe(role.ce), 0, role);

    // 更新最强五人阵容信息
    let r5 = new Rank(REDIS_KEY.TOP_LINEUP_INFO, { serverId });
    await r5.generParamAndSet(REDIS_KEY.TOP_LINEUP_INFO, { roleId }, { role });
}