/**
 * 设备信息、能力相关接口
 */
import { callNative, callNativeSync, emptyFn } from './util';
import { is } from './util/commonFn';
import shake from './util/shakeCore';
import * as eventHandlers from './util/eventHandlers';

export default {
    /**
     * 获取网络类型
     *
     * @param {object} options
     * @param {function} options.success 接口调用成功的回调
     * @param {function} options.fail 接口调用失败的回调
     */
    getNetworkType(options = {}) {
        callNative('getNetworkType', options);
    },
    /**
     * 复制
     *
     * @param {Object} options
     * @param {String} options.data 复制的字符串
     * @param {Function} options.success 成功回调
     * @param {Function} options.fail 失败回调
     */
    setClipboardData(options = {}) {
        callNative('setClipboardData', options, { data: 'String' });
    },
    getClipboardData(options = {}) {
        callNative('getClipboardData', options);
    },
    onUserCaptureScreen(callback) {
        const _sys = bl.getSystemInfoSync();
        // 仅安卓
        if (_sys.platform.toLowerCase() === 'android') {
            callNative('startCaptureListening', {});
        }

        if (typeof callback === 'function') {
            global.__biliCacheData.eventHandlers.captureListeningHandlers = [];
            global.__biliCacheData.eventHandlers.captureListeningHandlers.push(callback);
        }
    },
    offUserCaptureScreen(callback) {
        const _sys = bl.getSystemInfoSync();

        if (typeof callback === "function") {
            const index = global.__biliCacheData.eventHandlers.captureListeningHandlers.indexOf(callback);
            if (index >= 0) {
                global.__biliCacheData.eventHandlers.captureListeningHandlers.splice(index, 1);
                // 仅安卓
                if (_sys.platform.toLowerCase() === 'android') {
                    callNative('stopCaptureListening', {});
                }
            }
        } else {
            global.__biliCacheData.eventHandlers.captureListeningHandlers = [];
            // 仅安卓
            if (_sys.platform.toLowerCase() === 'android') {
                callNative('stopCaptureListening', {});
            }
        }
    },
    /**
     * 获取设备电量。
     *
     * @param {Object} options
     * @param {Function} options.success 成功回调
     * @param {Function} options.fail 失败回调
     */
    getBatteryInfo(options = {}) {
        callNative('getBatteryInfo', options);
    },
    /**
     * 获取设备电量。
     */
    getBatteryInfoSync() {
        return callNativeSync('getBatteryInfoSync').data;
    },
    /**
     * 使手机发生较长时间的振动（400 ms)
     *
     * @param {Object} [options = {}]
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    vibrateLong(options = {}) {
        callNative('vibrateLong', options);
    },
    /**
     * 使手机发生较短时间的振动（15 ms）。仅在 iPhone 7 / 7 Plus 以上及 Android 机型生效
     *
     * @param {Object} [options = {}]
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    vibrateShort(options = {}) {
        callNative('vibrateShort', options);
    },
    /**
     * startDeviceMotionListening
     *
     * @param {Object} [options = {}]
     * @param {String} [interval = 'normal'] 监听设备方向的变化回调函数的执行频率
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    startDeviceMotionListening({
        interval = 'normal',
        success = emptyFn,
        fail = emptyFn,
        complete = emptyFn
    } = {}) {
        callNative('startDeviceMotionListening',{ interval, success, fail, complete, pageId: global.__biliCacheData.getCurrentPageId()}, { interval: 'String' });
    },
    /**
     * 停止监听设备方向的变化。
     *
     * @param {Object} [options = {}]
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    stopDeviceMotionListening(options = {}) {
        callNative('stopDeviceMotionListening', Object.assign(options, {
            pageId: global.__biliCacheData.getCurrentPageId()
        }));
    },
    /**
     * 监听设备方向变化事件。
     *
     * @param {Function} callback
     */
    onDeviceMotionChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.deviceMotionChangeHandlers[_pageId]) eventHandlers.deviceMotionChangeHandlers[_pageId] = []
        eventHandlers.deviceMotionChangeHandlers[_pageId].push(callback);
    },
    /**
     * 取消监听设备方向变化事件。
     *
     * @param {Function} callback
     */
    offDeviceMotionChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.deviceMotionChangeHandlers[_pageId]) eventHandlers.deviceMotionChangeHandlers[_pageId] = []
        if (!callback) {
            eventHandlers.deviceMotionChangeHandlers[_pageId] = []
        } else {
            const index = eventHandlers.deviceMotionChangeHandlers[_pageId].indexOf(callback);
            if (index >= 0) {
                eventHandlers.deviceMotionChangeHandlers[_pageId].splice(index, 1);
            }
        }
    },
    /**
     * 开始监听加速度数据。
     *
     * @param {Object} [options = {}]
     * @param {String} [interval = 'normal'] 监听加速度数据回调函数的执行频率
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    startAccelerometer({
        interval = 'normal',
        success = emptyFn,
        fail = emptyFn,
        complete = emptyFn
    } = {}) {
        callNative('startAccelerometer',{ interval, success, fail, complete, pageId: global.__biliCacheData.getCurrentPageId()}, { interval: 'String' });
    },
    /**
     * 停止监听加速度数据。
     *
     * @param {Object} [options = {}]
     */
    stopAccelerometer(options = {}) {
        if (!(is.Object(options))) options = {};
        callNative('stopAccelerometer', Object.assign(options, {
            pageId: global.__biliCacheData.getCurrentPageId()
        }));
    },
    /**
     * 监听加速度数据事件。
     *
     * @param {Function} callback
     */
    onAccelerometerChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.accelerometerChangeHandlers[_pageId]) eventHandlers.accelerometerChangeHandlers[_pageId] = []
        eventHandlers.accelerometerChangeHandlers[_pageId].push(callback);
    },
    /**
     * 取消加速度数据事件。
     *
     * @param {Function} callback
     */
    offAccelerometerChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.accelerometerChangeHandlers[_pageId]) eventHandlers.accelerometerChangeHandlers[_pageId] = []
        if (!callback) {
            eventHandlers.accelerometerChangeHandlers[_pageId] = []
        } else {
            const index = eventHandlers.accelerometerChangeHandlers[_pageId].indexOf(callback);
            if (index >= 0) {
                eventHandlers.accelerometerChangeHandlers[_pageId].splice(index, 1);
            }
        }
    },
    /**
     * 开始监听罗盘数据
     *
     * @param {Object} [options = {}]
     */
    startCompass({
        success = emptyFn,
        fail = emptyFn,
        complete = emptyFn
    } = {}) {
        callNative('startCompass', { success, fail, complete, pageId: global.__biliCacheData.getCurrentPageId()});
    },
    /**
     * 停止监听罗盘数据
     *
     * @param {Object} [options = {}]
     */
    stopCompass(options = {}) {
        if (!(is.Object(options))) options = {};
        callNative('stopCompass', Object.assign(options, {
            pageId: global.__biliCacheData.getCurrentPageId()
        }));
    },
    /**
     * 监听罗盘数据变化事件。频率：5 次/秒，接口调用后会自动开始监听，可使用 wx.stopCompass 停止监听。
     *
     * @param {Function} callback
     */
    onCompassChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.compassChangeHandlers[_pageId]) eventHandlers.compassChangeHandlers[_pageId] = []
        eventHandlers.compassChangeHandlers[_pageId].push(callback);
    },
    /**
     * 取消罗盘数据变化事件。
     *
     * @param {Function} callback
     */
    offCompassChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!eventHandlers.compassChangeHandlers[_pageId]) eventHandlers.compassChangeHandlers[_pageId] = []
        if (!callback) {
            eventHandlers.compassChangeHandlers[_pageId] = []
        } else {
            const index = eventHandlers.compassChangeHandlers[_pageId].indexOf(callback);
            if (index >= 0) {
                eventHandlers.compassChangeHandlers[_pageId].splice(index, 1);
            }
        }
    },
    /**
     * 开始监听陀螺仪数据。
     *
     * @param {Object} [options = {}]
     * @param {String} [interval = 'normal'] 监听陀螺仪数据回调函数的执行频率
     * @param {Function} [success = emptyFn] 成功回调
     * @param {Function} [fail = emptyFn] 失败回调
     */
    startGyroscope({
        interval = 'normal',
        success = emptyFn,
        fail = emptyFn,
        complete = emptyFn
    } = {}) {
        callNative('startGyroscope', { interval, success, fail, complete, pageId: global.__biliCacheData.getCurrentPageId() }, { interval: 'String' });
    },
    /**
     * 停止监听陀螺仪数据。
     *
     * @param {Object} [options = {}]
     */
    stopGyroscope(options = {}) {
        callNative('stopGyroscope', Object.assign(options, {
            pageId: global.__biliCacheData.getCurrentPageId()
        }));
    },
    /**
     * 监听陀螺仪数据变化事件。
     *
     * @param {Function} callback
     */
    onGyroscopeChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId]) global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId] = []
        global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId].push(callback);
    },
    offGyroscopeChange(callback) {
        let _pageId = global.__biliCacheData.getCurrentPageId();
        if (!global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId]) global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId] = []
        if (!callback) {
            global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId] = []
        } else {
            const index = global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId].indexOf(callback);
            if (index >= 0) {
                global.__biliCacheData.eventHandlers.gyroscopeChangeHandlers[_pageId].splice(index, 1);
            }
        }
    },
    /**
     * 监听内存不足告警事件。
     *
     * @param {Function} callback
     */
    onMemoryWarning(callback) {
        eventHandlers.memoryWarningHandlers.push(callback);
    },
    /**
     * 设置屏幕亮度
     *
     * @param {Object} [options = {}]
     */
    setScreenBrightness(options = {}) {
        callNative('setScreenBrightness', options, { value: 'Number' });
    },
    /**
     * 设置是否保持常亮状态。仅在当前小程序生效，离开小程序后设置失效。
     *
     * @param {Object} [options = {}]
     */
    setKeepScreenOn(options = {}) {
        const keepScreenOn = !!options.keepScreenOn;
        callNative('setKeepScreenOn', Object.assign(options, { keepScreenOn }));
    },
    /**
     * 获取屏幕亮度
     *
     * @param {Object} [options = {}]
     */
    getScreenBrightness(options = {}) {
        callNative('getScreenBrightness', options);
    },
    /**
     * 监听窗口尺寸变化事件
     *
     * @param {Function} callback
     */
    onWindowResize(callback) {
        eventHandlers.windowResizeHandlers.push(callback);
    },
    /**
     * 取消监听窗口尺寸变化事件
     *
     * @param {Function} callback
     */
    offWindowResize(callback) {
        const index = eventHandlers.windowResizeHandlers.indexOf(callback);
        if (index >= 0) {
            eventHandlers.windowResizeHandlers.splice(index, 1);
        }
    },
    ...shake
};
