import * as crc from 'crc';
import { md5 } from './sdkUtil';

const BATTLE_CLASS_MOD = 100; // 存档分类模数
const CDN_URL_PREFIX_SQ = 'https://download-sgzzyz.yev242.com'; // sq cdn 服务器地址前缀
const CDN_URL_PREFIX_ZYZ = 'http://zyz-download.trgame.cn'; // 公司下载服务器地址前缀

// 将字符串 crc32 处理后取模，以将随机字符串分组
function modStr(str: string, mod: number) {
    if (typeof(str) !== 'string' || typeof(mod) !== 'number') return undefined;
    return crc.crc32(str) % mod;
}

function getPrefixByEnv(env: string) {
    switch (env) {
        case 'dev':
        case 'monitor':
        case 'alpha':
        case 'stable':
            return CDN_URL_PREFIX_ZYZ;
        case 'sq1':
        case 'sq4':
        case 'sq7':
        case 'sq9':
        default:
            return CDN_URL_PREFIX_SQ;
    }
}

export function getLocalRplUrl(roleId: string, warType: number, battleCode: string) {
    const battleClass = modStr(battleCode, BATTLE_CLASS_MOD); // 将存档文件按一定规则分批保存
    const writePath = `/zyz_logs/rpls/${roleId}/${warType}/${battleClass}`;
    return writePath;
}

export function getRemoteRplUrl(env: string, roleId: string, warType: number, battleCode: string) {
    const battleClass = modStr(battleCode, BATTLE_CLASS_MOD); // 将存档文件按一定规则分批保存
    const rplUrl = `${getPrefixByEnv(env)}/rpls/${md5(env).substring(0, 4)}/${roleId}/${warType}/${battleClass}`;
    return rplUrl;
}

export function getRemoteRplPrefix(env: string) {
    const rplUrl = `${getPrefixByEnv(env)}/rpls/${md5(env).substring(0, 4)}`;
    return rplUrl;
}

export function getRemoteRplFilePath(roleId: string, warType: number, battleCode: string) {
    const battleClass = modStr(battleCode, BATTLE_CLASS_MOD); // 将存档文件按一定规则分批保存
    const rplUrl = `/${roleId}/${warType}/${battleClass}/${battleCode}.bin`;
    return rplUrl;
}
