import {PublicShared} from "../shared/public/public";
import {RankKfjs} from "./rank/rank_kfjs";


function getStatus(type?) {
    if (type) return {conf: G.gc.kfcb_content[type], type}
    let openServerDay = PublicShared.getOpenServerDay()
    let conf = Object.values(G.gc.kfcb_content).find(i => i.showDay[0] < openServerDay && i.showDay[1] >= openServerDay)
    if (!conf) return {type: 999}
    return {conf, type: conf.type}
}

function getPrize(type) {
    let status = getStatus()
    if (status.type <= type) return []
    let conf = R.compose(R.filter(i => i.type == type), R.values())(G.gc.kfcb_prize)
    return conf || []
}

async function setHeroLvCount(call) {
    let status = getStatus()
    if (status.type != 2) return
    let arrList = await G.mongodb.find('hero', {
        uid: call.uid,
        _id: {$in: Object.values(call.conn.gud.heroPos).map(i => G.mongodb.conversionId(i))}
    }, ['lv']);

    let score = R.sum(arrList.map(i => i.lv))

    let rankKfjs = RankKfjs(2)
    rankKfjs.setDataAndCheck({
        player: call.conn.gud,
        valArr: [score]
    });
}

async function setEquipLvCount(call) {
    let status = getStatus()
    if (status.type != 3) return
    let rankKfjs = RankKfjs(3)
    let equips = await G.mongodb.collection('equip').find({
        uid: call.uid,
        wearaId: {$in: R.values(call.conn.gud.heroPos)}
    }).toArray();
    rankKfjs.setDataAndCheck({
        player: call.conn.gud,
        valArr: [R.compose(R.sum(), R.map(i => i.lv))(equips)]
    });
}

export default {
    getStatus: getStatus,
    getPrize: getPrize,
    setHeroLvCount: setHeroLvCount,
    setEquipLvCount: setEquipLvCount
}