import type * as Monaco from 'monaco-editor'

const TOKEN_RULES: Monaco.editor.ITokenThemeRule[] = [
  { token: '', foreground: 'D4D4D4' },
  { token: 'comment', foreground: '6A9955', fontStyle: 'italic' },
  { token: 'comment.line', foreground: '6A9955', fontStyle: 'italic' },
  { token: 'comment.block', foreground: '6A9955', fontStyle: 'italic' },
  { token: 'keyword', foreground: '569CD6', fontStyle: 'bold' },
  { token: 'keyword.control', foreground: 'C586C0' },
  { token: 'string', foreground: 'CE9178' },
  { token: 'string.escape', foreground: 'D7BA7D' },
  { token: 'number', foreground: 'B5CEA8' },
  { token: 'regexp', foreground: 'D16969' },
  { token: 'type', foreground: '4EC9B0' },
  { token: 'type.identifier', foreground: '4EC9B0' },
  { token: 'class', foreground: '4EC9B0' },
  { token: 'interface', foreground: '4EC9B0' },
  { token: 'function', foreground: 'DCDCAA' },
  { token: 'method', foreground: 'DCDCAA' },
  { token: 'variable', foreground: '9CDCFE' },
  { token: 'variable.predefined', foreground: '4FC1FF' },
  { token: 'constant', foreground: '4FC1FF' },
  { token: 'tag', foreground: '569CD6' },
  { token: 'attribute.name', foreground: '9CDCFE' },
  { token: 'attribute.value', foreground: 'CE9178' },
  { token: 'delimiter', foreground: 'D4D4D4' },
  { token: 'operator', foreground: 'D4D4D4' },
  { token: 'metatag', foreground: '569CD6' },
  { token: 'metatag.content.html', foreground: 'CE9178' },
  { token: 'key', foreground: '9CDCFE' },
  { token: 'string.key', foreground: '9CDCFE' },
  { token: 'string.value', foreground: 'CE9178' },
]

const LIGHT_TOKEN_RULES: Monaco.editor.ITokenThemeRule[] = [
  { token: '', foreground: '24292E' },
  { token: 'comment', foreground: '6A737D', fontStyle: 'italic' },
  { token: 'keyword', foreground: 'D73A49', fontStyle: 'bold' },
  { token: 'string', foreground: '032F62' },
  { token: 'number', foreground: '005CC5' },
  { token: 'type', foreground: '22863A' },
  { token: 'function', foreground: '6F42C1' },
  { token: 'variable', foreground: '24292E' },
  { token: 'tag', foreground: '22863A' },
  { token: 'attribute.name', foreground: '6F42C1' },
  { token: 'attribute.value', foreground: '032F62' },
]

export function registerNexusThemes(monaco: typeof Monaco): void {
  monaco.editor.defineTheme('nexus-dark', {
    base: 'vs-dark',
    inherit: true,
    rules: TOKEN_RULES,
    colors: {
      'editor.background': '#1E1E1E',
      'editor.foreground': '#D4D4D4',
      'editorLineNumber.foreground': '#858585',
      'editorLineNumber.activeForeground': '#C6C6C6',
      'editor.selectionBackground': '#264F78',
      'editor.inactiveSelectionBackground': '#3A3D41',
      'editor.lineHighlightBackground': '#2A2D2E',
      'editorCursor.foreground': '#AEAFAD',
      'editorWhitespace.foreground': '#3B3B3B',
      'editorIndentGuide.background': '#404040',
      'editorIndentGuide.activeBackground': '#707070',
      'editorBracketMatch.background': '#0064001a',
      'editorBracketMatch.border': '#888888',
    },
  })

  monaco.editor.defineTheme('nexus-light', {
    base: 'vs',
    inherit: true,
    rules: LIGHT_TOKEN_RULES,
    colors: {
      'editor.background': '#FFFFFF',
      'editor.foreground': '#24292E',
      'editorLineNumber.foreground': '#959DA5',
      'editorLineNumber.activeForeground': '#24292E',
      'editor.selectionBackground': '#C8E1FF',
      'editor.lineHighlightBackground': '#F6F8FA',
      'editorCursor.foreground': '#24292E',
      'editorIndentGuide.background': '#EFF2F5',
      'editorIndentGuide.activeBackground': '#D0D7DE',
    },
  })
}

/** 编辑器固定深色主题（避免浅色底与 Vuetify 卡片冲突导致看不清） */
export function nexusEditorThemeId(): 'nexus-dark' {
  return 'nexus-dark'
}

export function applyNexusEditorTheme(monaco: typeof Monaco): void {
  monaco.editor.setTheme(nexusEditorThemeId())
}

export function toggleNexusEditorTheme(monaco: typeof Monaco): 'dark' {
  localStorage.setItem('nexus_editor_theme', 'dark')
  applyNexusEditorTheme(monaco)
  return 'dark'
}
