/**
 * @packageDocumentation
 * @module jldbq-extension
 */

import {
  JupyterFrontEnd,
  JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { MainAreaWidget, ReactWidget } from '@jupyterlab/apputils';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import React from 'react';
import DataViewWidget from './DataViewWidget';
import DataTableWidget from './DataTableWidget';
import TaskViewWidget from './TaskViewWidget';
import MonitorViewWidget from './MonitorViewWidget';
import { cliIcon, monitorIcon, timerIcon } from './icons';
import config from './api/config';
import { SyncWizard } from './wizard';

const PLUGIN_ID = '@jupyterlab/jldbq-extension:plugin';

const plugin: JupyterFrontEndPlugin<void> = {
  id: PLUGIN_ID,
  autoStart: true,
  requires: [ISettingRegistry],
  activate: async (app: JupyterFrontEnd, registry: ISettingRegistry) => {
    registry.pluginChanged.connect(async (_sender, plugin) => {
      if (plugin === PLUGIN_ID) {
        await updateConfig(registry);
      }
    });
    await updateConfig(registry);

    const manager = 'test1'; // TODO: 读取当前用户

    const dataViewWidget = new DataViewWidget(manager);
    dataViewWidget.id = 'jldbq-data';
    dataViewWidget.title.icon = cliIcon;
    dataViewWidget.title.caption = '数据开发';
    dataViewWidget.title.label = '数据开发';
    dataViewWidget.tableOpened.connect(
      (_sender, { datasource, tableName, action }) => {
        const content = new DataTableWidget(datasource, tableName, action);
        const widget = new MainAreaWidget({ content });
        widget.title.label = tableName;
        widget.title.icon = cliIcon;
        app.shell.add(widget, 'main');
      }
    );
    dataViewWidget.wizardOpened.connect(() => {
      const widget = new MainAreaWidget({
        content: ReactWidget.create(
          <SyncWizard
            onFinish={data => {
              // TODO:
              alert(JSON.stringify(data, null, 2));
              console.log(data);
            }}
          />
        )
      });
      widget.title.label = '数据源同步';
      widget.title.icon = cliIcon;
      app.shell.add(widget, 'main');
    });
    // app.shell.add(dataViewWidget, 'left', { rank: 100 });

    const taskViewWidget = new TaskViewWidget();
    taskViewWidget.id = 'jldbq-sync';
    taskViewWidget.title.icon = timerIcon;
    taskViewWidget.title.caption = '定时任务';
    taskViewWidget.title.label = '定时任务';
    // app.shell.add(taskViewWidget, 'left', { rank: 1000 });

    const monitorViewWidget = new MonitorViewWidget();
    monitorViewWidget.id = 'jldbq-monitor';
    monitorViewWidget.title.icon = monitorIcon;
    monitorViewWidget.title.caption = '调度监控';
    monitorViewWidget.title.label = '调度监控';
    // app.shell.add(monitorViewWidget, 'left', { rank: 1100 });
  }
};

export default plugin;

async function updateConfig(registry: ISettingRegistry) {
  const settings = await registry.load(PLUGIN_ID);
  const endpoint = settings.composite['flaskBackend'] as string;
  config.endpoint = endpoint;
}
