import React, { useState, useRef, useEffect } from "react"; import { ExitIcon, PlayStopIcon, ResetIcon } from "../../icons/SimulationIcons"; import { useActiveTool } from "../../../store/store"; import { useAnimationPlaySpeed, usePauseButtonStore, usePlayButtonStore, useResetButtonStore, } from "../../../store/usePlayButtonStore"; import { DailyProductionIcon, EndIcon, ExpandIcon, HourlySimulationIcon, InfoIcon, MonthlyROI, SpeedIcon, StartIcon, } from "../../icons/ExportCommonIcons"; import { getAvatarColor } from "../../../modules/collaboration/functions/getAvatarColor"; import { useSubModuleStore } from "../../../store/useModuleStore"; import ProductionCapacity from "../analysis/ProductionCapacity"; import ThroughputSummary from "../analysis/ThroughputSummary"; import ROISummary from "../analysis/ROISummary"; const SimulationPlayer: React.FC = () => { const MAX_SPEED = 8; // Maximum speed const isDragging = useRef(false); const sliderRef = useRef(null); const [expand, setExpand] = useState(true); const [playSimulation, setPlaySimulation] = useState(false); const { speed, setSpeed } = useAnimationPlaySpeed(); const { setIsPlaying } = usePlayButtonStore(); const { setActiveTool } = useActiveTool(); const { isPaused, setIsPaused } = usePauseButtonStore(); const { isReset, setReset } = useResetButtonStore(); const { subModule } = useSubModuleStore(); // Button functions const handleReset = () => { setReset(!isReset); setSpeed(1); }; const handlePlayStop = () => { setIsPaused(!isPaused); setPlaySimulation(!playSimulation); }; const handleExit = () => { setPlaySimulation(false); setIsPlaying(false); setActiveTool("cursor"); }; // Slider functions starts const handleSpeedChange = (event: React.ChangeEvent) => { setSpeed(parseFloat(event.target.value)); }; const calculateHandlePosition = () => { return ((speed - 0.5) / (MAX_SPEED - 0.5)) * 100; }; const handleMouseDown = () => { isDragging.current = true; document.addEventListener("mousemove", handleMouseMove); document.addEventListener("mouseup", handleMouseUp); }; const handleMouseMove = (e: MouseEvent) => { if (!isDragging.current || !sliderRef.current) return; const sliderRect = sliderRef.current.getBoundingClientRect(); const offsetX = e.clientX - sliderRect.left; const percentage = Math.min(Math.max(offsetX / sliderRect.width, 0), 1); const newValue = 0.5 + percentage * (50 - 0.5); setSpeed(parseFloat(newValue.toFixed(1))); }; const handleMouseUp = () => { isDragging.current = false; document.removeEventListener("mousemove", handleMouseMove); document.removeEventListener("mouseup", handleMouseUp); }; useEffect(() => { return () => { document.removeEventListener("mousemove", handleMouseMove); document.removeEventListener("mouseup", handleMouseUp); }; }, []); // Slider function ends // UI-Part const hourlySimulation = 25; const dailyProduction = 75; const monthlyROI = 50; const process = [ { name: "process 1", completed: 0 }, // 0% completed { name: "process 2", completed: 20 }, // 20% completed { name: "process 3", completed: 40 }, // 40% completed { name: "process 4", completed: 60 }, // 60% completed { name: "process 5", completed: 80 }, // 80% completed { name: "process 6", completed: 100 }, // 100% completed { name: "process 7", completed: 0 }, // 0% completed { name: "process 8", completed: 50 }, // 50% completed { name: "process 9", completed: 90 }, // 90% completed { name: "process 10", completed: 30 }, // 30% completed ]; // Move getRandomColor out of render const getRandomColor = () => { const letters = "0123456789ABCDEF"; let color = "#"; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; }; // Store colors for each process item const [_, setProcessColors] = useState([]); // Generate colors on mount or when process changes useEffect(() => { const generatedColors = process.map(() => getRandomColor()); setProcessColors(generatedColors); }, []); const intervals = [10, 20, 30, 40, 50, 60]; // in minutes const totalSegments = intervals.length; const progress = 20; // percent (example) const processPlayerRef = useRef(null); const processWrapperRef = useRef(null); const [playerPosition, setPlayerPosition] = useState(20); // initial position const handleProcessMouseDown = (e: React.MouseEvent) => { if (!processWrapperRef.current) return; const rect = processWrapperRef.current.getBoundingClientRect(); let x = e.clientX - rect.left; x = Math.max(0, Math.min(x, rect.width)); setPlayerPosition(x); const onMouseMove = (e: MouseEvent) => { if (!processWrapperRef.current) return; const newRect = processWrapperRef.current.getBoundingClientRect(); let newX = e.clientX - newRect.left; newX = Math.max(0, Math.min(newX, newRect.width)); setPlayerPosition(newX); const progressPercent = (newX / newRect.width) * 100; console.log(`Dragging at progress: ${progressPercent.toFixed(1)}%`); }; const onMouseUp = () => { document.removeEventListener("mousemove", onMouseMove); document.removeEventListener("mouseup", onMouseUp); }; document.addEventListener("mousemove", onMouseMove); document.addEventListener("mouseup", onMouseUp); }; return ( <>
{subModule === "analysis" && (
{/* hourlySimulation */}
Hourly Simulation
{/* dailyProduction */}
Daily Production
{/* monthlyROI */}
Monthly ROI
{" "}
)} {subModule === "simulations" && (
{playSimulation ? "Paused - system idle." : "Running simulation..."}
)}
{subModule === "analysis" && ( )}
23 April ,25
04:41 PM
{intervals.map((label, index) => { const segmentProgress = (index / totalSegments) * 100; const isFilled = progress >= segmentProgress; return (
{label} mins
{index < intervals.length - 1 && (
= ((index + 1) / totalSegments) * 100 ? "filled" : "" }`} >
)}
); })}
00:10:20
Speed
0.5X
4x
{subModule === "analysis" && (
00:00
24:00
{process.map((item, index) => (
))}
)}
); }; export default SimulationPlayer;