import React, {useState} from 'react';
import CropPicker from './CropPicker';
import Modal from './Modal';
import Steps from './Steps';
import RecordHarvestModal from './RecordHarvestModal';
import SaveSeedsModal from './SaveSeedsModal';
// What can be done to a planting from a page that is not about one planting,
// like the home page: the question to ask, and the dialog that does it.
const BIG_TILES = 12; // up to this many plantings get big tiles, more get small ones
// Where seeds that were not saved from one of your plants can have come from;
// the keys are SaveSeedsModal's SOURCES.
const SOURCE_TILES = [
{key: 'bought', icon: 'shopping-basket', name: 'Seeds I bought'},
{key: 'catalogue', icon: 'book-open', name: 'Ordered from a seed catalogue'},
{key: 'seedbank', icon: 'landmark', name: 'From a seed bank or library'},
{key: 'traded', icon: 'exchange-alt', name: 'Traded with someone'},
{key: 'other', icon: 'question', name: 'Somewhere else, or not sure'},
];
const ACTIONS = {
harvest: {title: 'What did you harvest?', hint: 'These are your plantings. Choose the one you harvested from, or Something else for anything not listed.', elseTitle: 'What else did you harvest?', Dialog: RecordHarvestModal},
seeds: {title: 'Which plant are you saving seeds from?', hint: 'Choose the plant you saved the seeds from.', elseTitle: 'Which seeds are you adding?', Dialog: SaveSeedsModal},
};
// Asks which of your plantings you mean (or, for seeds, where they came from),
// then opens the usual dialog (recording a harvest, or saving seeds); once
// saved the page reloads.
//
// plantings: [{id, url, planted_at, ready, thumbnail, garden, crop: {id, name}}], what is growing,
// with what is ready to pick first.
// crop: {id, name}, when the crop is already known: only its plantings are offered, and
// the crop is not asked for again.
export default function PlantingFlow({action, plantings: all, crop: knownCrop, onClose}) {
const plantings = knownCrop ? all.filter((candidate) => candidate.crop.id === knownCrop.id) : all;
const [planting, setPlanting] = useState(null);
const [somethingElse, setElse] = useState(action === 'harvest' && !!knownCrop && plantings.length === 0); // a crop that is not one of the plantings
const [fromPlanting, setFromPlanting] = useState(false); // seeds: yes, they were saved from one of the plantings
const [source, setSource] = useState(null); // seeds: where the ones that were not saved from a plant came from
const [chosenCrop, setCrop] = useState(null); // the crop chosen with no planting
const crop = chosenCrop || (somethingElse ? knownCrop : null);
const {title, hint, elseTitle, Dialog} = ACTIONS[action];
// Back out of the crop's dialog, to the step before it.
function backFromCrop() {
setCrop(null);
if (knownCrop) {
setElse(false);
setSource(null);
}
}
// The steps before the dialog's own, so the progress shows the whole way through.
const choose = somethingElse ? 'Which crop?' : 'Which plant?';
const lead = action === 'seeds' ? ['Where from?', choose] : [choose];
const stepsBefore = (current) => ;
const askingSource = action === 'seeds' && !fromPlanting && !somethingElse; // seeds start with: from a plant of yours?
return (
<>
{askingSource && !planting && !crop && (