import { boolean } from 'mathjs';
import { ApiCall } from 'tsrpc';
import { PublicShared } from '../shared/public/public';


export class DiXiaQianZhuangFun {
    /**获取配置信息 */
    static async getCon() {
        let _con = G.gc.dixiaqianzhuang;
        return _con;
    }

    /**获取某个vip等级可购买的次数上限 */
    static async getCanBuyNum(vip: number) {
        let canbuyNum = 0;
        let _con = await this.getCon();
        for(let id in _con){
            if(vip >= _con[id].needVIP){
                canbuyNum = Math.max(canbuyNum,..._con[id].cs);
            }
        }
        return canbuyNum;
    }

    /**获取我的数据 */
    static async getMyData(call: ApiCall,) {
        let result = await G.mongodb.collection('playerInfo', 'dixiaqianzhuang').findOne({ uid: call.uid, type: 'dixiaqianzhuang' });
        let num: number = 0;
        if (result && PublicShared.chkSameDate(result.time, G.time)) {
            num = result.num;
        }
        return num;
    }

    /**返回 第 num 次祈福的消耗*/
    static async getNumNeed(call: ApiCall, num: number) {
        let _res = {
            jinbi: 0,
            need: 0,
            needvip: 0
        };
        let _con = await this.getCon();
        let maxJb = G.gc.com.dxqz_dh_sx.value;   // 单次获得金币最大值
        let lv = call.conn.gud.lv;
        // let lv = 10000000                            // 测试数据
        let _gongshi = G.gc.com.dxqz_gs.value.toString();   // 获得金币公式
        let _isnum: 0 | 1 = 0;
        for (let index = 1; index <= Object.keys(_con).length; index++) {
            const element = _con[index.toString()];

            let acquire = Math.floor(eval(_gongshi));
            if (acquire > maxJb) acquire = maxJb;

            if (element.cs[0] <= num && num <= element.cs[1]) {
                _res.jinbi += acquire;
                _res.need += element.need;
                _res.needvip = element.needVIP;
                _isnum = 1;
                break;
            }
        }
        if (_isnum == 0) {
            _res.needvip = 999999;
        }
        return _res;
    }

    /**获取暴击倍数 */
    static async getBj() {
        let _sjNum = PublicShared.randomNum(1, 100);
        let _twoNum = Number(G.gc.com.dxqz_crit_2.value) * 100;
        let _threeNum = Number(G.gc.com.dxqz_crit_3.value) * 100;
        let bj = 1;
        if (_sjNum <= _threeNum) {
            bj = 3;
        } else if (_sjNum <= _twoNum) {
            bj = 2;
        }
        return bj;
    }

    /**红点 */
    static async getHongDian(call: ApiCall) {
        let _res = {
            show: false
        };
        let _num = await this.getMyData(call) + 1;
        let _con = await this.getNumNeed(call, _num);
        if (_con.needvip <= call.conn.gud.vip && !_con.need) {
            _res.show = true;
        }
        return _res;
    }
}