import ms from "pretty-ms";
import { useCallback, useState } from "react";
import { createGame } from "../../../domain/apiMethods";
import { RANDOM_STREET_VIEW, URBAN } from "../../../domain/genMethods";
import {
FROZEN,
NORMAL,
TIME_BANK,
RACE,
COUNTRY_RACE,
} from "../../../domain/ruleSets";
import useCountryLookup from "../../../hooks/useCountryLookup";
import Loading from "../Loading";
import { Dropdown, DropdownGroup, Item, CountryDropdown } from "./Dropdown";
import ErrorModal from "./ErrorModal";
import styles from "./GameCreationForm.module.css";
const DEFAULTS = {
timer: 300,
rounds: 5,
countryLock: null,
genMethod: RANDOM_STREET_VIEW,
ruleSet: NORMAL,
};
const PRESETS = {
URBAN_AMERICA: {
...DEFAULTS,
genMethod: URBAN,
countryLock: "us",
},
URBAN_GLOBAL: {
...DEFAULTS,
genMethod: URBAN,
},
FAST_FROZEN: {
...DEFAULTS,
timer: 30,
rounds: 3,
genMethod: RANDOM_STREET_VIEW,
ruleSet: FROZEN,
},
};
const GameCreationForm = ({ afterCreate }) => {
const [loading, setLoading] = useState(false);
const [creationError, setCreationError] = useState(false);
const [timer, setTimer] = useState(DEFAULTS.timer);
const [rounds, setRounds] = useState(DEFAULTS.rounds);
const [countryLock, setCountryLock] = useState(DEFAULTS.countryLock);
const [genMethod, setGenMethod] = useState(DEFAULTS.genMethod);
const [ruleSet, setRuleSet] = useState(DEFAULTS.ruleSet);
const countryLookup = useCountryLookup(genMethod);
const setPreset = useCallback(
({
timer: newTimer,
rounds: newRounds,
countryLock: newCountryLock,
genMethod: newGenMethod,
ruleSet: newRuleSet,
}) => {
setTimer(newTimer);
setRounds(newRounds);
setCountryLock(newCountryLock);
setGenMethod(newGenMethod);
setRuleSet(newRuleSet);
},
[]
);
if (loading || countryLookup === null) {
return