+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 { useAosBaseRom } from '../../helpers/AosBaseRomContext';
+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('aosSeeds.patchError', { msg: e.message }));
+ }
+};
+
+const Seed = ({ patch, seed }) => {
+ const { rom } = useAosBaseRom();
+
+ return <Container>
+ <h1>{i18n.t('aosSeeds.heading')}</h1>
+ <Row>
+ <Col md={{ order: 2 }}>
+ {rom ?
+ <Button
+ disabled={!patch}
+ onClick={() => applyPatch(
+ rom,
+ patch,
+ `${i18n.t('aosSeeds.filename', {
+ hash: seed.hash,
+ preset: seed.preset,
+ })}.gba`,
+ )}
+ variant="primary"
+ >
+ {i18n.t(patch ? 'aosSeeds.patch' : 'aosSeeds.fetchingPatch')}
+ </Button>
+ :
+ <BaseRomButton />
+ }
+ </Col>
+ <Col md={{ order: 1 }}>
+ <p>
+ {i18n.t('aosSeeds.preset')}:
+ {' '}
+ <strong>{i18n.t(`aosSeeds.presets.${seed.preset}`)}</strong>
+ </p>
+ <p>{i18n.t(seed.race ? 'aosSeeds.race' : 'aosSeeds.noRace')}</p>
+ <p>{i18n.t(seed.mystery ? 'aosSeeds.mystery' : 'aosSeeds.noMystery')}</p>
+ </Col>
+ </Row>
+ <h2 className="mt-5">{i18n.t('aosSeeds.generator')}</h2>
+ <p>{i18n.t(`aosSeeds.generators.${seed.generator}`)}</p>
+ </Container>;
+};
+
+Seed.propTypes = {
+ patch: PropTypes.instanceOf(ArrayBuffer),
+ seed: PropTypes.shape({
+ generator: PropTypes.string,
+ hash: PropTypes.string,
+ mystery: PropTypes.bool,
+ preset: PropTypes.string,
+ race: PropTypes.bool,
+ }),
+};
+
+export default withTranslation()(Seed);