import { OptionalId, UpdateFilter } from 'mongodb';
import { ApiCall } from "tsrpc";
import { CollectionPlayerInfo } from '../../module/collection_palyerInfo';
import { PlayerFun } from '../../public/player';
import { ReqQf, ResQf } from "../../shared/protocols/dixiaqianzhuang/PtlQf";
import { ResOpen } from '../../shared/protocols/dixiaqianzhuang/PtlOpen';
import { DiXiaQianZhuangFun } from '../../public/dixiaqianzhuang';
import { HongDianChange } from '../hongdian/fun';

export default async function (call: ApiCall<ReqQf, ResQf>) {
    let need: atn[];
    let prize: atn[];
    let _num = await DiXiaQianZhuangFun.getMyData(call)
    let canbuyNum = await DiXiaQianZhuangFun.getCanBuyNum(call.conn.gud.vip);
    
    let update: UpdateFilter<OptionalId<CollectionPlayerInfo<"dixiaqianzhuang">>>;
    let changeData: Partial<ResOpen>;
    let _rbj = {}      // 返回的暴击次数

    let _needNum: number = 0
    let _jinbi: number = 0
    let isnum: number = 0   // 实际次数
    if (call.req.type == 1) {

        //兑换次数判断
        if( canbuyNum-_num <= 0){
            return call.error('', { code: -2, message: "dxqz_t3" })
        }
        
        let _myNeed = await DiXiaQianZhuangFun.getNumNeed(call, _num + 1)
        // vip 条件不足
        if (_myNeed.needvip > call.conn.gud.vip) {
            return call.error('', { code: -1, message: globalThis.lng.dixaiqianzhuang_1 })
        }

        if (_myNeed.need) {
            _needNum = _myNeed.need
        }
        let _bj = await DiXiaQianZhuangFun.getBj()
        _jinbi += _myNeed.jinbi * _bj
        _rbj[_bj.toString()] = 1
        isnum = 1

    } else if (call.req.type == 10) {
        let _maxTimes = 10;
        if( _maxTimes > canbuyNum-_num){
            _maxTimes = canbuyNum-_num;
        }
        for (let index = 1; index <= _maxTimes; index++) {
            let _myNeed = await DiXiaQianZhuangFun.getNumNeed(call, _num + index)
            if (_myNeed.needvip > call.conn.gud.vip) {
                break
            }
            
            isnum += 1
            if (_myNeed.need) {
                _needNum += _myNeed.need
            }

            let _bj = await DiXiaQianZhuangFun.getBj()
            // console.log("第 {} 次暴击 {} 倍数", index, _bj)
            if (_myNeed.jinbi) {
                _jinbi += _myNeed.jinbi * _bj
            }

            // 返回的暴击次数

            if (_rbj[_bj]) {
                _rbj[_bj] += 1
            } else {
                _rbj[_bj] = 1
            }
        }
        
        // 一次次数都没有
        if (isnum == 0) {
            return call.error('', { code: -2, message: "dxqz_t3" })
        }
    }

    // 更新数据的数据
    update = {
        $set: { time: G.time, num: isnum + _num }
    };

    if (_needNum) {
        need = [{ "a": "attr", "t": "rmbmoney", "n": _needNum }]
        let meet = await PlayerFun.checkNeedByArgs(call, need[0]);
        need = [meet.atn];
    }
    prize = [{ "a": "attr", "t": "jinbi", "n": _jinbi }]

    // 设置数据
    G.mongodb.collection('playerInfo', 'dixiaqianzhuang').updateOne(
        {
            uid: call.uid,
            type: 'dixiaqianzhuang'
        },
        {
            ...update
        },
        {
            upsert: true
        }
    );

    // 扣除奖励和发奖
    need && await PlayerFun.cutNeed(call, need);
    await PlayerFun.sendPrize(call, prize);

    changeData = {
        num: _num + isnum
    };
    HongDianChange.sendChangeKey(call.uid, ['dixiaqianzhuanghd', 'taskhd', 'huodonghd'])

    call.succ({
        prize: prize,
        changeData: changeData,
        bj: _rbj
    });
}