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

export default function ROIData() {
    const { inputValues } = useInputValues();
    const { productionCapacityData } = useProductionCapacityData()

    useEffect(() => {
        if (inputValues === undefined) return;

        const electricityCost = parseFloat(inputValues["Electricity cost"]);
        const fixedCost = parseFloat(inputValues["Fixed costs"]);
        const laborCost = parseFloat(inputValues["Labor Cost"]);
        const maintenanceCost = parseFloat(inputValues["Maintenance cost"]); // Remove space typ
        const materialCost = parseFloat(inputValues["Material cost"]);
        const productionPeriod = parseFloat(inputValues["Production period"]);
        const salvageValue = parseFloat(inputValues["Salvage value"]);
        const sellingPrice = parseFloat(inputValues["Selling price"]);
        const initialInvestment = parseFloat(inputValues["Initial Investment"]);
        const shiftLength = parseFloat(inputValues["Shift length"]);
        const shiftsPerDay = parseFloat(inputValues["Shifts / day"]);
        const workingDaysPerYear = parseFloat(inputValues["Working days / year"]);

        if (!isNaN(electricityCost) && !isNaN(fixedCost) && !isNaN(laborCost) && !isNaN(maintenanceCost) &&
            !isNaN(materialCost) && !isNaN(productionPeriod) && !isNaN(salvageValue) && !isNaN(sellingPrice) &&
            !isNaN(shiftLength) && !isNaN(shiftsPerDay) && !isNaN(workingDaysPerYear) && productionCapacityData > 0) {

            console.log('sellingPrice: ', sellingPrice);
            console.log('salvageValue: ', salvageValue);
            console.log('productionPeriod: ', productionPeriod);
            console.log('materialCost: ', materialCost);
            console.log('maintenanceCost: ', maintenanceCost);
            console.log('laborCost: ', laborCost);
            console.log('fixedCost: ', fixedCost);
            console.log('electricityCost: ', electricityCost);


            // Revenue
            const RevenueForYear = productionCapacityData * sellingPrice;
            console.log('RevenueForYear: ', RevenueForYear);

            //Costs
            let MaterialCost = productionCapacityData * materialCost
            console.log('MaterialCost: ', MaterialCost);
            let LaborCost = laborCost * shiftLength * shiftsPerDay * workingDaysPerYear;
            console.log('LaborCost: ', LaborCost);
            let EnergyCost = electricityCost * shiftLength * shiftsPerDay * workingDaysPerYear;
            console.log('EnergyCost: ', EnergyCost);
            let MaintenceCost = maintenanceCost + fixedCost;
            console.log('MaintenceCost: ', MaintenceCost);

            //Total Anuual Cost
            let TotalAnnualCost = MaterialCost + LaborCost + EnergyCost + MaintenceCost;
            console.log('TotalAnnualCost: ', TotalAnnualCost);

            //Profit for Year
            let ProfitforYear = RevenueForYear - TotalAnnualCost;
            console.log('ProfitforYear: ', ProfitforYear);

            //Net Profit 
            let NetProfit = ProfitforYear * productionPeriod;
            console.log('NetProfit: ', NetProfit);


            //Final ROI
            const ROIData = ((NetProfit + salvageValue - initialInvestment) / TotalAnnualCost) * 100;
            console.log('ROIData: ', ROIData);


            // Payback Period
            const paybackPeriod = initialInvestment / ProfitforYear;
            console.log('paybackPeriod: ', paybackPeriod);


        }

    }, [inputValues, productionCapacityData]);

    return (
        <></>
    )
}


