import type { OnFeaturesChange } from '@/app/components/base/features/types'
import type { UploadFileSetting } from '@/app/components/workflow/types'
import { RiCloseLine } from '@remixicon/react'
import { produce } from 'immer'
import * as React from 'react'
import { useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Button from '@/app/components/base/button'
import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks'
import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
import FileUploadSetting from '@/app/components/workflow/nodes/_base/components/file-upload-setting'
import { SupportUploadFileTypes } from '@/app/components/workflow/types'

type SettingContentProps = {
  imageUpload?: boolean
  onClose: () => void
  onChange?: OnFeaturesChange
}
const SettingContent = ({
  imageUpload,
  onClose,
  onChange,
}: SettingContentProps) => {
  const { t } = useTranslation()
  const featuresStore = useFeaturesStore()
  const file = useFeatures(state => state.features.file)
  const fileSettingPayload = useMemo(() => {
    return {
      allowed_file_upload_methods: file?.allowed_file_upload_methods || ['local_file', 'remote_url'],
      allowed_file_types: file?.allowed_file_types || [SupportUploadFileTypes.image],
      allowed_file_extensions: file?.allowed_file_extensions || FILE_EXTS[SupportUploadFileTypes.image],
      max_length: file?.number_limits || 3,
    } as UploadFileSetting
  }, [file])
  const [tempPayload, setTempPayload] = useState<UploadFileSetting>(fileSettingPayload)

  const handleChange = useCallback(() => {
    const {
      features,
      setFeatures,
    } = featuresStore!.getState()

    const newFeatures = produce(features, (draft) => {
      draft.file = {
        ...draft.file,
        allowed_file_upload_methods: tempPayload.allowed_file_upload_methods,
        number_limits: tempPayload.max_length,
        allowed_file_types: tempPayload.allowed_file_types,
        allowed_file_extensions: tempPayload.allowed_file_extensions,
      }
    })

    setFeatures(newFeatures)
    if (onChange)
      onChange()
  }, [featuresStore, onChange, tempPayload])

  return (
    <>
      <div className="mb-4 flex items-center justify-between">
        <div className="text-text-primary system-xl-semibold">{!imageUpload ? t('feature.fileUpload.modalTitle', { ns: 'appDebug' }) : t('feature.imageUpload.modalTitle', { ns: 'appDebug' })}</div>
        <div className="cursor-pointer p-1" onClick={onClose}><RiCloseLine className="h-4 w-4 text-text-tertiary" /></div>
      </div>
      <FileUploadSetting
        isMultiple
        inFeaturePanel
        hideSupportFileType={imageUpload}
        payload={tempPayload}
        onChange={(p: UploadFileSetting) => setTempPayload(p)}
      />
      <div className="mt-4 flex items-center justify-end">
        <Button
          onClick={onClose}
          className="mr-2"
        >
          {t('operation.cancel', { ns: 'common' })}
        </Button>
        <Button
          variant="primary"
          onClick={handleChange}
          disabled={tempPayload.allowed_file_types.length === 0}
        >
          {t('operation.save', { ns: 'common' })}
        </Button>
      </div>
    </>
  )
}

export default React.memo(SettingContent)
