import { ApiCall } from "tsrpc";
import { GongGaoBan_info, Reqnotification, Resnotification } from "../../monopoly/protocols/games/Ptlnotification";
import { ChatFun } from "../../public/chat";
import { EmailFun } from "../../public/email";
import { NotifiCationFun } from "../../public/notification";
import { ServerToolFun } from "../../public/servertooltcp";

export default async function (call: ApiCall<Reqnotification, Resnotification>) {
    let template = call.req.template
    let action = call.req.action

    if (template == "server_mail") {   // 全服邮件
        if (action == "offline") {  // publish 发布全服邮件/个人邮件/公告板/跑马灯  offline 下线全服邮件/公告板/跑马灯
            // 根据g123 唯一id 删除邮件 
            await G.mongodb.collection('email').deleteMany({ "g123log.id": { $in: call.req.data } });
        } else {
            let _prize = await NotifiCationFun.getFmtPrize(call.req.data.assets)

            let _mails = call.req.data.mail
            for (let indexMail = 0; indexMail < _mails.length; indexMail++) {
                const elementMail = _mails[indexMail];
                EmailFun.addQuanFuEmail({
                    uid: "system",
                    type: 'gm',
                    title: elementMail.header,
                    content: elementMail.content,
                    prize: _prize,
                    contentInsertArr: [],
                    createTime: call.req.data?.startTime || G.time,
                    g123log: { id: call.req.data.id }
                })
            }
        }

    } else if (template == "player_mail") {     // 个人邮件
        let _playerIds = call.req.data.playerIds
        for (let index = 0; index < _playerIds.length; index++) {
            const element = _playerIds[index];
            // 邮件内容
            let _prize = await NotifiCationFun.getFmtPrize(call.req.data.assets)
            let _mails = call.req.data.mail
            for (let indexMail = 0; indexMail < _mails.length; indexMail++) {
                const elementMail = _mails[indexMail];
                EmailFun.addEmail({
                    uid: element,
                    type: 'gm',
                    title: elementMail.header,
                    content: elementMail.content,
                    prize: _prize,
                    contentInsertArr: [],
                    createTime: call.req.data?.sendTime || G.time,
                    g123log: { id: call.req.data.id }
                })
            }
        }
    } else if (template == "news_ticker") {      // 跑马灯
        if (action == "offline") {     // 删除跑马灯
            ChatFun.newMsg({
                type: 'local',
                msg: "",
                time: G.time,
                sender: 'system',
                otherData: {
                    pmd: false,
                    args: [],

                    ids: call.req.data
                }
            })

        } else {
            let newsTicker = call.req.data.newsTicker
            for (let index = 0; index < newsTicker.length; index++) {
                const element = newsTicker[index];

                ChatFun.newMsg({
                    type: 'local',
                    msg: element.content,
                    time: call.req.data.startTime,
                    sender: 'system',
                    otherData: {
                        pmd: true,
                        args: [],

                        interval: call.req.data.interval,     // 播放间隔，单位分钟，为0时仅播放frequency次
                        frequency: call.req.data.frequency,   // 每回播放频次，单位条数
                        id: call.req.data.id                  // G123生成的告知唯一ID
                    }
                })
            }

        }
    } else if (template == "bulletin") {         // 游戏公告板(登录游戏之前的公告)
        if (action == "offline") {         // 删除公告
            ServerToolFun.postServer("server/delGongGao", { "gonggao": call.req.data })
        } else {
            let _gongGao: GongGaoBan_info[] = []
            let bulletin = call.req.data.bulletin

            let levelMin = call.req.data.levelMin
            let levelMax = call.req.data.levelMax
            let stime = call.req.data.startTime
            let etime = call.req.data.endTime
            for (let index = 0; index < bulletin.length; index++) {
                const element = bulletin[index];

                let _tmp: GongGaoBan_info = {
                    content: element.content,
                    title: element.header,
                    stime: stime,
                    etime: etime,
                    sort: "0",      // 写入的时候会复写
                    otherData: {
                        levelMin: levelMin,
                        levelMax: levelMax,
                        id: call.req.data.id,
                        serverIds: call.req.data.serverIds
                    }
                }
                _gongGao.push(_tmp)
            }
            ServerToolFun.postServer("server/setGongGao", { "gonggao": _gongGao })
        }
    }

    call.succ({ message: "success", code: 0 });
}