<?php


namespace App\Service;


use App\Entity\DinAsset;
use App\Entity\DinAssetBenchmark;
use App\Utils\Utils;

class CalculatorService
{
    const KEY_REC_CYCLE_MAINTENANCE = 'recCycleMaintenance';
    const KEY_REC_CYCLE_INSPECTION = 'recCycleInspection';
    const KEY_COST_DIFF_CUR_REC_CYCLE = 'costDiffCurCycleRecCycle';
    const KEY_COST_DIFF_CUR_REC_PERCENTAGE = 'costDiffCurRecPercentage';

    public function calculateRisk($params, DinAsset $asset, DinAssetBenchmark $benchmark = null)
    {
        $userCycle = $params['cycle'];

        $pointsFailure = Utils::$riskFailure[$params['failure']][1];
        $pointsCosts = Utils::$riskCosts[$params['costs']][1];
        if ((int)$params['costs'] === Utils::PERSON_DAMAGE_INDEX) {
            $pointsCosts = Utils::PERSON_DAMAGE_BASE - $pointsFailure;
        }
        $riskPoints = $pointsFailure + $pointsCosts;
        $riskFactor = Utils::$riskPoints[$riskPoints]['factor'];

        $recCycleMaintenance = $asset->getCycleMonthsMax();
        if ((int)($params['maintenance']) === 1) {
            $recCycleMaintenance = (int) ($asset->getCycleMonthsMax() * $riskFactor);
            if ($recCycleMaintenance < $asset->getCycleMonthsMin()) {
                $recCycleMaintenance = $asset->getCycleMonthsMin();
            }
        }

        $recCycleInspection = $asset->getCycleMonthsMax();
        if ( ((int)$params['inspection']) === 1 ) {
            $recCycleInspection = (int) ($asset->getCycleMonthsMax() * $riskFactor);
            if ($recCycleInspection < $asset->getCycleMonthsMin()) {
                $recCycleInspection = $asset->getCycleMonthsMin();
            }
        }

        if ($recCycleInspection > $recCycleMaintenance) {
            $recCycleInspection = $recCycleMaintenance;
        }

        $costDiffCurCycleRecCycle = null;
        $costDiffCurRecPercentage = null;
        if ($benchmark !== null) {
            $costsCurrentPerYear = (12 / $userCycle) * $benchmark->getAverageCosts();
            $costsRecPerYear = (12 / $recCycleMaintenance) * $benchmark->getAverageCosts();
            $costDiffCurRecPercentage = round(1 - ($userCycle / $recCycleMaintenance), 2);
            $costDiffCurCycleRecCycle = $this->customRounding($costsCurrentPerYear - $costsRecPerYear);
        }

        $res[self::KEY_REC_CYCLE_MAINTENANCE] = $recCycleMaintenance;
        $res[self::KEY_REC_CYCLE_INSPECTION] = $recCycleInspection;
        $res[self::KEY_COST_DIFF_CUR_REC_PERCENTAGE] = $costDiffCurRecPercentage;
        $res[self::KEY_COST_DIFF_CUR_REC_CYCLE] = $costDiffCurCycleRecCycle;
        return $res;
    }

    private function customRounding($number)
    {
        $roundedNumber = round($number);
        $roundedNumberStr = (string)$roundedNumber;
        $numDigitsBeforeComma = strlen($roundedNumberStr);

        if ($numDigitsBeforeComma <= 2) {
            return $roundedNumber;
        } else {
            return round($number, ($numDigitsBeforeComma - 2) * -1);
        }
    }

}