import React, { useEffect } from 'react'
import { useInputValues, useProductionCapacityData, useThroughPutData } from '../../../../store/builder/store'
import { usePlayButtonStore } from '../../../../store/usePlayButtonStore';

export default function ProductionCapacityData() {
    const { throughputData } = useThroughPutData()
    const { productionCapacityData, setProductionCapacityData } = useProductionCapacityData()
    const { inputValues } = useInputValues();
    const { isPlaying } = usePlayButtonStore();

    useEffect(() => {
        if (!isPlaying) {
            setProductionCapacityData(0);
            return;
        }
        if (!inputValues || throughputData === undefined) return;

        const shiftLength = parseFloat(inputValues["Shift length"]);
        const shiftsPerDay = parseFloat(inputValues["Shifts / day"]);
        const workingDaysPerYear = parseFloat(inputValues["Working days / year"]);
        const yieldRate = parseFloat(inputValues["Yield rate"]);

        if (!isNaN(shiftLength) && !isNaN(shiftsPerDay) && !isNaN(workingDaysPerYear) &&
            !isNaN(yieldRate) && throughputData >= 0) {
            // Total units produced per day before yield
            const dailyProduction = throughputData * shiftLength * shiftsPerDay;
            

            // Units after applying yield rate
            const goodUnitsPerDay = dailyProduction * (yieldRate / 100);
            

            // Annual output
            const annualProduction = goodUnitsPerDay * workingDaysPerYear;
            

            // Final production capacity per hour (after yield)
            const productionPerHour = throughputData * (yieldRate / 100);
            

            // Set the final capacity (units/hour)
            setProductionCapacityData(Number(productionPerHour.toFixed(2)));
        }
    }, [throughputData, inputValues]);

    return (
        <></>
    )
}


