import React , { useEffect, useState, useMemo }from 'react';
import { AppRenderer, requireModule } from 'alex';
import { SlotLocation } from './plugins/idePlugin';

import css from './extensions/alex-ext-public.css-language-features-worker.js';
import html from './extensions/alex-ext-public.html-language-features-worker.js';
import json from './extensions/alex-ext-public.json-language-features-worker.js';
import markdown from './extensions/alex-ext-public.markdown-language-features-worker.js';
import typescript from './extensions/alex-ext-public.typescript-language-features-worker.js';
import anycode from './extensions/alex-ext-public.anycode.js';
import anycodeCSharp from './extensions/alex-ext-public.anycode-c-sharp.js';
import anycodeCpp from './extensions/alex-ext-public.anycode-cpp.js';
import anycodeGo from './extensions/alex-ext-public.anycode-go.js';
import anycodeJava from './extensions/alex-ext-public.anycode-java.js';
import anycodePhp from './extensions/alex-ext-public.anycode-php.js';
import anycodePython from './extensions/alex-ext-public.anycode-python.js';
import anycodeRust from './extensions/alex-ext-public.anycode-rust.js';
import anycodeTypescript from './extensions/alex-ext-public.anycode-typescript.js';
import codeRunner from './extensions/alex-ext-public.code-runner-for-web.js';
import emmet from './extensions/alex-ext-public.emmet.js';
import codeswing from './extensions/alex-ext-public.codeswing.js';
import referencesView from './extensions/alex-ext-public.references-view.js';
import gitlens from './extensions/alex-ext-public.gitlens.js';
import graph from './extensions/alex-ext-public.git-graph.js';
import imagePreview from './extensions/alex-ext-public.image-preview.js';
import webSCM from './extensions/alex-ext-public.web-scm.js';
import * as SCMPlugin from './plugins/web-scm.plugin';
import * as AlexApp from './plugins/alex-app.plugin';


import Welcome from './module/Welcome';

import './index.scss';

const CodeServiceModule = requireModule(
  '@alipay/alex-code-service'
).CodeServiceModule;
const CodeAPIModule = requireModule('@alipay/alex-code-api').CodeAPIModule;

const layoutConfig = {
  [SlotLocation.top]: {
    modules: ['@opensumi/ide-menu-bar'],
  },
  [SlotLocation.action]: {
    modules: [''],
  },
  [SlotLocation.left]: {
    modules: [
      '@opensumi/ide-explorer',
      // 暂时移除搜索能力
      // '@opensumi/ide-search',
      '@opensumi/ide-extension-manager',
      '@opensumi/ide-scm',
    ],
  },
  [SlotLocation.main]: {
    modules: ['@opensumi/ide-editor'],
  },
  [SlotLocation.bottom]: {
    modules: ['@opensumi/ide-output', '@opensumi/ide-markers'],
  },
  [SlotLocation.statusBar]: {
    modules: ['@opensumi/ide-status-bar'],
  },
  [SlotLocation.extra]: {
    modules: ['breadcrumb-menu'],
  },
  [SlotLocation.action]: {
    modules: ['@opensumi/ide-toolbar-action'],
  },
};


const platform = 'gitlink';

const WebIDE = ( props) => {
  const { match: { params: { owner, projectsId, branchName } }, mygetHelmetapi } = props;
  let origin=window.location.origin;
  if(origin.indexOf('localhost')>-1){
    origin='https://testforgeplus.trustie.net';
  }

  useEffect(()=>{
    if(mygetHelmetapi && mygetHelmetapi.name){
      document.title= mygetHelmetapi.name
    }
  },[mygetHelmetapi])

  return (<AppRenderer
    appConfig={{
      plugins: [SCMPlugin, AlexApp],
      modules: [
        CodeServiceModule.Config({
          platform: platform,
          owner: owner,
          name: projectsId,
          refPath: 'tree/'+branchName,
          commit: '',
          hash: window.location.hash,
          gitlink: {
            // webpack 代理转发
            // 使用代理本地开发时 因为接口返回头像为地址为相对地址 内部做了拼接 
            // gitlens插件的头像在本地开发会无法展示
            // endpoint: '/code-service',
            // endpoint:''
            origin: origin,
            endpoint: origin
          },
        }),
        CodeAPIModule,
      ],
      extensionMetadata: [
        css,
        html,
        json,
        markdown,
        typescript,
        // TODO 后续增加插件 这些插件暂时还没有在公网发布
        gitlens,
        graph,
        imagePreview,
        webSCM,
        referencesView,
        codeswing,
        emmet,
        anycodeCSharp,
        anycodeCpp,
        anycodeGo,
        anycodeJava,
        anycodePhp,
        anycodePython,
        anycodeRust,
        anycodeTypescript,
        anycode,
        codeRunner,
      ],
      workspaceDir: `${platform}/${owner}/${projectsId}`,
      layoutConfig,
      defaultPreferences: {
        'general.theme': 'opensumi-light',
      },
    }}
    runtimeConfig={{
      biz: 'alex',
      // scm文件树
      scmFileTree: true,
      EditorEmpty: Welcome,
      WelcomePage: Welcome
    }}
  />
)};

export default WebIDE;
