import { useGameRepairMutation, useGameResetMutation, useGameStartMutation, useGameStopMutation, useGameSwitchPlayerMutation } from '@/hooks/mutations/useGameMutation' import { useAuth } from '@/hooks/useAuth' import { useGame, useSetLocationAccuracy, useUpdateGameState } from '@/hooks/useGame' import { useQueryClient } from '@tanstack/react-query' import { useRouter } from 'expo-router' import { useState } from 'react' import { Button, Dialog, List, MD3Colors, Portal, Snackbar, Surface, Text } from 'react-native-paper' import { Dropdown } from 'react-native-paper-dropdown' import { Accuracy } from 'expo-location' export default function HistoryScreen() { const [successVisible, setSuccessVisible] = useState(false) const [successMessage, setSuccessMessage] = useState("") const [errorVisible, setErrorVisible] = useState(false) const [error, setError] = useState([200, ""]) const router = useRouter() const queryClient = useQueryClient() const auth = useAuth() const game = useGame() const updateGameState = useUpdateGameState() const setLocationAccuracy = useSetLocationAccuracy() const accuracyArrayList = [ { value: Accuracy.BestForNavigation.toString(), label: "Navigation" }, { value: Accuracy.Highest.toString(), label: "Plus haute" }, { value: Accuracy.High.toString(), label: "Haute" }, { value: Accuracy.Balanced.toString(), label: "Équilibrée" }, { value: Accuracy.Low.toString(), label: "Basse" }, { value: Accuracy.Lowest.toString(), label: "Plus basse" }, { value: 'null', label: "Désactivée" }, ] const gameStartMutation = useGameStartMutation({ auth, updateGameState, onPostSuccess: () => { setSuccessVisible(true) setSuccessMessage("Jeu démarré avec succès") queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const gameStopMutation = useGameStopMutation({ auth, updateGameState, onPostSuccess: () => { setSuccessVisible(true) setSuccessMessage("Jeu arrêté avec succès") queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const gameSwitchMutation = useGameSwitchPlayerMutation({ auth, updateGameState, onPostSuccess: () => { setSuccessVisible(true) setSuccessMessage("Échange de joueuse en course réalisé avec succès") queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const gameRepairMutation = useGameRepairMutation({ auth, onPostSuccess: () => { setSuccessVisible(true) setSuccessMessage("Réparation du jeu effectuée avec succès") queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-game' }) queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-player' }) queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'money-updates' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const gameResetMutation = useGameResetMutation({ auth, updateGameState, onPostSuccess: () => { setSuccessVisible(true) setSuccessMessage("Jeu réinitialisé avec succès") queryClient.invalidateQueries() }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const [resetConfirmVisible, setResetConfirmVisible] = useState(false) return ( } onPress={() => router.navigate('/login')} /> setLocationAccuracy(!value || value == 'null' ? null : +value)} />} /> } onPress={() => gameStartMutation.mutate()} /> } onPress={() => gameStopMutation.mutate()} /> } onPress={() => gameSwitchMutation.mutate()} /> } onPress={() => gameRepairMutation.mutate()} /> } onPress={() => setResetConfirmVisible(true)} /> setSuccessVisible(false)} onIconPress={() => setSuccessVisible(false)}> {successMessage} setErrorVisible(false)} onIconPress={() => setErrorVisible(false)}> Erreur {error[0]} : {error[1]} setResetConfirmVisible(false)}> Confirmer Cette action va réinitialiser TOUTES les données de jeu : l'historique des positions, les défis réalisés et les trains empruntés. Êtes-vous réellement sûr⋅e de vouloir tout supprimer ? ) }