/**
 * 存储相关接口
 */
/* globals GLOBAL_OBJECT */
import { callNative,callNativeSync, getUid, emptyFn, typeCheck, throwError } from './util';

export default {
    /**
     * 获取本地缓存
     *
     * @param {Object} options
     * @param {String} options.key 本地缓存中指定的 key
     * @param {function} options.success 成功回调
     * @param {function} options.fail 失败回调
     */
    getStorage(options = {}) {
        const callbackId = getUid();
        const successFn = typeof options.success === 'function' ? options.success : emptyFn;
        const failFn = typeof options.fail === 'function' ? options.fail : emptyFn;
        const completeFn = typeof options.complete === 'function' ? options.complete : emptyFn;

        const callbackFn = (result) => {
            if (result.code === 0) {
                let resultData;
                try {
                    resultData = JSON.parse(result.data);
                } catch (e) {
                    resultData = null;
                }
                successFn({
                    errMsg: 'getStorage:ok',
                    data: resultData
                });
                completeFn({
                    errMsg: 'getStorage:ok',
                    data: resultData
                });
            } else {
                failFn({
                    errMsg: 'getStorage:fail ' + result.msg
                });
                completeFn({
                    errMsg: 'getStorage:fail ' + result.msg
                });
            }
        };

        const result = typeCheck(options.key, 'String');
        if (result) {
            failFn({errMsg: `getStorage:fail parameter error: parameter.key should be String instead of ${result};`});
            completeFn({errMsg: `getStorage:fail parameter error: parameter.key should be String instead of ${result};`});
            return;
        }

        GLOBAL_OBJECT.callbacks[callbackId] = callbackFn;
        GLOBAL_OBJECT.callNative('getStorage', JSON.stringify(options), callbackId);
    },
    /**
     * 获取本地缓存
     *
     * @param {String} key 键值
     */
    getStorageSync(key) {
        const checkResult = typeCheck(key, 'String');
        if (checkResult) {
            throwError(`getStorageSync:fail parameter error: parameter.key should be String instead of ${checkResult};`);
        }

        const result = callNativeSync('getStorageSync', {key});

        if (result.code === 0) {
            try {
                return JSON.parse(result.data);
            } catch (e) {
                return '';
            }
        }

        return '';

        // throw new Error(result.msg);
    },
    /**
     * 设置本地缓存
     *
     * @param {Object} options
     * @param {String} options.key 本地缓存中指定的 key
     * @param {String | Object} options.data 需要存储的内容
     * @param {function} options.success 成功回调
     * @param {function} options.fail 失败回调
     */
    setStorage(options = {}) {
        callNative('setStorage', Object.assign({}, options, {
            data: JSON.stringify(options.data)
        }), {key: 'String'});
    },

    /**
     * 设置本地缓存
     *
     * @param {String} key 本地缓存中指定的 key
     * @param {String} data 需要存储的内容
     */
    setStorageSync(key, data) {
        callNativeSync('setStorageSync', Object.assign({key}, {
            data: JSON.stringify(data)
        }), {key: 'String'});
    },
    /**
     * 删除本地缓存
     *
     * @param {Object} options
     * @param {String} options.key 本地缓存中指定的 key
     * @param {function} options.success 成功回调
     * @param {function} options.fail 失败回调
     */
    removeStorage(options = {}) {
        callNative('removeStorage', options, {key: 'String'});
    },
    /**
     * 删除本地缓存
     *
     * @param {String} key 本地缓存中指定的 key
     */
    removeStorageSync(key) {
        callNativeSync('removeStorageSync', {key}, {key: 'String'});
    },
    /**
     * 清除所有本地缓存
     *
     * @param {Object} options
     * @param {function} options.success 成功回调
     * @param {function} options.fail 失败回调
     */
    clearStorage(options = {}) {
        callNative('clearStorage', options);
    },
    /**
     * 清除所有本地缓存
     */
    clearStorageSync() {
        callNativeSync('clearStorageSync', '');
    },
    /**
     * 获取当前storage的相关信息
     *
     * @param {Object} options
     * @param {Function} options.success 成功回调
     * @param {Function} options.fail 失败回调
     */
    getStorageInfo(options = {}) {
        callNative('getStorageInfo', options);
    },
    /**
     * 清除所有本地缓存
     */
    getStorageInfoSync() {
        return callNativeSync('getStorageInfoSync', '').data;
    },
};
