import { ApiCall } from 'tsrpc';
import { ResOpen } from '../shared/protocols/dixialeitai/PtlOpen';
import { PublicShared } from '../shared/public/public';


export class DxltFun {
    /**修改数据 */
    static async changeData(call: ApiCall, change: Partial<ResOpen>) {

        let data = await this.getData(call);
        Object.assign(data, change);
        G.ioredis.set(`dxlt:${call.uid}`, JSON.stringify(data));

        G.mongodb.collection('dxlt').updateOne({ uid: call.uid }, { $set: { ...change } });
        return change;
    }
    static async getData(call: ApiCall) {
        return JSON.parse(await G.ioredis.get(`dxlt:${call.uid}`)) ;
    }
    /**回退层数 */
    static backLayer(data: ResOpen) {
        let num = data.curLayer - G.gc.dxlt_com.dayResetBackLayer;
        data.curLayer = num < 1 ? 1 : num;
        data.over = G.gc.dxlt_layer[data.curLayer].type == 2;
    }
    /**扫荡 */
    static saodan(data: ResOpen, num?: number) {
        let prizeNum = 0;

        data.saodang = {
            item: {},
            buff: {}
        };

        let conf = G.gc.dxlt_layer[data.curLayer];

        if (conf.isBoss) {
            let random = PublicShared.randomDropAny(G.gc.dxlt_com.fightPrizeBoss.random);

            if (random.dlz) data.saodang.prize = PublicShared.randomDropGroup(random.dlz);
            if (random.mibaoNum) data.saodang.mibaoNum = random.mibaoNum;
        } else {
            let r = PublicShared.randomDropAny(G.gc.dxlt_com.fightPrize.random);

            switch (r.type) {
                case 'item':
                    let item = PublicShared.randomDropAny(r.rd);
                    let itemObj = Object.fromEntries(Object.keys(item).filter(k => k == 'id').map(k => [item[k], 1]));
                    PublicShared.mergeProperty(data.saodang.item, itemObj);
                    PublicShared.mergeProperty(data.item, itemObj);
                    break;
                case 'buff':
                    let buff = PublicShared.randomDropAny(r.rd);
                    let buffObj = Object.fromEntries(Object.keys(buff).filter(k => k == 'id').map(k => [buff[k], 1]));
                    PublicShared.mergeProperty(data.saodang.buff, buffObj);
                    PublicShared.mergeProperty(data.buff, buffObj);
                    break;
                case 'prize':
                    prizeNum++;
                    break;
                case 'shop':
                    data.saodang.shop = true;
                    break;
            }
            if (prizeNum > 0) {
                data.saodang.prize = PublicShared.randomDropGroup(G.gc.dxlt_com.fightPrize.prizeDlz, prizeNum);
            }
        }
    }
    /**获取buff */
    static getBuff(data: ResOpen) {
        let buff = {};

        Object.entries(data.buff).forEach(b => {
            PublicShared.mergeProperty(buff, G.gc.dxlt_com.fightPrize.buff[b[0]]);
            buff[b[0]] *= b[1];
        });

        return buff;
    }
    /**通关一层后修改数据 */
    static passLayerChange(data: ResOpen, change: Partial<ResOpen>) {
        if (data.curLayer > data.maxLayer) change.maxLayer = data.curLayer;
        if (data.curLayer > data.curMaxLayer) change.curMaxLayer = data.curLayer;
        if (data.recordLayer < G.gc.dxlt_com.getMiBaoByFightNum * G.gc.dxlt_com.getPrizeByMiBaoNum) change.recordLayer = data.recordLayer + 1;
    }
    /**修改商店 */
    static changeShop(data: ResOpen, change: Partial<ResOpen>) {
        let shop = PublicShared.randomDropAny(Object.values(G.gc.dxlt_shop));

        if (data.shop[shop.id]) {
            data.shop[shop.id].buyNum++;
        } else {
            data.shop[shop.id] = {
                ...shop,
                useBuyNum: 0
            };
        }
        change.shop = data.shop;
    }

    /**红点 子函数 */
    static async getFinish(call: ApiCall, _type: string, data) {
        let ishd: 0 | 1 = 0;
        let _val = _type == 'killPrizeRec' ? (data.killBoss || 0) : data.maxLayer;
        let _taskCon = _type == 'killPrizeRec' ? G.gc.dxlt_com.killPrize : G.gc.dxlt_com.passPrize;
        for (let indexTask = 0; indexTask < _taskCon.length; indexTask++) {
            const elementTask = _taskCon[indexTask];
            if (elementTask.need > _val) continue;
            if (data[_type].includes(indexTask)) continue;
            ishd = 1;
            break;
        }
        return ishd;
    }

    /**红点 */
    static async getHongDian(call: ApiCall) {
        let _res = {
            show: false
        };
        let data = await this.getData(call);
        if (!data) return _res;
        if (data.recordLayer / G.gc.dxlt_com.getMiBaoByFightNum >= G.gc.dxlt_com.getPrizeByMiBaoNum) {
            // 秘宝奖励
            _res.show = true;
        }

        if (!_res.show) {
            const _type = ['killPrizeRec'];
            for (let index = 0; index < _type.length; index++) {
                const element = _type[index];
                let ishd = await this.getFinish(call, element, data);
                if (ishd) {
                    _res.show = true;
                    break;
                }
            }
        }
        return _res;
    }
}