import { Pizza, PlayCircle, Moon, Compass, Sword, FeatherIcon } from 'lucide-react'; import CollectResourcesButton from './CollectResourcesButton'; import { Pet } from '../types/Pet'; import { useState, useEffect } from 'react'; import { updatePetAction, getPetGatheredResources } from '../services/api/api'; import { PetActionGathered, PetBasicAction, PetGatherAction } from '../types/PetAction'; import ActionButton from './button/ActionButton'; import ActionResourceButton from './ActionResourceButton'; import ResourceSelectionModal from './modal/ResourceSelectionModal'; interface InteractionMenuProps { pet: Pet; onPetUpdate: (updatedPet: Pet) => void; onCustomize: () => void; } export default function InteractionMenu({ pet, onPetUpdate }: InteractionMenuProps) { const [gatheredResources, setGatheredResources] = useState([]); const [remainingCooldown, setRemainingCooldown] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); useEffect(() => { const updateCooldown = () => { if (!pet.basicActionCooldown) { setRemainingCooldown(null); return; } const cooldownTime = new Date(pet.basicActionCooldown).getTime(); const now = new Date().getTime(); const remaining = Math.max(0, cooldownTime - now); if (remaining === 0) { setRemainingCooldown(null); } else { setRemainingCooldown(remaining); } }; updateCooldown(); const interval = setInterval(updateCooldown, 1000); return () => clearInterval(interval); }, [pet.basicActionCooldown]); useEffect(() => { const fetchGatheredResources = async () => { if (pet.petGatherAction === 'IDLE') { return; } try { const resources = await getPetGatheredResources(pet.id); setGatheredResources(resources); } catch (error) { console.error('Failed to fetch gathered resources:', error); } }; fetchGatheredResources(); const interval = setInterval(fetchGatheredResources, 10000); // Poll every 10 seconds return () => clearInterval(interval); }, [pet.id, pet.petGatherAction]); const formatCooldownTime = (ms: number) => { const minutes = Math.floor(ms / 60000); const seconds = Math.floor((ms % 60000) / 1000); if (minutes > 0) { return `${minutes} minute${minutes > 1 ? 's' : ''} remaining`; } return `${seconds} second${seconds > 1 ? 's' : ''} remaining`; }; const handleGatherComplete = (updatedPet: Pet) => { onPetUpdate(updatedPet); }; function performBasicAction(basicAction: PetBasicAction): () => void { return async () => { try { const updatedPet = await updatePetAction(pet.id, { basicAction: basicAction }); onPetUpdate(updatedPet); } catch (error) { console.error('Failed to perform basic action:', error); } }; } const handleActionStart = async (actionType: 'gather' | 'explore' | 'battle') => { if (actionType === 'gather') { setIsModalOpen(true); return; } try { const action: PetGatherAction = actionType === 'explore' ? 'EXPLORE' : 'BATTLE'; const updatedPet = await updatePetAction(pet.id, { gatherAction: action }); onPetUpdate(updatedPet); } catch (error) { console.error('Failed to start action:', error); } }; const handleResourceSelect = async (resourceType: string) => { if (resourceType === 'stop') { try { const updatedPet = await updatePetAction(pet.id, { gatherAction: 'IDLE' }); onPetUpdate(updatedPet); } catch (error) { console.error('Failed to stop action:', error); } setIsModalOpen(false); return; } try { const action: PetGatherAction = `GATHERING_${resourceType.toUpperCase()}` as PetGatherAction; const updatedPet = await updatePetAction(pet.id, { gatherAction: action }); onPetUpdate(updatedPet); } catch (error) { console.error('Failed to start gathering:', error); } finally { setIsModalOpen(false); } }; const handleCollect = () => { setGatheredResources([]); } return (
{remainingCooldown !== null && (
Cooldown: {formatCooldownTime(remainingCooldown)}
)}
handleActionStart('gather')} onActionComplete={handleGatherComplete} /> handleActionStart('explore')} onActionComplete={handleGatherComplete} /> handleActionStart('battle')} onActionComplete={handleGatherComplete} />
setIsModalOpen(false)} onGather={handleResourceSelect} pet={pet} isGathering={pet.petGatherAction !== 'IDLE'} />
); }