import {PayFun} from "./pay";

const PushGiftType = {
    LevelGift: 1,     // 关卡推送
    ItemGift: 2,      // 道具推送
    LoseGift: 3,      // 战败推送
    RecruitGift: 4,   // 十连推送
    LvGift: 5,        // 等级推送
}

export class PushGiftFun {

    static async getGift(uid: string) {
        // 查询所有礼包
        return (await G.mongodb.collection("pushgift").find({
            uid: uid
        }).toArray()).map((i) => {
            let {_id, ...gift} = i;
            return gift
        })
    }

    static async chkItemGift(uid: string, atn: atn) {
        let gift_ids = [];
        for (let id in G.gc.tuisonglibao) {
            let conf = G.gc.tuisonglibao[id];
            if (conf.type == PushGiftType.ItemGift && conf.num[0].a == atn.a && conf.num[0].t == atn.t) {
                gift_ids.push(id);
            }
        }
        if (gift_ids.length <= 0) {
            return
        } else {
            this.pushGift(uid, gift_ids)
        }
    }

    static async chkLevelGift(uid: string, level: number) {
        let gift_ids = [];
        for (let id in G.gc.tuisonglibao) {
            let conf = G.gc.tuisonglibao[id];
            if (conf.type == PushGiftType.LevelGift && conf.num[0] == level) {
                gift_ids.push(id);
            }
        }
        if (gift_ids.length <= 0) {
            return
        } else {
            this.pushGift(uid, gift_ids)
        }
    }

    static async chkLvGift(uid: string, lv: number) {
        let gift_ids = [];
        for (let id in G.gc.tuisonglibao) {
            let conf = G.gc.tuisonglibao[id];
            if (conf.type == PushGiftType.LvGift && conf.num[0] == lv) {
                gift_ids.push(id);
            }
        }
        if (gift_ids.length <= 0) {
            return
        } else {
            this.pushGift(uid, gift_ids)
        }
    }

    static async chkLoseGift(uid: string) {
        let gift_ids = [];
        for (let id in G.gc.tuisonglibao) {
            let conf = G.gc.tuisonglibao[id];
            if (conf.type == PushGiftType.LoseGift) {
                gift_ids.push(id);
            }
        }
        if (gift_ids.length <= 0) {
            return
        } else {
            this.pushGift(uid, gift_ids)
        }
    }

    static async chkRecruitGift(uid: string) {
        let gift_ids = [];
        for (let id in G.gc.tuisonglibao) {
            let conf = G.gc.tuisonglibao[id];
            if (conf.type == PushGiftType.RecruitGift) {
                gift_ids.push(id);
            }
        }
        if (gift_ids.length <= 0) {
            return
        } else {
            this.pushGift(uid, gift_ids)
        }
    }

    static async pushGift(uid: string, gift_ids: string[]) {
        let gifts = {};
        (await this.getGift(uid)).forEach(i => gifts[i.id] = i)
        for (let gift_id of gift_ids) {
            let conf = G.gc.tuisonglibao[gift_id];
            if (gifts[gift_id] && conf.displayCD > 0 && gifts[gift_id].ctime + conf.displayCD > G.time) {
                continue
            }
            if (conf.type == PushGiftType.RecruitGift) {
                if (gifts[gift_id]) {
                    if (gift_ids[gift_id].ext_data.num + 1 >= conf.num[0]) {
                        this.addGift(uid, gift_id, {num: 0})
                    } else {
                        G.mongodb.collection("pushgift").updateOne({
                            uid: uid, id: gift_id
                        }, {"$inc": {"ext_data.num": 1}})
                    }
                } else {
                    // 招募礼包不存在 初始化数据 记录招募次数
                    G.mongodb.collection("pushgift").updateOne({
                        uid: uid, id: gift_id
                    }, {
                        $set: {
                            ctime: 0,
                            passTime: 0,
                            ext_data: {num: 1},
                            buy: conf.payId.map(() => 0),
                        }
                    }, {upsert: true})
                }
            } else {
                this.addGift(uid, gift_id, {})
            }
        }
    }

    static async addGift(uid: string, giftid: string, ext_data: { [key: string]: any }) {
        let conf = G.gc.tuisonglibao[giftid];
        // 更新礼包
        await G.mongodb.collection("pushgift").updateOne({
            uid: uid, id: giftid
        }, {
            $set: {
                ctime: G.time,
                ext_data: ext_data,
                passTime: G.time + conf.time,
                buy: conf.payId.map(() => 0),
            }
        }, {upsert: true})

        // 删除购买记录
        PayFun.delPayLog(uid, ...conf.payId.map(i => {
            return {payId: i, val: []}
        }))
        // 推送客户端消息
        G.server.sendMsgByUid(uid, "msg_s2c/PushGiftChange", conf.id);
    }

    static async buy(uid: string, payId: string) {
        // 购买礼包
        let gift;
        for (let gift_id in G.gc.tuisonglibao) {
            let temp = G.gc.tuisonglibao[gift_id];
            if (temp.payId.includes(payId)) {
                gift = temp;
                break
            }
        }
        if (!gift) return;
        let index = gift.payId.indexOf(payId);
        let info = await G.mongodb.collection("pushgift").findOne({
            uid: uid, id: gift.id.toString()
        })
        if (info.passTime >= G.time) {
            await G.mongodb.collection("pushgift").updateOne({
                uid: uid, id: gift.id.toString()
            }, {$inc: {[`buy.${index}`]: 1}})
            // 推送客户端消息
            G.server.sendMsgByUid(uid, "msg_s2c/PushGiftChange", gift.id);
        }
    }
}