import compareVersion from './compareVersion';

const supportJSON = {
    applet: {
        "1.5.2": [
            "fs.access",
            "fs.accessSync",
            "fs.appendFile",
            "fs.appendFileSync",
            "fs.copyFile",
            "fs.copyFileSync",
            "fs.getFileInfo",
            "fs.getSavedFileList",
            "fs.mkdir",
            "fs.mkdirSync",
            "fs.readdir",
            "fs.readdirSync",
            "fs.readFile",
            "fs.readFileSync",
            "fs.removeSavedFile",
            "fs.rename",
            "fs.renameSync",
            "fs.rmdir",
            "fs.rmdirSync",
            "fs.saveFile",
            "fs.saveFileSync",
            "fs.stat",
            "fs.statSync",
            "fs.unlink",
            "fs.unlinkSync",
            "fs.unzip",
            "fs.writeFile",
            "fs.writeFileSync",
            "internal.isBiliLogin",
            "internal.login",
            "internal.getBiliUserInfo",
            "internal.uploadImage",
            "internal.reportNeuron",
            "internal.reportCommon",
            "internal.share",
            "internal.openSchema",
            "internal.navigateToSmallApp"
        ],
        "3.2.0": [
            "connectSocket",
            "SocketTask.close",
            "SocketTask.onClose",
            "SocketTask.onError",
            "SocketTask.onMessage",
            "SocketTask.send",
            "internal.configNavigationRightButton",
            "internal.enableNavBackHook",
            "internal.enableSideSlide"
        ],
        "1.5.2-Android": [
            "audio.destroy",
            "audio.getAutoplay",
            "audio.getBuffered",
            "audio.getCurrentTime",
            "audio.getDuration",
            "audio.getLoop",
            "audio.getObeyMuteSwitch",
            "audio.getPaused",
            "audio.getSrc",
            "audio.getStartTime",
            "audio.getVolume",
            "audio.pause",
            "audio.play",
            "audio.seek",
            "audio.setAutoplay",
            "audio.setDataBuffer",
            "audio.setLoop",
            "audio.setObeyMuteSwitch",
            "audio.setSrc",
            "audio.setStartTime",
            "audio.setVolume",
            "audio.stop"
        ],
        "3.8.0-iOS": [
            "audio.destroy",
            "audio.getAutoplay",
            "audio.getBuffered",
            "audio.getCurrentTime",
            "audio.getDuration",
            "audio.getLoop",
            "audio.getObeyMuteSwitch",
            "audio.getPaused",
            "audio.getSrc",
            "audio.getStartTime",
            "audio.getVolume",
            "audio.pause",
            "audio.play",
            "audio.seek",
            "audio.setAutoplay",
            "audio.setDataBuffer",
            "audio.setLoop",
            "audio.setObeyMuteSwitch",
            "audio.setSrc",
            "audio.setStartTime",
            "audio.setVolume",
            "audio.stop"
        ],
        "2.0.0": [
            "internal.addPullDownRefresh",
            "internal.enableScroll"
        ],
        "2.0.0-iOS": [
            "internal.openAppSettings",
            "internal.enableBounce"
        ],
        "2.1.0-Android": [
            "internal.openAppSettings"
        ],
        "2.1.0-iOS": [
            "internal.isEnableBounce",
            "internal.isEnablePullDownRefresh",
            "internal.isEnableScroll"
        ],
        "3.1.0": [
            "internal.mall.callNative",
            "internal.enableModule.mall",
            "internal.rotateScreen"
        ],
        "3.0.0": [
            "internal.switchTo"
        ],
        "2.9.0": [
            "internal.showModal"
        ],
        "3.4.0": [
            "internal.updatePackage",
            "internal.reboot"
        ],
        "2.6.0": [
            "internal.openThirdApp",
            "updater.applyUpdate"
        ],
        "2.3.0": [
            "internal.showNavigationBackButton",
            "internal.showNavigationRightButton",
            "internal.hideNavigationBackButton",
            "internal.hideNavigationRightButton"
        ]
    }
}

/**
 * 处理mapping json映射，将api映射成对象
 *  每个数组对象中存在该api的版本号，以及支持平台
 * */
let allApi = {}
for (let key in supportJSON.applet) {
    // 获取版本号 x.x.x
    let version = /^((\d+)\.(\d+)\.(\d+)).*/.exec(key)[1]
    // 获取平台 如果没有匹配到就是所有平台都支持-all
    let platform = /-(.+)$/.exec(key) ? /-(.+)$/.exec(key)[1] : 'all'
    supportJSON.applet[key].forEach(x => {
        let content = {
            version,
            platform
        }
        if (allApi[x]) {
            allApi[x].push(content)
        } else {
            allApi[x] = [content]
        }
    })
}

const mapping = {
    'FileSystemManager': 'fs',
    'InnerAudioContext': 'audio',
    'UpdateManager': 'updater'
}

/**
 * 对外api转换为内部api
 * */
function convertMapping(api) {
    let temp = api
    for (let key in mapping) {
        let reg = new RegExp(key)
        if (reg.test(temp)) {
            temp = temp.replace(reg, mapping[key])
        }
    }
    return temp
}

/**
 * 判断属性是否支持
 *  1. 判断是否在json支持里
 *  2. 判断版本号是否支持
 *  3. 判断是否独立平台
 * */
function isSupport(api, version, platform) {
    let reallyValue = convertMapping(api)
    let output = {
        // 是否透传 优先级1
        isThrough: false,
        // 在json列表中是否支持 优先级2
        result: false
    }
    // 是否在列表中
    if (allApi[reallyValue]) {
        // 检测api版本和平台支持
        let isContinue = true
        let num = 0
        while (isContinue) {
            if (compareVersion(version, allApi[reallyValue][num].version) >= 0) {
                // 检测是否全平台支持，或者
                if (allApi[reallyValue][num].platform === 'all' || allApi[reallyValue][num].platform.toUpperCase() === platform.toUpperCase()) {
                    output.result = true
                    // 检测到支持，断开循环
                    isContinue = false
                }
            } else {
                output.result = false
                isContinue = false
            }
            // 判断不要超过数组的大小
            if (allApi[reallyValue].length - 1 === num) {
                // 超过数组大小，停止
                isContinue = false
            } else {
                // 没有，继续
                num++
            }
        }
    } else {
        // 不在列表，透传NA
        output.isThrough = true
    }
    return output
}

export default {
    isSupport
}
