// p0 functions
/* global Service */
/* eslint-disable no-unused-vars */
import nativeApi from './nativeApi';
import { showSystemLog } from './nativeApi/util';
import * as eventHandlers from './nativeApi/util/eventHandlers';
import Network from './nativeApi/network';

showSystemLog && console.log('bl app');

Service.callbacks = {};
Service.invokeCallback = (result, callbackId) => {
    if (!callbackId) {
        return;
    }
    
    showSystemLog && console.log(result, callbackId);
    const callbackFn = Service.callbacks[callbackId];
    if (callbackFn) {
        callbackFn(result);
        delete Service.callbacks[callbackId];
    }
};
window.__SmallApp.handleMessage = (message = {}) => {
    const { path, query } = window.app.$route;

    if (message.type === 'lifecycle') {
        switch(message.event) {
        case 'onShow':
            window.__appInstance.onShow && window.__appInstance.onShow({
                path,
                query,
                refererInfo: query.refererInfo || {}
            });
            break;
        case 'onHide':
            window.__appInstance.onHide && window.__appInstance.onHide();
            break;
        }
    } else if (message.type === 'download') {
        if (message.event === 'success') {
            Network.downloadFileCallBacks[message.id][message.event](Object.assign({
                errMsg: 'downloadFile:ok'
            }, message.data));
        } else if (message.event === 'fail') {
            Network.downloadFileCallBacks[message.id][message.event]({
                errMsg: message.data.msg
            });
        } else if (message.event === 'onProgressUpdate') {
            Network.downloadFileCallBacks[message.id].onProgressUpdateCallbacks.forEach(callback => {
                callback(message.data);
            });
        }
    } else if (message.type === 'upload') {
        const handlers = eventHandlers.uploadFileHandlers[message.id];
        
        if (message.event === 'success') {
            handlers[message.event](Object.assign({
                errMsg: 'uploadFile:ok'
            }, message.data));
            handlers.complete(Object.assign({
                errMsg: 'uploadFile:ok'
            }, message.data));
        } else if (message.event === 'fail') {
            handlers[message.event]({
                errMsg: 'uploadFile:fail ' + message.data.msg
            });
            handlers.complete({
                errMsg: 'uploadFile:fail ' + message.data.msg
            });
        } else {
            handlers[message.event + 'Callbacks'].forEach(callback => {
                callback(message.data);
            });
        }
    } else if (message.type === 'updater') {
        eventHandlers.updateManagerHandlers[message.event + 'Param'] = message.data;
        eventHandlers.updateManagerHandlers[message.event + 'Callbacks'].forEach(callback => {
            callback(message.data);
        });
    } 
};
window.__SmallApp.handleMessageWithReturn = (isString, message) => {
    let result;
    if (message.type === 'system') {
        switch (message.event) {
        case 'onShareAppMessage': {
            result = {};
            break;
        }
        }
    }
    return isString ? JSON.stringify(result) : result;
};

const bl = nativeApi;


export default bl;
