import { Container, Loading } from '@/components/shared'
import { useLocalization } from '@/utils/hooks/useLocalization'
import { Helmet } from 'react-helmet'
import { useParams } from 'react-router-dom'
import FormButtons from './FormButtons'
import FormDevExpress from './FormDevExpress'
import SubForms from './SubForms'
import { FormProps } from './types'
import { useGridData } from './useGridData'
import { useCurrentMenuIcon } from '@/utils/hooks/useCurrentMenuIcon'
import { Badge } from '@/components/ui'
import { useState } from 'react'
import { FormActivityPanel } from './FormActivityPanel/FormActivityPanel'

const FormEdit = (
  props: FormProps = {
    isSubForm: false,
    onActionView: () => {},
    onActionNew: () => {},
  },
) => {
  const mode = 'edit'
  const { isSubForm, level } = props
  const params = useParams()
  const listFormCode = props?.listFormCode ?? params?.listFormCode ?? ''
  const id = props?.id ?? params?.id ?? ''
  const MenuIcon = useCurrentMenuIcon('w-5 h-5')
  
  const [isActivityPanelVisible, setIsActivityPanelVisible] = useState(false)

  const { translate } = useLocalization()
  const {
    fetchData,
    setFormData,
    handleSubmit,
    loading,
    gridDto,
    dataSource,
    commandColumnData,
    filter,
    formData,
    formItems,
    refForm,
    permissionResults,
  } = useGridData({
    mode: 'edit',
    listFormCode,
    id,
    level,
    isSubForm,
    onSubmitAction: props?.onActionView,
  })

  if (!listFormCode) {
    return <></>
  }

  if (loading) {
    return <Loading type="cover" loading={loading}></Loading>
  }

  if (!formData) {
    return <>{translate('::Error:0002')}</>
  }

  return (
    <>
      <Container className={`${isActivityPanelVisible && !isSubForm ? 'mr-[400px]' : ''} transition-all duration-300`}>
        {!isSubForm && (
          <Helmet
            titleTemplate="%s | Sözsoft Kurs Platform"
            title={translate('::' + gridDto?.gridOptions.title)}
            defaultTitle="Sözsoft Kurs Platform"
          ></Helmet>
        )}

        <div
          className={`flex items-center pb-2 px-2 ${isSubForm ? 'justify-end' : 'justify-between'}`}
        >
          <div className="flex items-center gap-2">
            {MenuIcon}
            {!isSubForm && (
              <>
                <h4 className="text-slate-700 text-sm font-medium leading-none">
                  {translate('::' + gridDto?.gridOptions?.title)}
                </h4>
                ●
                <Badge content={mode} />
              </>
            )}
          </div>
          {permissionResults && (
            <FormButtons
              isSubForm={isSubForm}
              mode={mode}
              listFormCode={listFormCode}
              id={formData?.Id}
              gridDto={gridDto!}
              commandColumnData={commandColumnData!}
              dataSource={dataSource!}
              permissions={permissionResults}
              handleSubmit={handleSubmit}
              refreshData={fetchData}
              getSelectedRowKeys={() => [id]}
              getSelectedRowsData={() => [formData]}
              getFilter={() => filter}
              onActionView={props?.onActionView}
              onActionNew={props?.onActionNew}
            />
          )}
        </div>
        <div className="px-2">
          <FormDevExpress
            mode={mode}
            refForm={refForm}
            formData={formData}
            formItems={formItems}
            setFormData={setFormData}
            listFormCode={listFormCode}
          />
        </div>
        <SubForms gridDto={gridDto!} formData={formData} level={level ?? 0} />
      </Container>

      {/* Activity Panel - sadece ana formda göster */}
      {!isSubForm && listFormCode && id && gridDto?.gridOptions?.showActivity && (
        <FormActivityPanel
          entityName={listFormCode}
          entityId={id}
          isVisible={isActivityPanelVisible}
          onToggle={() => setIsActivityPanelVisible(!isActivityPanelVisible)}
        />
      )}
    </>
  )
}

export default FormEdit
