import { ApiCall } from 'tsrpc';
import { formatNpcData } from '../shared/fightControl/fightFun';
import { hongdianVal } from '../shared/protocols/hongdian/PtlGet';
import { wangzherongyao_baoming, wangzherongyao_staus, wzry_info } from '../shared/protocols/wzry/PtlOpen';
import { PublicShared } from '../shared/public/public';


export class WangZheRongYaofun {
    /**王者荣耀配置 */
    static async getCon() {
        let _con = G.gc.wangzherongyao;
        return _con;
    }

    /**获取赛程状态 */
    static async getWangZheStatus() {
        let _tmp = await this.getCon();
        let _con = _tmp.wangzhe.timestatus;
        let _weekOneTime = PublicShared.getToWeekMondayZeroTime();
        let _res: wangzherongyao_staus = {        // 默认比赛结束状态
            status: _con[_con.length - 1].status + 1,
            stime: _weekOneTime + _con[_con.length - 1].etime,
            etime: _weekOneTime + 7 * 24 * 3600
        };
        for (let index = 0; index < _con.length; index++) {
            const element = _con[index];
            let _stime = _weekOneTime + element.stime;
            let _etime = _weekOneTime + element.etime;
            _res.stime = _stime;
            _res.etime = _etime;
            if (_stime <= G.time && G.time < _etime) {
                _res.status = element.status;
                _res.num = await this.getBaoMingNum();
                break;
            }
        }
        return _res;
    }

    /**获取报名人数 */
    static async getBaoMingNum() {
        let zkey: string = PublicShared.getToWeek();
        let _num = G.mongodb.collection('playerInfo', 'wzry').countDocuments({ type:'wzry', isbm: 1 });
        return _num;
    }

    /**获取本服各种数据 */
    static async getMyData(call) {
        let zkey = PublicShared.getToWeek();
        let _res: wzry_info = {
            isdldrefre: 0,
            isauto: 0,
            zkey: zkey,
            isbm: 0,
            dldinfo: {
                dldpknum: 0,
                dldrefrenum: 0
            },
            isjjzs: 0,
            uid: call.uid,
        };
        let _mydata = await G.mongodb.collection('playerInfo', 'wzry').findOne({ uid: call.conn.uid, type: 'wzry' });
        if (!_mydata) {
            await G.mongodb.collection('playerInfo', 'wzry').updateOne({
                uid: call.uid,
                type: 'wzry'
            }, { $set: _res }, { upsert: true });
            return _res;
        } else {
            delete _mydata._id;
        }
        return _mydata;
    }

    /**设置报名数据 */
    static async setWzryBaoMing(call: ApiCall, setData: { [id: string]: any; }) {
        await G.mongodb.cPlayerInfo("wzry").updateOne({ uid: call.uid, type: 'wzry' }, { $set: setData }, { upsert: true });
        let data = await call.conn.getDefaultFightData();
        let _res = await G.clientCross.callApi('wzry/BaoMing', { zkey: setData.zkey, data: data });
        return _res;
    }

    /**获取我的wzry战斗数据 */
    static async getMyFight(call: ApiCall) {
        let _res = await G.clientCross.callApi('wzry/WzFightData', { uid: call.uid, iswzfight: true });
        if (_res.isSucc && _res.res.info) {
            return _res.res.info;
        }
        return false;
    }

    /**获取王者之巅数据 */
    static async getWzzd(uid: string) {
        let _res = await G.clientCross.callApi('wzry/WzFightData', { uid: uid, iswzzd: true });
        return _res;
    }

    /**获取王者之巅数据 n 条数据 */
    static async getWzzdList() {
        let _res = await G.clientCross.callApi('wzry/Wzzd', {});
        if (_res.isSucc && _res.res.wzzdlist) {
            return _res.res.wzzdlist;
        }
        return false;
    }

    /**获取wzry某一组数据 */
    static async getFightGroup(call: ApiCall, where: {}) {
        let _res = await G.clientCross.callApi('wzry/WzFightGroup', { where: where });
        if (_res.isSucc && _res.res.info) {
            return _res.res.info;
        }
        return false;
    }

    /**wzry 结束 删除相关数据 */
    static async delGroup() {
        let _res = await G.clientCross.callApi('wzry/WzFightData', { uid: '', isdel: true });
        await G.mongodb.collection('playerInfo', 'wzry').updateMany({ type: 'wzry' }, {
            $set: {
                isdldrefre: 0,
                isbm: 0,
                dldinfo: {
                    dldpknum: 0,
                    dldrefrenum: 0,
                    dldfight: []
                },
                isjingcai: {
                    totalmoney: 0,
                    guessuid: ''
                }
            }
        });
        return true;
    }

    /**设置大乱斗战斗数据 */
    static async setWzryDldFight(call: ApiCall, Fight: wangzherongyao_baoming) {
        let data = Fight.data;
        let _res = await G.clientCross.callApi('wzry/SetWzFight', { zkey: Fight.zkey, data: data, jifen: Fight.jifen });
        return _res.res;
    }

    /**增加npc数据 */
    static async addNpc(num: number) {
        let _wzryCon = await this.getCon();
        let addnpcid = _wzryCon.wangzhe.addnpcid;
        let _npcFights = [];
        let _zkey = await PublicShared.getToWeek();

        for(let index = 0; index < num; index++){
            let _fight = formatNpcData(addnpcid[index]);
            let _tmp = {
                data: _fight,
                zkey: _zkey,
                jifen: 0,
                uid: _fight.player.uid
            };
            _npcFights.push(_tmp);
        }
        return _npcFights;
    }

    /**获取增加的积分 */
    static async getMyScore(dldfight: number[]) {
        let _score = 0
        if (dldfight[dldfight.length-1] == 0) {
            _score = 10;
        } else {
            _score = 2;
        }

        let win_num = 0;
        for (let i = dldfight.length - 1; i >= 0; i--) {
            if (dldfight[i] == 0) {
                win_num++;
            } else {
                break
            }
        }
        if (win_num > 1) {
            return _score + win_num
        }
        return _score
    }

    /**红点 */
    static async getHongDian(call: ApiCall) {
        let _res: hongdianVal = {
            show: false
        };
        let [isOpen, tips] = PublicShared.getOpenCond(call, "wzrybm")
        if (!isOpen) return _res;

        // 周一0点-22点  开启功能未报名加红点 1 
        // 周二0点-22点  开启功能有大乱斗次数加红点 3
        // 周四钻石赛最后一场  未竞猜加红点
        let status = await WangZheRongYaofun.getWangZheStatus();
        let _mydata = await this.getMyData(call);
        if (status.status == 1 && _mydata.isbm == 0) {
            _res.show = true;
        }

        if (!_res.show && status.status == 3 && _mydata.dldinfo.dldpknum < G.gc.wangzherongyao.wangzhe.dlpknum) {
            _res.show = true;
        }

        if (!_res.show && status.status == 15 && !_mydata?.isjingcai?.totalmoney) {
            let _jcList = await WangZheRongYaofun.getFightGroup(call, { deep: { $gte: 5 } });
            if (_jcList) {
                _res.show = true;
            }
        }

        return _res;
    }

    /**转str */
    static async fmtContent(content: string, ...arg) {
        if (arguments.length < 2) return content;
        if (arguments.length == 2 && Object.prototype.toString.call(arguments[1]) == "[object Array]") {
            let args = arguments[1];
            for (let i = 0; i < args.length; i++) {
                content = content.replace(new RegExp("\\{" + (i + 1) + "\\}", "g"), args[i]);
            }
        } else {
            for (let i = 1; i < arguments.length; i++) {
                content = content.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
            }
        }
        return content;
    }
}
