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 NewGCodeFile = ({
  onOk,
  defaultValues,
  slicerIntegration = false,
  shouldPrint = false,
  currentStep,
  onStepChange
}) => {
  const initialValues = useMemo(
    () => ({
      state: { type: 'draft' },
      ...defaultValues
    }),
    [defaultValues]
  )

  return (
    <NewObjectForm type={'gcodeFile'} defaultValues={initialValues}>
      {({ handleSubmit, submitLoading, objectData, formValid }) => {
        const steps = [
          ...(!slicerIntegration
            ? [
                {
                  title: 'Upload',
                  key: 'upload',
                  content: (
                    <ObjectInfo
                      type='gcodeFile'
                      column={1}
                      bordered={false}
                      isEditing={true}
                      required={true}
                      objectData={objectData}
                      visibleProperties={{ file: true }}
                      showLabels={false}
                    />
                  )
                }
              ]
            : []),
          {
            title: 'Required',
            key: 'required',
            content: (
              <ObjectInfo
                type='gcodeFile'
                column={1}
                bordered={false}
                isEditing={true}
                required={true}
                objectData={objectData}
                visibleProperties={{ file: false }}
              />
            )
          },
          {
            title: 'Summary',
            key: 'summary',
            content: (
              <ObjectInfo
                type='gcodeFile'
                column={1}
                bordered={false}
                visibleProperties={{
                  _id: false,
                  _reference: false,
                  parts: false,
                  createdAt: false,
                  updatedAt: false,
                  startedAt: false
                }}
                isEditing={false}
                objectData={objectData}
              />
            )
          }
        ]
        return (
          <WizardView
            steps={steps}
            loading={submitLoading}
            formValid={formValid}
            title='New GCode File'
            showSteps={!slicerIntegration || !shouldPrint}
            currentStep={currentStep}
            onStepChange={onStepChange}
            onSubmit={async () => {
              const result = await handleSubmit()
              if (result) {
                onOk(result)
              }
            }}
          />
        )
      }}
    </NewObjectForm>
  )
}

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

export default NewGCodeFile
