import PropTypes from 'prop-types';
import React from 'react';
import FieldSet from 'Components/FieldSet';
import FormGroup from 'Components/Form/FormGroup';
import FormInputGroup from 'Components/Form/FormInputGroup';
import FormLabel from 'Components/Form/FormLabel';
import { inputTypes, sizes } from 'Helpers/Props';
import titleCase from 'Utilities/String/titleCase';
import translate from 'Utilities/String/translate';

function UpdateSettings(props) {
  const {
    advancedSettings,
    settings,
    isWindows,
    isDocker,
    packageUpdateMechanism,
    onInputChange
  } = props;

  const {
    branch,
    updateAutomatically,
    updateMechanism,
    updateScriptPath
  } = settings;

  if (!advancedSettings) {
    return null;
  }

  const usingExternalUpdateMechanism = packageUpdateMechanism !== 'builtIn';

  const updateOptions = [];

  if (usingExternalUpdateMechanism) {
    updateOptions.push({
      key: packageUpdateMechanism,
      value: titleCase(packageUpdateMechanism)
    });
  } else {
    updateOptions.push({ key: 'builtIn', value: 'Built-In' });
  }

  updateOptions.push({ key: 'script', value: 'Script' });

  if (isDocker) {
    return (
      <FieldSet legend={translate('Updates')}>
        <div>
          {translate('UpdatingIsDisabledInsideADockerContainerUpdateTheContainerImageInstead')}
        </div>
      </FieldSet>
    );
  }

  return (
    <FieldSet legend={translate('Updates')}>
      <FormGroup
        advancedSettings={advancedSettings}
        isAdvanced={true}
      >
        <FormLabel>
          {translate('Branch')}
        </FormLabel>

        <FormInputGroup
          type={inputTypes.TEXT}
          name="branch"
          helpText={usingExternalUpdateMechanism ? translate('UsingExternalUpdateMechanismBranchUsedByExternalUpdateMechanism') : translate('UsingExternalUpdateMechanismBranchToUseToUpdateReadarr')}
          helpLink="https://wiki.servarr.com/readarr/faq#how-do-I-update-my-readarr"
          {...branch}
          onChange={onInputChange}
          readOnly={usingExternalUpdateMechanism}
        />
      </FormGroup>

      {
        !isWindows &&
          <div>
            <FormGroup
              advancedSettings={advancedSettings}
              isAdvanced={true}
              size={sizes.MEDIUM}
            >
              <FormLabel>
                {translate('Automatic')}
              </FormLabel>

              <FormInputGroup
                type={inputTypes.CHECK}
                name="updateAutomatically"
                helpText={translate('UpdateAutomaticallyHelpText')}
                onChange={onInputChange}
                {...updateAutomatically}
              />
            </FormGroup>

            <FormGroup
              advancedSettings={advancedSettings}
              isAdvanced={true}
            >
              <FormLabel>
                {translate('Mechanism')}
              </FormLabel>

              <FormInputGroup
                type={inputTypes.SELECT}
                name="updateMechanism"
                values={updateOptions}
                helpText={translate('UpdateMechanismHelpText')}
                helpLink="https://wiki.servarr.com/readarr/faq#how-do-i-update-my-readarr"
                onChange={onInputChange}
                {...updateMechanism}
              />
            </FormGroup>

            {
              updateMechanism.value === 'script' &&
                <FormGroup
                  advancedSettings={advancedSettings}
                  isAdvanced={true}
                >
                  <FormLabel>
                    {translate('ScriptPath')}
                  </FormLabel>

                  <FormInputGroup
                    type={inputTypes.TEXT}
                    name="updateScriptPath"
                    helpText={translate('UpdateScriptPathHelpText')}
                    onChange={onInputChange}
                    {...updateScriptPath}
                  />
                </FormGroup>
            }
          </div>
      }
    </FieldSet>
  );
}

UpdateSettings.propTypes = {
  advancedSettings: PropTypes.bool.isRequired,
  settings: PropTypes.object.isRequired,
  isWindows: PropTypes.bool.isRequired,
  isDocker: PropTypes.bool.isRequired,
  packageUpdateMechanism: PropTypes.string.isRequired,
  onInputChange: PropTypes.func.isRequired
};

export default UpdateSettings;
