import { ApiCall, BaseConnection } from "tsrpc";
import { PayFun } from '../../../public/pay';
import { ReqOpen, ResOpen, jijinType } from "../../../shared/protocols/event/jijin/PtlOpen";
import { payLog } from '../../../shared/protocols/pay/PtlGetList';
import { PublicShared } from '../../../shared/public/public';

/** 基金、dengjijijin
 *  关卡基金、guanqiajijin
 *  特别通行证、dayjijin
 *  天数基金、tianshujijin
 * */
export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let data = await G.mongodb.cEvent(call.req.type).findOne({ uid: call.uid, type: call.req.type });
    let payId = call.req.type == 'tianshujijin' ? 'zhongshenka' : call.req.type;
    let logs = await PayFun.getPayLog(call.uid,payId);

    call.succ({
        isPay: logs?.length > 0,
        rec: data?.rec || {},
        finished: await getVal(call, logs)
    });
}

export async function getVal(call: { conn: BaseConnection; req: ReqOpen }, logs: { [p: string]: any }, type: jijinType | false = false) {
    let val = 0;
    let _type = type || call.req.type;

    if (_type == 'dayjijin') {
        val = PublicShared.getOpenServerDay() - G.gc.eventOpen.dayjijin.day + 1;
        val = (await G.mongodb.collection('pata').findOne({ uid: call.conn.uid }))?.lv || 0;
    } else if (_type == 'dengjijijin') {
        val = call.conn.gud.lv;
    } else if (_type == 'guanqiajijin') {
        val = call.conn.gud.mapId;
    } else if (_type == 'tianshujijin') {
        val = call.conn.gud.loginDays
    }
    if (val < 0) val = 0;
    return val;
}