import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqLoginPrize, ResLoginPrize } from "../../../shared/protocols/event/kaifukuanghuan/PtlLoginPrize";
import { PublicShared } from '../../../shared/public/public';
import { HongDianChange } from "../../hongdian/fun";

export default async function (call: ApiCall<ReqLoginPrize, ResLoginPrize>) {
    let day = PublicShared.getOpenServerDay(call.conn.gud.cTime);
    let data = await G.mongodb.cEvent('kaifukuanghuan').findOne({ uid: call.uid, type: 'kaifukuanghuan' });

    if (data.receiveLogin.length >= day) return call.error(globalThis.lng.event_kfkh_3);

    let recIndex = Array(day).fill(1).map((v, i) => i).filter(i => !data.receiveLogin.includes(i));
    let prize = recIndex.map(i => G.gc.kaifukuanghuan.loginPrize[i]).reduce((a, b) => a.concat(b));
    let _prize = PublicShared.mergePrize(prize);

    G.mongodb.cEvent('kaifukuanghuan').updateOne({ uid: call.uid, type: 'kaifukuanghuan' }, { $push: { receiveLogin: { $each: recIndex } } });
    await PlayerFun.sendPrize(call, _prize);

    HongDianChange.sendChangeKey(call.uid, ['kaifukuanghuanhd'])
    
    call.succ({
        prize: _prize
    });
}