/**
 * 分享相关接口
 */
import { callNative, callNativeSync, emptyFn, typeCheck } from './util';
import makeShareQuery from '../lib/makeShareQuery';
import compareVersion from '../lib/compareVersion';

const share = ({
    title = '',
    subTitle = '',
    biliContent = '',
    imageUrl = '',
    path = '',
    biliMessageTitle = '',
    success = emptyFn,
    fail = emptyFn,
    complete = emptyFn
} = {}) => {
    const options = {
        title,
        subTitle,
        biliContent,
        path,
        imageUrl,
        biliMessageTitle,
        success,
        fail,
        complete
    };
    const systemInfo = callNativeSync('getSystemInfoSync').data;
    let [baseUrl, query = ''] = options.path
            ? options.path.split('?')
            : Service.location.currentPage.$root.$page.uri.split('?');
    try {
        query = makeShareQuery(query);
    } catch (error) {
        fail({
            errMsg: error.message
        })
        return;
    }
    
    options.path = baseUrl + query;

    callNative('share', options, {
        title: 'String',
        subTitle: 'String',
        biliContent: 'String',
        imageUrl: 'String',
        path: 'String',
        biliMessageTitle: 'String'
    });
};

export default {
    /**
     * 打开分享浮层
     *
     * @param {Object} options
     * @param {String} [options.title = ''] 转发标题，默认需使用当前小程序名称
     * @param {String} [options.subTitle = ''] 子标题或者内容，默认空
     * @param {String} [options.path = ''] 转发路径，默认当前展示页面 path ， /pages/xxx
     * @param {String} [options.imageUrl = ''] 自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。
     * @param {Function} [options.success = emptyFn] 成功回调
     * @param {Function} [options.fail = emptyFn] 失败回调
     */
    share,
    /**
     * 显示当前页面的转发按钮
     *
     * @param {Object} [options={}]
     */
    showShareMenu(options = {}) {
        callNative('showShareMenu', options);
    },
    /**
     * 隐藏转发按钮
     *
     * @param {*} [options={}]
     */
    hideShareMenu(options = {}) {
        callNative('hideShareMenu', options);
    },
    /**
     * 获取转发详细信息
     * 暂时无法使用
     *
     * @param {*} [options={}]
     */
    getShareInfo(options = {}) {
        if (typeof options.fail === 'function') {
            options.fail({ errMsg: 'Cannot use' });
        }
    },
    /**
     * 主动拉起转发
     *
     * @param {Object} [options]
     * @param {String} [title = '']
     * @param {String} [imageUrl = '']
     * @param {String} [query = '?']
     * @param {Function} [success = emptyFn]
     * @param {Function} [fail = emptyFn]
     */
    // 这个api没用到？
    shareAppMessage({
        title = '',
        imageUrl = '',
        biliContent = '',
        path = '',
        success = emptyFn,
        fail = emptyFn
    } = {}) {
        const failFn = typeof fail === 'function' ? fail : emptyFn;
        let result = typeCheck(path, 'String');

        if (result && result !== 'Undefined') {
            failFn({errMsg: `share:fail parameter error: parameter.path should be String|Undefined instead of ${result};`});
        }

        const options = {
            title,
            path,
            imageUrl,
            biliContent,
            success,
            fail,
        };

        callNative('share', options, {
            title: 'String',
            imageUrl: 'String',
            biliContent: 'String'
        });
    }
};
