import { ApiCall } from "tsrpc";
import { JJCFun } from '../../public/jjc';
import { Rank } from '../../public/rank/rank';
import { ReqOpen, ResOpen } from "../../shared/protocols/rank/PtlOpen";
import { getToper50RankInfo } from "../hbzb/jfs/fun";
import { getZbsRankList } from "../hbzb/zbs/fun";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    console.log("==============>", call.req)
    let obj = await rankOpenfun(call, call.req);
    call.succ(obj);
}


// 参数需调整，req数组应使用types字段接收，需增加分页参数,需前端配合，优化redis存储暂不改。
/**
 * 
 * @param call 
 * @param req <array> 旧模式兼容，保留数组的判断
 * @param req.type <array> 查询数据的类型
 * @param req.page 页码，从0开始
 * @param req.offset 每页条数
 * @returns 
 */
export async function rankOpenfun(call, req: ReqOpen): Promise<ResOpen> {
    let obj: ResOpen = {};
    // 数组，兼容旧的参数
    if (req instanceof Array) {
        var types = req
        var page = 0
        var offset = 1

    } else {
        var types = req.type
        var page = req.page || 0
        var offset = req.offset || 1
    }
    // 取排行范围
    // 应用规则：接口类（如跨服）统一使用page&offset, 接口内部需要min&max自行转换； 调用函数使用min,max
    const { min, max } = Rank.pageToMin(page, offset)
    for (let type of types) {
        switch (type) {
            case 'jjc':
                // 获取前50名ranklist数据
                let rankList = await JJCFun.getRankList(min, max);
                // 获取自己的排名
                let myRank = await JJCFun.getRankSortByUid(call.uid)
                // 获取自己的rank信息
                let myRankInfo = await JJCFun.getPlayerData(call.uid)
                obj[type] = {
                    rankList: rankList,
                    myRank: {
                        rank: myRank,
                        player: myRankInfo?.player || call.conn.gud,
                        valArr: myRankInfo?.valArr || [],
                        otherData: myRankInfo?.otherData || {}
                    }
                };
                break;
            // case 'hbzbCross':
            //     obj[type] = await Rank.list.hbzbLocal.getCrossRankList(call.uid, page, offset);
            //     break;
            // case 'hbzbZbsCross':
            //     obj[type] = await Rank.list.hbzbLocal.getZbsRankList(call.uid, page, offset);
            //     break;
            case 'slzd1':
            case 'slzd2':
            case 'slzd3':
            case 'slzd4':
            case 'slzd5':
            case 'slzd6':
                obj[type] = await Rank.list[type].getRankList(call.conn.gud.ghId, { min, max });
                break;
            case 'kbzz':
                let resCall = await G.clientCross.callApi('kbzz/Rank', { uid: call.uid, page, offset });
                if (!resCall.isSucc) {
                    return call.error('', { code: -2, message: globalThis.lng.rank_kbzz });
                }
                obj[type] = resCall.res;
                break;
            case 'clslCross':
                let res = await G.clientCross.callApi('clsl/Rank', {
                    uid: call.uid, page: page, offset: offset
                });
                obj[type] = res.res;
                break;
            case 'wzryCross':
                let resCallWzry = await G.clientCross.callApi('wzry/Rank', { gud: call.conn.gud, page, offset });
                obj[type] = resCallWzry.res;
                break;
            case "hbzbLocal":
                //黑帮积分赛排行
                obj[type] = await getToper50RankInfo(call.uid) as any;
                break;
            case 'hbzbCross':
                obj[type] = await getZbsRankList(call.uid) as any;
                break;
            default:
                obj[type] = await Rank.list[type].getRankList(call.uid, { min, max });
                break;
        }
    }
    return obj
}

