import {ApiCall} from 'tsrpc';
import {ReqAddHuoDong} from '../monopoly/protocols/PtlAddHuoDong';
import {PublicShared} from '../shared/public/public';
import {UserFun} from './user';

const HUODONG_SHUJUKU = "hdinfo";

export class HuoDongFun {
    /**插入活动数据 */
    static async addHuoDong(info: ReqAddHuoDong) {
        let result = (await G.mongodb.collection(HUODONG_SHUJUKU).insertOne(info)).insertedId.toHexString();
        return result;
    }

    /**删除活动数据 */
    static async delHD(hdid: number) {
        let result = await G.mongodb.collection(HUODONG_SHUJUKU).deleteOne({hdid: hdid})
        return result
    }

    /**查看所有活动数据 包括不符合时间的活动 */
    static async catAllHD(where: { [id: string]: any }) {
        let result = await G.mongodb.collection(HUODONG_SHUJUKU).find(where).toArray()
        return result
    }

    /**
     * 获取所有符合时间的活动（默认根据实际结束时间查询）
     * 1.可根据 htype 查询
     * 2.永久活动的 rtime etime stime 全为0
     * 3.ttype 为 4 屏蔽活动
     */
    static async gethdList(call: ApiCall, htype: number = 0) {
        let _zeroTime = PublicShared.getToDayZeroTime(call.conn.gud.cTime)
        let _pDay = Number((G.time - _zeroTime) / 86400)

        let openTime = PublicShared.getToDayZeroTime(G.openTime)
        let _sDay = Number((G.time - openTime) / 86400)

        // ttype为0 根据开服时间计算，ttype为1，根据用户注册时间计算
        let _where = {
            $or: [
                {
                    ttype: 0,
                    $or: [{stime: {$lte: _sDay}, etime: {$gte: _sDay}}, {stime: {$lte: G.time}, etime: {$gte: G.time}}]
                },
                {
                    ttype: 1,
                    stime: {$lte: _pDay},
                    etime: {$gte: _pDay}
                }]
        }

        if (htype != 0) _where["htype"] = htype

        let _res = await this.catAllHD(_where)
        let result = []
        for (let index = 0; index < _res.length; index++) {
            const element = _res[index];
            // 算出符合注册时间的活动时间
            let initTime = element.ttype ? call.conn.gud.cTime : openTime
            if (element.stime < 99999999) {
                element.stime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * element.stime)
                element.rtime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * element.rtime) - 1
                element.etime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * element.etime) - 1
            }
            let _st = PublicShared.fmtTime(element["stime"])
            let _rt = PublicShared.fmtTime(element["rtime"])
            element["showtime"] = _st + " - " + _rt
            result.push(element)
        }

        return await this.chkOpenHd(call, result)
    }

    // 活动开启条件
    static async chkOpenHd(call: ApiCall, hds) {
        let _res = []
        for (let index = 0; index < hds.length; index++) {
            const element = hds[index];
            if (element.opencond) {
                let [isOpen, tips] = PublicShared.getOpenCond(call, element.opencond)
                if (isOpen) _res.push(element)
            } else {
                _res.push(element)
            }
        }
        return _res
    }

    /**根据hdid 获取对应的活动 */
    static async getHdidInfo(call: ApiCall, hdid: number) {
        let _where: {} = {
            hdid: hdid
        }
        let result = await G.mongodb.collection(HUODONG_SHUJUKU).findOne(_where)
        if (result && Object.keys(result).length > 0) {
            let openTime = PublicShared.getToDayZeroTime(G.openTime)
            let initTime = result.ttype ? call.conn.gud.cTime : openTime
            if (result.stime < 99999999) {
                result.stime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * result.stime)
                result.rtime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * result.rtime) - 1
                result.etime = PublicShared.getToDayZeroTime(initTime + 24 * 3600 * result.etime) - 1
            }
            let _st = PublicShared.fmtTime(result["stime"])
            let _rt = PublicShared.fmtTime(result["rtime"])
            result["showtime"] = _st + " - " + _rt
        }
        if (result && result._id) delete result._id
        return result
    }

    /**增加游戏内活动 */
    static async fmtHD() {
        let _r = await G.mongodb.collection(HUODONG_SHUJUKU).findOne({});
        if (_r && Object.keys(_r).length > 0) return;
        let _con = G.gc.huodong
        let [_addHd, _openTime] = [[], PublicShared.getToDayZeroTime(G.openTime)]
        for (let index = 0; index < _con.length; index++) {
            const element = _con[index];

            if (element["ttype"] == 0) {
                if (element["stime"] == -1) {    // 永久活动
                    element["stime"] = 0
                    element["rtime"] = 0
                    element["etime"] = 0
                    element["showtime"] = "永久"
                } else {                        // 不是永久活动，直接使用传输时间；检测显示时间
                    // element.stime = PublicShared.getToDayZeroTime(_openTime + 24 * 3600 * element.stime)
                    // element.rtime = Number(PublicShared.getToDayZeroTime(_openTime + 24 * 3600 * element.rtime)) + 24 * 3600 - 1
                    // element.etime = Number(PublicShared.getToDayZeroTime(_openTime + 24 * 3600 * element.etime)) + 24 * 3600 - 1

                    // let _st = PublicShared.fmtTime(element["stime"])
                    // let _rt = PublicShared.fmtTime(element["rtime"])
                    // element["showtime"] = _st + " - " + _rt
                }
            } else if (element["ttype"] == 1) {
                element["showtime"] = "根据玩家注册时间，游戏返回时复写"
            }
            _addHd.push(element)
        }
        if (_addHd.length > 0) await G.mongodb.collection(HUODONG_SHUJUKU).insertMany(_addHd);
        return
    }

}
