import 'kit_logger';

import {
  addGlobalUncaughtErrorHandler,
  initGlobalState,
  registerMicroApps,
  runAfterFirstMounted,
  setDefaultMountApp,
  start,
} from 'qiankun';

import micorConfig, { genActiveRule } from '../config/micor';
import pkg from '../package.json';
import { actionCreators } from './containers/App/store';
import { FILTER_FOLER_REG } from './utils/constants';

// eslint-disable-next-line no-undef
const Logger = logger('micro');
const MICRO_STATUS = {
    NOT_LOADED: 'NOT_LOADED',
    LOADING_SOURCE_CODE: 'LOADING_SOURCE_CODE',
    NOT_BOOTSTRAPPED: 'NOT_BOOTSTRAPPED',
    BOOTSTRAPPING: 'BOOTSTRAPPING',
    NOT_MOUNTED: 'NOT_MOUNTED',
    MOUNTING: 'MOUNTING',
    MOUNTED: 'MOUNTED',
    UPDATING: 'UPDATING',
    UNMOUNTING: 'UNMOUNTING',
    UNLOADING: 'UNLOADING',
    SKIP_BECAUSE_BROKEN: 'SKIP_BECAUSE_BROKEN',
    LOAD_ERROR: 'LOAD_ERROR',
};

export const initMicroApps = (loader, store) => {
    /* eslint-disable */

    const config = createStoreage.get('globalConfig');
    const application = config.products || [];
    let products = [];
    application && Array.isArray(application) && application.length > 0 && application.map(item => {
        products.push({
            name: item.PackageName,
            entry: "//" + window.location.host + (`/${item.PackageName}`),
            container: '#micro-container',
            activeRule: genActiveRule(`/civbase/${item.PackageName}`),
            props: item.DefaultSetting
        })
    });
    const entrys =
        process.env.NODE_ENV !== 'production' ?
        micorConfig.dev :
        products;

    registerMicroApps(
        entrys.map(item => {
            item.loader = loader;
            item.props = {
                emitter: window.share.event,
                baseRoot: item.name,
                // eslint-
                // disable-next-line no-undef
                globalConfig: createStoreage.get('globalConfig'),
                XMLHttpRequest: window.XMLHttpRequest,
            };
            return item;
        }), {
            beforeLoad: [
                app => {
                    store.dispatch(actionCreators.updateMicroMounted(true));
                    Logger.info('[LifeCycle] before load %c%s');
                },
            ],
            beforeMount: [
                app => {
                    Logger.info(`[LifeCycle] before mount %c%s ${app.name}`);
                },
            ],
            afterMount: [
                app => {
                    store.dispatch(actionCreators.updateMicroMounted(false));
                    Logger.info(`[LifeCycle] after mount %c%s ${app.name}`);
                },
            ],
            beforeUnmount: [
                app => {
                    window.share.event.removeAllListeners('changeRoute');
                    Logger.info(`[LifeCycle] after unmount %c%s ${app.name}`);
                },
            ],
            afterUnmount: [app => {}],
        },
    );
    const globalStore = store.getState().toJS();
    const { setGlobalState } = initGlobalState({
        // eslint-disable-next-line no-undef
        globalConfig: createStoreage.get('globalConfig'),
        global: globalStore.global
    });
    setGlobalState({
        // eslint-disable-next-line no-undef
        globalConfig: createStoreage.get('globalConfig'),
    });
    start({
        sandbox: {
            experimentalStyleIsolation: false,
            loose: true,
        },
        singular: true,
        scopedCSS: true,
        prefetch: 'all',
        // eslint-disable-next-line no-underscore-dangle
        // getPublicPath: window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
        excludeAssetFilter: url =>
            url.indexOf('framework/highcharts') !== -1 ||
            url.indexOf('framework/amap/AMap.UI') !== -1 ||
            url.indexOf('framework/amap/init.js') !== -1 ||
            url.indexOf('configuration/js') !== -1 ||
            url.indexOf('framework/three.js') !== -1 ||
            url.indexOf('threedimensional/frameworkthree') !== -1 ||
            url.indexOf('iframe/Civ3DLLab/js') !== -1 ||
            url.indexOf('framework/jquery/gridify-min.js') !== -1 ||
            url.indexOf('framework/jquery/gridify.qrcode.js') !== -1 ||
            url.indexOf('framework/application/Platform.js') !== -1 ||
            url.indexOf('echarts') !== -1 ||
            url.indexOf('lbs.amap.com') !== -1 ||
            url.indexOf('restapi.amap.com') !== -1 ||
            url.indexOf('webapi.amap.com') !== -1 ||
            url.indexOf('webapi.amap.com/count') !== -1 ||
            url.indexOf('restapi.amap.com/v3') !== -1 ||
            url.indexOf('api.map.baidu.com') !== -1 ||
            url.indexOf('map.baidu.com') !== -1 ||
            url.indexOf('pv.sohu.com') !== -1 ||
            url.indexOf('mt0.google.cn') !== -1 ||
            url.indexOf('mt1.google.cn') !== -1 ||
            url.indexOf('mt2.google.cn') !== -1 ||
            url.indexOf('mt3.google.cn') !== -1 ||
            url.indexOf('hm.baidu.com') !== -1 ||
            url.indexOf('framework/antv/antvL7.min.js') !== -1 ||
            url.indexOf('https://maponline0.bdimg.com') !== -1 ||
            url.indexOf('https://maponline1.bdimg.com') !== -1 ||
            url.indexOf('https://maponline2.bdimg.com') !== -1 ||
            url.indexOf('https://maponline3.bdimg.com') !== -1 ||
            url.indexOf('https://api.map.baidu.com/getscript') !== -1,
    });
    runAfterFirstMounted(() => {
        Logger.info('[MainApp] first app mounted');
    });
    // eslint-disable-next-line no-use-before-define
    defaultApp();
    addGlobalUncaughtErrorHandler(event => {

        const { error } = event;
        console.log(event);
        const reson = 'Failed to fetch';
        if (
            error &&
            error.message.indexOf(MICRO_STATUS.LOADING_SOURCE_CODE) &&
            error.message.indexOf(reson)
        ) {
            // window.history.pushState({message: '应用服务请求异常，请检查应用配置'}, null, '/civbase/500')
        }
    });
};

export const defaultApp = () => {
    // eslint-disable-next-line no-undef
    const config = createStoreage.get('globalConfig');
    if (config && config.token) {

        // const startWith = config.homepage ? config.homepage.split('/') : [];
        // console.log(findPathByLeafId(config.homepage,  '', 'url'))
        const basePath =
            config.homepage !== '' && FILTER_FOLER_REG.test(config.homepage) ?
            'civweb4' :
            'civweb4';
        const defaultURL =
            config.homepage === '' ?
            `/${pkg.name.toLocaleLowerCase()}/${basePath}?client=${config.client}` :
            `/${pkg.name.toLocaleLowerCase()}/${basePath}/${config.homepage}`;
        setDefaultMountApp(defaultURL);
    }
};

/**
 * window.app主要功能：
   define 定义项目的公共库，主要用来解决JS公共库的管理问题
   require 引用自己的定义的基础库，配合define来使用
   routes 用于存放全局的路由，子项目路由添加到window.app.routes，用于完成路由的注册
   init 注册入口，为子项目添加上namesapce标识，注册上子项目管理数据流的reducers
 */

window.app = {
    modules: {},
    require: function(request) {
        return this.modules[request]();
    },
    define: function(name, context, index) {
        let keys = context.keys();
        for (let key of keys) {
            let parts = name.indexOf('@') > -1 ? [name, ...(key.slice(1).split('/'))] : (name + key.slice(1)).split('/');
            parts = parts.filter(item => item !== "");
            let dir = this.modules;
            for (let i = 0; i < parts.length - 1; i++) {
                let part = parts[i];
                if (!dir.hasOwnProperty(part)) {
                    dir[part] = {};
                }
                dir = dir[part];
            }
            dir[parts[parts.length - 1]] = context.bind(context, key);
        }
        if (index && index !== null) {
            this.modules[name] = this.modules[name][index];
        }
    },
    init: function(namespace, reducers) {
        [namespace] = reducers;
    }
};
// window.app.define('city-data', require.context('./components/CitySelector', true, /^.\/(CitySelector\/)?[^\/]+\.js$/), 'city-data.js')
window.app.define('react', require.context('react', true, /^.\/index\.js$/), 'index.js');
window.app.define('react-dom', require.context('react-dom', true, /^.\/index\.js$/), 'index.js');
window.app.define('react-router', require.context('react-router', true, /^.\/index\.js$/), 'index.js');
window.app.define('connected-react-router', require.context('../node_modules/connected-react-router/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('react-helmet', require.context('../node_modules/react-helmet/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'Helmet.js');
window.app.define('qrcode.react', require.context('../node_modules/qrcode.react/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('qr.js', require.context('qr.js', true, /^.\/index\.js$/), 'index.js');
window.app.define('query-string', require.context('query-string', true, /^.\/index\.js$/), 'index.js');
window.app.define('react-helmet-async', require.context('../node_modules/react-helmet-async/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('immutable', require.context('../node_modules/immutable/dist', true, /^.\/(dist\/)?[^\/]+\.js$/), 'immutable.min.js');
window.app.define('redux-immutable', require.context('../node_modules/redux-immutable/dist', true, /^.\/(dist\/)?[^\/]+\.js$/), 'index.js');
window.app.define('redux-logger', require.context('../node_modules/redux-logger/dist', true, /^.\/(dist\/)?[^\/]+\.js$/), 'redux-logger.js');

window.app.define('react-router-dom', require.context('react-router-dom', true, /^.\/index\.js$/), 'index.js');
window.app.define('react-redux', require.context('../node_modules/react-redux/dist', true, /^.\/(dist\/)?[^\/]+\.js$/), 'react-redux.min.js');
window.app.define('redux', require.context('../node_modules/redux/dist', true, /^.\/(dist\/)?[^\/]+\.js$/), 'redux.min.js');
window.app.define('mqtt-client', require.context('../node_modules/mqtt-client/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('kit_logger', require.context('../node_modules/kit_logger/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('kit_utils', require.context('../node_modules/kit_utils/lib', true, /^.\/(lib\/)?[^\/]+\.js$/));
window.app.define('kit_global_config', require.context('../node_modules/kit_global_config/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@wisdom-utils/utils', require.context('../node_modules/@wisdom-utils/utils/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@wisdom-utils/runtime', require.context('../node_modules/@wisdom-utils/runtime/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@wisdom-utils/components', require.context('../node_modules/@wisdom-utils/components/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('classnames', require.context('classnames', true, /^.\/index\.js$/), 'index.js');
window.app.define('@ant-design/icons', require.context('../node_modules/@ant-design/icons/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@ant-design/pro-utils', require.context('../node_modules/@ant-design/pro-utils/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@ant-design/pro-layout', require.context('../node_modules/@ant-design/pro-layout/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@ant-design/pro-table', require.context('../node_modules/@ant-design/pro-table/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@ant-design/pro-provider', require.context('../node_modules/@ant-design/pro-provider/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('pinyin-match', require.context('../node_modules/pinyin-match/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'main.js');
window.app.define('react-intl', require.context('../node_modules/react-intl/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('moment', require.context('moment', true, /^.\/moment\.js$/), 'moment.js');
window.app.define('rc-align', require.context('../node_modules/rc-align/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-cascader', require.context('../node_modules/rc-cascader/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-checkbox', require.context('../node_modules/rc-checkbox/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-collapse', require.context('../node_modules/rc-collapse/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js')
window.app.define('rc-dialog', require.context('../node_modules/rc-dialog/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-drawer', require.context('../node_modules/rc-drawer/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-dropdown', require.context('../node_modules/rc-dropdown/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-field-form', require.context('../node_modules/rc-field-form/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-image', require.context('../node_modules/rc-image/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-input-number', require.context('../node_modules/rc-input-number/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-mentions', require.context('../node_modules/rc-mentions/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-menu', require.context('../node_modules/rc-menu/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-motion', require.context('../node_modules/rc-motion/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-notification', require.context('../node_modules/rc-notification/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-overflow', require.context('../node_modules/rc-overflow/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-pagination', require.context('../node_modules/rc-pagination/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-picker', require.context('../node_modules/rc-picker/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-progress', require.context('../node_modules/rc-progress/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-rate', require.context('../node_modules/rc-rate/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-resize-observer', require.context('../node_modules/rc-resize-observer/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-select', require.context('../node_modules/rc-select/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-slider', require.context('../node_modules/rc-slider/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-steps', require.context('../node_modules/rc-steps/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-switch', require.context('../node_modules/rc-switch/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-table', require.context('../node_modules/rc-table/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-tabs', require.context('../node_modules/rc-tabs/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-textarea', require.context('../node_modules/rc-textarea/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-tooltip', require.context('../node_modules/rc-tooltip/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-tree', require.context('../node_modules/rc-tree/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-tree-select', require.context('../node_modules/rc-tree-select/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-trigger', require.context('../node_modules/rc-trigger/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-upload', require.context('../node_modules/rc-upload/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('rc-virtual-list', require.context('../node_modules/rc-virtual-list/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
