import { UpdateFilter } from 'mongodb';
import { paTaType } from '../shared/protocols/pata/type';
import { PublicShared } from '../shared/public/public';


export class PataFun {
    /**获取爬塔信息 */
    static async getInfo(uid: string, sid: number = 0) {
        let info: paTaType = await G.mongodb.collection('pata').findOne({ uid: uid, sid: sid });
        let nt = G.time;
        if (!info) {
            info = {
                lv: 1,
                useNum: 0,
                lastRefreshTime: nt,
                gotarr: [],
                sid: sid,
                saodangNum: 0
            };
            this.changeInfo(uid, info.sid, { $set: info });
        }

        /** 第二天 */
        if (PublicShared.chkSameDate(nt, info.lastRefreshTime) == false) {
            let setData = {
                lastRefreshTime: nt,
                useNum: 0,
                saodangNum: 0
            };
            this.changeInfo(uid, info.sid, { $set: setData });
        }

        if (info['_id']) {
            delete info['_id'];
        }

        return info;
    }

    /**修改爬塔信息 */
    static async changeInfo(uid: string, sid: number, change: Pick<UpdateFilter<paTaType>, '$set'>) {

        G.mongodb.collection('pata').updateOne(
            { uid: uid, sid: sid },
            change,
            { upsert: true }
        );
    }
    
    /**获取爬塔红点 */
    static async getHongDian(uid: string) {
        let mydata = await this.getInfo(uid);
        let con = Object.keys(G.gc.patacom);
        con.sort((a, b) => { return parseInt(a) - parseInt(b); });
        for (let idx in con) {
            idx = con[idx];
            if (mydata.lv <= parseInt(idx)) {
                break;
            }
            // 已经领取
            if (!mydata.gotarr.indexOf(parseInt(idx))) {
                continue;
            }
            return 1;
        }

        return 0;

    }
}