+import FileSaver from 'file-saver';
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Button, Col, Container, Row } from 'react-bootstrap';
+import { withTranslation } from 'react-i18next';
+import toastr from 'toastr';
+
+import BaseRomButton from './BaseRomButton';
+import { useAlttpBaseRom } from '../../helpers/AlttpBaseRomContext';
+import BPS from '../../helpers/bps';
+import i18n from '../../i18n';
+
+const applyPatch = (rom, patch, filename) => {
+ try {
+ const bps = new BPS();
+ bps.setPatch(patch);
+ bps.setSource(rom);
+ const result = bps.applyPatch();
+ FileSaver.saveAs(new Blob([result], { type: 'application/octet-stream' }), filename);
+ } catch (e) {
+ toastr.error(i18n.t('alttpSeeds.patchError', { msg: e.message }));
+ }
+};
+
+const isDefaultSetting = () => false;
+
+const Seed = ({ onRetry, patch, seed }) => {
+ const { rom } = useAlttpBaseRom();
+
+ return <Container>
+ <h1>{i18n.t('alttpSeeds.heading')}</h1>
+ <Row>
+ <Col md={{ order: 2 }}>
+ {rom ?
+ <Button
+ disabled={!seed || seed.status !== 'generated' || !patch}
+ onClick={() => applyPatch(
+ rom,
+ patch,
+ `${i18n.t('alttpSeeds.filename', {
+ hash: seed.hash,
+ preset: seed.preset,
+ })}.sfc`,
+ )}
+ variant="primary"
+ >
+ {i18n.t(patch ? 'alttpSeeds.patch' : 'alttpSeeds.fetchingPatch')}
+ </Button>
+ :
+ <BaseRomButton />
+ }
+ </Col>
+ <Col md={{ order: 1 }}>
+ <p>
+ {i18n.t('alttpSeeds.preset')}:
+ {' '}
+ <strong>{i18n.t(`alttpSeeds.presets.${seed.preset}`)}</strong>
+ </p>
+ {seed.seed ?
+ <p>
+ {i18n.t('alttpSeeds.seed')}:
+ {' '}
+ <strong>{seed.seed}</strong>
+ </p>
+ : null}
+ {seed.race ?
+ <p>{i18n.t('alttpSeeds.race')}</p>
+ : null}
+ {seed.mystery ?
+ <p>{i18n.t('alttpSeeds.mystery')}</p>
+ : null}
+ {seed.status === 'generated' ?
+ <p>
+ {i18n.t('alttpSeeds.generated')}:
+ {' '}
+ <strong>
+ {i18n.t('alttpSeeds.date', { date: new Date(seed.updated_at) })}
+ </strong>
+ </p>
+ :
+ <p>
+ {i18n.t('alttpSeeds.status')}:
+ {' '}
+ <strong>{i18n.t(`alttpSeeds.statuses.${seed.status}`)}</strong>
+ </p>
+ }
+ {seed.status === 'error' ?
+ <p>
+ <Button
+ onClick={onRetry}
+ variant="secondary"
+ >
+ {i18n.t('button.retry')}
+ </Button>
+ </p>
+ : null}
+ </Col>
+ </Row>
+ <h2 className="mt-5">{i18n.t('alttpSeeds.generator')}</h2>
+ <p>{i18n.t(`alttpSeeds.generators.${seed.generator}`)}</p>
+ {seed.settings ? <>
+ <h2 className="mt-5">{i18n.t('alttpSeeds.settings')}</h2>
+ <Row>
+ {Object.entries(seed.settings).map(([key, value]) =>
+ <Col key={key} sm={4} md={3} lg={2} className="mb-2">
+ <small className="text-muted">
+ {i18n.t(`alttpSeeds.settingName.${key}`)}
+ </small>
+ <br />
+ {isDefaultSetting(key, value) ?
+ i18n.t(`alttpSeeds.settingValue.${key}.${value}`)
+ :
+ <strong>{i18n.t(`alttpSeeds.settingValue.${key}.${value}`)}</strong>
+ }
+ </Col>
+ )}
+ </Row>
+ </> : null}
+ </Container>;
+};
+
+Seed.propTypes = {
+ onRetry: PropTypes.func,
+ patch: PropTypes.instanceOf(ArrayBuffer),
+ seed: PropTypes.shape({
+ generator: PropTypes.string,
+ hash: PropTypes.string,
+ mystery: PropTypes.bool,
+ preset: PropTypes.string,
+ race: PropTypes.bool,
+ seed: PropTypes.string,
+ settings: PropTypes.shape({
+ }),
+ status: PropTypes.string,
+ updated_at: PropTypes.string,
+ }),
+};
+
+export default withTranslation()(Seed);