'use client'

import { Button } from '@langgenius/dify-ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '@langgenius/dify-ui/dropdown-menu'
import { RiAddLine, RiArrowDownSLine } from '@remixicon/react'
import { useSuspenseQuery } from '@tanstack/react-query'
import { noop } from 'es-toolkit/function'
import { useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { FileZip } from '@/app/components/base/icons/src/vender/solid/files'
import { Github } from '@/app/components/base/icons/src/vender/solid/general'
import { MagicBox } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
import InstallFromGitHub from '@/app/components/plugins/install-plugin/install-from-github'
import InstallFromLocalPackage from '@/app/components/plugins/install-plugin/install-from-local-package'
import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
import { systemFeaturesQueryOptions } from '@/service/system-features'

type Props = {
  onSwitchToMarketplaceTab: () => void
}

type InstallMethod = {
  icon: React.FC<{ className?: string }>
  text: string
  action: string
}

const InstallPluginDropdown = ({
  onSwitchToMarketplaceTab,
}: Props) => {
  const { t } = useTranslation()
  const fileInputRef = useRef<HTMLInputElement>(null)
  const [selectedAction, setSelectedAction] = useState<string | null>(null)
  const [selectedFile, setSelectedFile] = useState<File | null>(null)
  const { data: enable_marketplace } = useSuspenseQuery({
    ...systemFeaturesQueryOptions(),
    select: s => s.enable_marketplace,
  })
  const { data: plugin_installation_permission } = useSuspenseQuery({
    ...systemFeaturesQueryOptions(),
    select: s => s.plugin_installation_permission,
  })

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0] ?? null
    event.target.value = ''
    if (file) {
      setSelectedFile(file)
      setSelectedAction('local')
    }
  }

  const handleCloseLocalInstaller = () => {
    setSelectedAction(null)
    setSelectedFile(null)
    if (fileInputRef.current)
      fileInputRef.current.value = ''
  }

  // TODO TEST INSTALL : uninstall
  // const [pluginLists, setPluginLists] = useState<any>([])
  // useEffect(() => {
  //   (async () => {
  //     const list: any = await get('workspaces/current/plugin/list')
  //   })()
  // })

  // const handleUninstall = async (id: string) => {
  //   const res = await post('workspaces/current/plugin/uninstall', { body: { plugin_installation_id: id } })
  //   console.log(res)
  // }

  const installMethods = useMemo<InstallMethod[]>(() => {
    const methods: InstallMethod[] = []
    if (enable_marketplace)
      methods.push({ icon: MagicBox, text: t('source.marketplace', { ns: 'plugin' }), action: 'marketplace' })

    if (plugin_installation_permission.restrict_to_marketplace_only)
      return methods

    methods.push({ icon: Github, text: t('source.github', { ns: 'plugin' }), action: 'github' })
    methods.push({ icon: FileZip, text: t('source.local', { ns: 'plugin' }), action: 'local' })
    return methods
  }, [plugin_installation_permission, enable_marketplace, t])

  const handleInstallMethodSelect = (action: string) => {
    if (action === 'local') {
      fileInputRef.current?.click()
      return
    }

    if (action === 'marketplace') {
      onSwitchToMarketplaceTab()
      return
    }

    queueMicrotask(() => {
      setSelectedAction(action)
    })
  }

  return (
    <DropdownMenu modal={false}>
      <div className="relative">
        <input
          type="file"
          ref={fileInputRef}
          style={{ display: 'none' }}
          onChange={handleFileChange}
          accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
        />
        <DropdownMenuTrigger
          render={(
            <Button
              className="size-full p-2 text-components-button-secondary-text data-popup-open:bg-state-base-hover"
            />
          )}
        >
          <>
            <RiAddLine className="size-4" />
            <span className="pl-1">{t('installPlugin', { ns: 'plugin' })}</span>
            <RiArrowDownSLine className="ml-1 size-4" />
          </>
        </DropdownMenuTrigger>
        <DropdownMenuContent
          placement="bottom-start"
          sideOffset={4}
          popupClassName="w-[200px] pb-2"
        >
          <span className="flex items-start self-stretch px-3 pt-1 pb-0.5 system-xs-medium-uppercase text-text-tertiary">
            {t('installFrom', { ns: 'plugin' })}
          </span>
          {installMethods.map(({ icon: Icon, text, action }) => (
            <DropdownMenuItem
              key={action}
              className="gap-1 px-2"
              onClick={() => handleInstallMethodSelect(action)}
            >
              <div className="flex items-center gap-1">
                <Icon className="size-4 text-text-tertiary" />
                <span className="px-1 system-md-regular text-text-secondary">{text}</span>
              </div>
            </DropdownMenuItem>
          ))}
        </DropdownMenuContent>
      </div>
      {selectedAction === 'github' && (
        <InstallFromGitHub
          onSuccess={noop}
          onClose={() => setSelectedAction(null)}
        />
      )}
      {selectedAction === 'local' && selectedFile
        && (
          <InstallFromLocalPackage
            file={selectedFile}
            onClose={handleCloseLocalInstaller}
            onSuccess={noop}
          />
        )}
      {/* {pluginLists.map((item: any) => (
        <div key={item.id} onClick={() => handleUninstall(item.id)}>{item.name} 卸载</div>
      ))} */}
    </DropdownMenu>
  )
}

export default InstallPluginDropdown
