import { ApiCall } from "tsrpc";
import { HuoDongFun } from "../../../public/huodongfun";
import { ReqCanReceive, ResCanReceive } from '../../../shared/protocols/event/payForDiamond/PtlCanReceive';
import { PublicShared } from "../../../shared/public/public";
import { ActionLog } from "../../../public/actionLog/actionLog";
import { WithId, OptionalId } from "mongodb";
import { ReqAddHuoDong } from "../../../monopoly/protocols/PtlAddHuoDong";

const hasGotKeyPrefix = 'payForDiamond:hasGot:'; // 已领取额度
const showOffListKeyPrefix = 'payForDiamond:ShowOff:'; // 需要炫耀的玩家列表。 其中需要包含信息：玩家区服, 玩家玩家名称, 领到的数量

export async function playerCanReceive(call: ApiCall, callError : boolean = true) {
    let activityId = call.req.activityId;
    let activityInfo: WithId<OptionalId<ReqAddHuoDong>>;
    if (!activityId) {
        activityInfo = (await HuoDongFun.gethdList(call, 13))[0];
    } else {
        activityInfo = await HuoDongFun.getHdidInfo(call, activityId);
    }
    if (!activityInfo) {
        if (callError) {
            return call.error('', { code: -1, message: lng.huodong_open_1 });
        } else {
            return { result: false };
        }
    } 
    activityId = activityInfo.hdid;
    const hasReceivedKey = hasGotKeyPrefix + activityId;
    const hasReceivedStr = await G.crossioredis.get(hasReceivedKey);
    const hasReceived = hasReceivedStr? parseInt(hasReceivedStr) : 0;
    const remaining = activityInfo.data['totalmoney'] - hasReceived;
    const showOffResult = await G.crossioredis.lrange(showOffListKeyPrefix + activityId, 0, -1);
    const showOffList = showOffResult.map(result => JSON.parse(result));
    const zeroTime = PublicShared.getToDayZeroTime();
    const vipScore = await ActionLog.getDayLog(call.uid, 'pay');
    const price = activityInfo.data['price'];
    if (!vipScore) {
        return {
            payNum: 0, remaining, result: false, activityInfo, showOffList, price, gotAmount: 0
        };
    }
    const payNum = vipScore;
    // 玩家充值未达标或者奖池余额耗尽则不能领取
    if (payNum < price || remaining <= 0) {
        return {
            payNum, remaining, result: false, activityInfo, showOffList, price, gotAmount: 0
        }
    }
    // 检查玩家今日是否已经领取
    const playerActivityInfo = await G.mongodb.cEvent('payForDiamond').findOne({ uid: call.uid, type: 'payForDiamond' });
    if (playerActivityInfo) {
        if (playerActivityInfo[zeroTime]) {
            return {
                payNum, remaining, result: false, activityInfo, showOffList, price, gotAmount: playerActivityInfo[zeroTime]
            };
        }
    }
    return {
        payNum, remaining, result: true, activityInfo, showOffList, price, gotAmount: 0
    };
}

export default async function (call: ApiCall<ReqCanReceive, ResCanReceive>) {
    const canReceiveResult = await playerCanReceive(call);
    if (canReceiveResult) {
        const { payNum, remaining, result, showOffList, price, gotAmount } = canReceiveResult;
        call.succ({ payNum, remaining, result, showOffList, price, gotAmount });
    }
}