import { useEffect } from 'react'
import { comparsionMaterialData, useInputValues, useProductionCapacityData, useThroughPutData } from '../../../../store/builder/store'

import { saveSimulationData } from '../../../../components/layout/scenes/functions/simulationStorage';
import { usePlayButtonStore } from '../../../../store/ui/usePlayButtonStore';

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


    useEffect(() => {
        if (!isPlaying) {
            setProductionCapacityData(0);
        }
    }, [isPlaying]);

    useEffect(() => {
        if (!inputValues || throughputData === undefined || !isPlaying) return;
        const workingDaysPerYear = parseFloat(inputValues["Working days / year"]);

        if (!isNaN(workingDaysPerYear) && throughputData > 0) {
            const Monthly_working_days = workingDaysPerYear / 12;
            const Production_capacity_per_month = throughputData * Monthly_working_days;
            const data = Number(Production_capacity_per_month.toFixed(2));
            // saveSimulationData({ key: 'productionCapacity', data: data });
            setMaterialData({ ...materialData, productionCapacity: data });
            setProductionCapacityData(Number(Production_capacity_per_month.toFixed(2)));
        }
    }, [throughputData, inputValues, isPlaying, materialData]);

    return <></>;
}
