/**
 * @name 合同模板管理
 */
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { clearHostPrototypeRouteInfo } from '../../common/useHashPage';
import * as antd from 'antd';
import { ConfigProvider } from 'antd';
import {
  type AnnotationDirectoryRouteNode,
  type AnnotationSourceDocument,
  type AnnotationViewerOptions
} from '@axhub/annotation';
import { PrototypeAnnotationHost } from '../../common/prototype-annotation-host';
import 'antd/dist/reset.css';
import '../vehicle-management/style.css';
import './styles/index.css';
import ContractTemplateApp from './ContractTemplate.jsx';
import annotationSourceDocument from './annotation-source.json';

type ContractPageId = 'list' | 'editor';

type ContractNavigationRef = {
  openCreate?: () => void;
  backToList?: () => void;
};

declare global {
  interface Window {
    React: typeof React;
    antd: typeof antd;
  }
}

window.React = React;
window.antd = antd;

/** 与 vehicle-management / Linear 主题一致 */
const vmTheme = {
  token: {
    colorPrimary: '#32a06e',
    colorLink: '#32a06e',
    colorLinkHover: '#3fb87c',
    borderRadius: 8,
    fontFamily:
      'Inter, -apple-system, BlinkMacSystemFont, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif',
    fontSize: 14,
    colorText: '#18181b',
    colorTextSecondary: '#52525b',
    colorBorder: '#e5e7eb',
    colorBgContainer: '#ffffff',
  },
  components: {
    Table: {
      headerBg: '#f6f7f7',
      headerColor: '#71717a',
      rowHoverBg: '#f6f7f7',
      borderColor: '#e5e7eb',
      cellPaddingBlock: 8,
      cellPaddingInline: 12,
    },
    Tabs: {
      inkBarColor: '#32a06e',
      itemActiveColor: '#32a06e',
      itemSelectedColor: '#32a06e',
    },
    Card: {
      borderRadiusLG: 12,
    },
  },
};

export function ContractTemplateManagementContent({ embedded = false }: { embedded?: boolean }) {
  const [annotationPageId, setAnnotationPageId] = useState<ContractPageId>('list');
  const navigationRef = useRef<ContractNavigationRef>({});

  useEffect(() => {
    if (!embedded) {
      clearHostPrototypeRouteInfo();
    }
  }, [embedded]);

  const handleDirectoryRoute = useCallback((node: AnnotationDirectoryRouteNode) => {
    const payload = node.payload as { pageId?: string } | undefined;
    const pageId: ContractPageId = payload?.pageId === 'editor' ? 'editor' : 'list';
    if (pageId === 'editor') {
      navigationRef.current.openCreate?.();
    } else {
      navigationRef.current.backToList?.();
    }
    setAnnotationPageId(pageId);
  }, []);

  const annotationOptions = useMemo<AnnotationViewerOptions>(
    () => ({
      showToolbar: true,
      showThemeToggle: true,
      showColorFilter: true,
      emptyWhenNoData: false,
      toolbarEdge: 'right',
      currentPageId: annotationPageId,
      onDirectoryRoute: handleDirectoryRoute,
    }),
    [annotationPageId, handleDirectoryRoute],
  );

  return (
    <ConfigProvider theme={vmTheme}>
      <ContractTemplateApp
        navigationRef={navigationRef}
        onPageViewChange={setAnnotationPageId}
      />
      <PrototypeAnnotationHost
        source={annotationSourceDocument as AnnotationSourceDocument}
        options={annotationOptions}
      />
    </ConfigProvider>
  );
}

export default function ContractTemplateManagementPage() {
  return <ContractTemplateManagementContent />;
}
