import { pinus } from 'pinus';
import { isEqual } from 'underscore';
import { ITEM_CHANGE_REASON, PUSH_ROUTE } from '../consts';
import { HiddenDataModelType } from '../db/HiddenData';
import { sendMessageToAllServersWithSuc } from './pushService';
import { itemLogger } from '../util/logger';

export function setHiddenData(heroes: number[], goods: number[], refTime: number) {
    pinus.app.set('hiddenHeroees', heroes||[]);
    pinus.app.set('hiddenGoods', goods||[]);
    pinus.app.set('hiddenRefTime', refTime||0);
}

export async function setHiddenDataToMemory(data: HiddenDataModelType, nextData: HiddenDataModelType) {
    let heroes = data?.heroes??[];
    let goods = data?.goods??[];
    let refTime = nextData?.refTime||0;
    sendUpdateMessage(getHiddenData(), heroes, goods, refTime);
    pinus.app.rpc.activity.activityRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.battle.battleRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.chat.chatRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.connector.connectorRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.guild.guildRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.order.orderRemote.setHiddenData.broadcast(heroes, goods, refTime);
    pinus.app.rpc.role.roleRemote.setHiddenData.broadcast(heroes, goods, refTime);
    if(pinus.app.getServerType() != 'gm') {
        pinus.app.rpc.gm.gmRemote.setHiddenData.broadcast(heroes, goods, refTime);
    }
    if(pinus.app.getServerType() != 'systimer') {
        pinus.app.rpc.systimer.systimerRemote.setHiddenData.broadcast(heroes, goods, refTime);
    }
    setHiddenData(heroes, goods, refTime);
}

async function sendUpdateMessage(origin: {heroes: number[], items: number[], refTime: number}, heroes: number[], goods: number[], refTime: number) {
    console.log('##### sendUpdateMessage', origin.refTime, refTime, origin.heroes, heroes, origin.items, goods)
    if(origin.refTime != refTime || !isEqual(origin.heroes, heroes) || !isEqual(origin.items, goods)) {
        console.log('******');
        await sendMessageToAllServersWithSuc(PUSH_ROUTE.HIDDEN_DATA, { heroes, items: goods, refTime });
    }
}

export function getHiddenData() {
    let heroes: number[] = pinus.app.get('hiddenHeroees')||[];
    let goods: number[] = pinus.app.get('hiddenGoods')||[];
    let refTime = pinus.app.get('hiddenRefTime')||0;
    return {
        refTime, heroes, items: goods
    }
}

export function filterHeroes<T>(arr: T[], cb?: (obj: T) => number, roleId?: string) {
    return arr.filter(obj => {
        let hid = cb? cb(obj): (typeof obj == 'number'? obj: 0);
        let { heroes } = getHiddenData();
        if(heroes.indexOf(hid) != -1) {
            if(roleId) itemLogger.error(`玩家 ${roleId} 的武将 ${hid} 未获得`);
            return false;
        } 
        return true;
    });
}

export function filterGoods<T>(arr: T[], cb?: (obj: T) => number, roleId?: string, reason?: ITEM_CHANGE_REASON) {
    return arr.filter(obj => {
        let gid = cb? cb(obj): (typeof obj == 'number'? obj: 0);
        let { items } = getHiddenData();
        if(items.indexOf(gid) != -1) {
            if(roleId) itemLogger.error(`玩家 ${roleId} 的道具 ${gid} 未获得 (${reason})`);
            return false;
        } 
        return true;
    });
}

export function isHeroHidden(...hids: number[]) {
    for(let hid of hids) {
        let { heroes } = getHiddenData();
        if(heroes.indexOf(hid) != -1) {
            itemLogger.error(`武将 ${hid} 被拦截`);
            return true;
        }
    }
    return false;
}

export function isGoodsHidden(...gids: number[]) {
    for(let gid of gids) {
        let { items } = getHiddenData();
        if(items.indexOf(gid) != -1) {
            itemLogger.error(`道具 ${gid} 被拦截`);
            return true;
        }
    }
    return false;
}