import { useMemo } from 'react'
import PropTypes from 'prop-types'
import ObjectInfo from '../../common/ObjectInfo'
import NewObjectForm from '../../common/NewObjectForm'
import WizardView from '../../common/WizardView'

const NewJob = ({
  onOk,
  defaultValues,
  slicerIntegration = false,
  currentStep,
  onStepChange
}) => {
  const initialValues = useMemo(
    () => ({
      state: { type: 'draft' },
      ...defaultValues
    }),
    [defaultValues]
  )

  return (
    <NewObjectForm type={'job'} defaultValues={initialValues}>
      {({ handleSubmit, submitLoading, objectData, formValid }) => {
        const steps = [
          {
            title: 'Required',
            key: 'required',
            content: (
              <ObjectInfo
                type='job'
                column={1}
                bordered={false}
                isEditing={true}
                required={true}
                labelWidth='100px'
                objectData={objectData}
              />
            )
          },
          {
            title: 'Summary',
            key: 'summary',
            content: (
              <ObjectInfo
                type='job'
                column={1}
                bordered={false}
                labelWidth='100px'
                visibleProperties={{
                  _id: false,
                  _reference: false,
                  createdAt: false,
                  updatedAt: false,
                  startedAt: false,
                  finishedAt: false,
                  totalTime: false
                }}
                isEditing={false}
                objectData={objectData}
              />
            )
          }
        ]
        return (
          <WizardView
            steps={steps}
            loading={submitLoading}
            formValid={formValid}
            title='New Job'
            showSteps={!slicerIntegration}
            currentStep={currentStep}
            onStepChange={onStepChange}
            showSkipButton={slicerIntegration}
            onSkip={() => {
              onOk(null)
            }}
            onSubmit={async () => {
              const result = await handleSubmit()
              if (result) {
                onOk(result)
              }
            }}
          />
        )
      }}
    </NewObjectForm>
  )
}

NewJob.propTypes = {
  onOk: PropTypes.func.isRequired,
  reset: PropTypes.bool,
  defaultValues: PropTypes.object,
  slicerIntegration: PropTypes.bool,
  currentStep: PropTypes.number,
  onStepChange: PropTypes.func
}

export default NewJob
