<?php 
 
namespace App\Services\Health; 
 
class CvdRiskPredictionLipids 
{ 
 
    private $M_AGE = 3.06117; 
    private $M_SBP_NO_TRT = 1.93303; 
    private $M_SBP_TRT = 1.99881; 
    private $M_TCL = 1.1237; 
    private $M_HDL = -0.93263; 
    private $M_SMOKE = 0.65451; 
    private $M_DIAB = 0.57367; 
 
    private $F_AGE = 2.32888; 
    private $F_SBP_NO_TRT = 2.76157; 
    private $F_SBP_TRT = 2.82263; 
    private $F_TCL = 1.20904; 
    private $F_HDL = -0.70833; 
    private $F_SMOKE = 0.52873; 
    private $F_DIAB = 0.69154; 
 
    public function calcGenCvdRiskPredictionLipids( 
        int $iSex, 
        int $iAge, 
        int $iSysBloodPress, 
        bool $bTreatmentForHypertension, 
        bool $bSmoking, 
        bool $bDiabetes, 
        int $iHDL, 
        int $iTotalCholesterol, 
        bool $debug = false 
    ) { 
        if ($debug) { 
            echo 'iSex: ' . $iSex . '<br/>'; 
            echo 'iAge: ' . $iAge . '<br/>'; 
            echo 'iSysBloodPress: ' . $iSysBloodPress . '<br/>'; 
            echo 'bTreatmentForHypertension: ' . (int)$bTreatmentForHypertension . '<br/>'; 
            echo 'bSmoking: ' . (int)$bSmoking . '<br/>'; 
            echo 'bDiabetes: ' . (int)$bDiabetes . '<br/>'; 
            echo 'iHDL: ' . $iHDL . '<br/>'; 
            echo 'iTotalCholesterol: ' . $iTotalCholesterol . '<br/><br/>'; 
        } 
 
        $risk = new PredictedRisk($iSex, $bTreatmentForHypertension); 
 
        $sex = $iSex; 
        $age = log($iAge); 
        $sbp = log($iSysBloodPress); 
        $tcl = log($iTotalCholesterol); 
        $hdl = log($iHDL); 
        $trtbp = $bTreatmentForHypertension ? 1 : 0; 
        $smoke = $bSmoking ? 1 : 0; 
        $diab = $bDiabetes ? 1 : 0; 
 
        if ($debug) { 
            echo 'sex: ' . $sex . '<br />'; 
            echo 'age: ' . $age . '<br />'; 
            echo 'sbp: ' . $sbp . '<br />'; 
            echo 'tcl: ' . $tcl . '<br />'; 
            echo 'hdl: ' . $hdl . '<br />'; 
            echo 'trtbp: ' . $trtbp . '<br />'; 
            echo 'smoke: ' . $smoke . '<br />'; 
            echo 'diab: ' . $diab . '<br /><br />'; 
        } 
 
        if ($iSex == GenderType::FEMALE) { 
            $sumBetaX_No_SBP_Trt = $age * $this->F_AGE + $sbp * $this->F_SBP_NO_TRT + $tcl * $this->F_TCL + $hdl * $this->F_HDL + $smoke * $this->F_SMOKE + $diab * $this->F_DIAB; 
 
            $sumBetaX_SBP_With_Trt = $age * $this->F_AGE + $sbp * $this->F_SBP_TRT + $tcl * $this->F_TCL + $hdl * $this->F_HDL + $smoke * $this->F_SMOKE + $diab * $this->F_DIAB; 
 
            $riskScore_No_SBP_Trt = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt - 26.1931)); 
 
            $riskScore_With_SBP_Trt = 1 - pow(0.95012, exp($sumBetaX_SBP_With_Trt - 26.1931)); 
        } elseif ($iSex == GenderType::MALE) { 
            $sumBetaX_No_SBP_Trt = $age * $this->M_AGE + $sbp * $this->M_SBP_NO_TRT + $tcl * $this->M_TCL + $hdl * $this->M_HDL + $smoke * $this->M_SMOKE + $diab * $this->M_DIAB; 
 
            $sumBetaX_SBP_With_Trt = $age * $this->M_AGE + $sbp * $this->M_SBP_TRT + $tcl * $this->M_TCL + $hdl * $this->M_HDL + $smoke * $this->M_SMOKE + $diab * $this->M_DIAB; 
 
            $riskScore_No_SBP_Trt = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt - 23.9802)); 
 
            $riskScore_With_SBP_Trt = 1 - pow(0.88936, exp($sumBetaX_SBP_With_Trt - 23.9802)); 
        } else { 
            return null;  // ERROR invalid gender 
        } 
 
        if ($debug) { 
            echo 'sumBetaX_No_SBP_Trt: ' . $sumBetaX_No_SBP_Trt . '<br/>'; 
            echo 'sumBetaX_SBP_With_Trt: ' . $sumBetaX_SBP_With_Trt . '<br/>'; 
            echo 'riskScore_No_SBP_Trt: ' . $riskScore_No_SBP_Trt . '<br/>'; 
            echo 'riskScore_With_SBP_Trt: ' . $riskScore_With_SBP_Trt . '<br/><br />'; 
        } 
 
        $risk->riskScore_No_SBP_Trt = $riskScore_No_SBP_Trt; 
        $risk->riskScore_With_SBP_Trt = $riskScore_With_SBP_Trt; 
 
        if ($iSex == GenderType::FEMALE) { 
            $sumBetaX_No_SBP_Trt_OPTIMAL = $this->F_AGE * $age + $this->F_SBP_NO_TRT * log(110) + $this->F_TCL * log(160) + $this->F_HDL * log(60); 
 
            $sumBetaX_No_SBP_Trt_NORMAL = $this->F_AGE * $age + $this->F_SBP_NO_TRT * log(125) + $this->F_TCL * log(180) + $this->F_HDL * log(45); 
        } elseif ($iSex == GenderType::MALE) { 
            $sumBetaX_No_SBP_Trt_OPTIMAL = $this->M_AGE * $age + $this->M_SBP_NO_TRT * log(110) + $this->M_TCL * log(160) + $this->M_HDL * log(60); 
 
            $sumBetaX_No_SBP_Trt_NORMAL = $this->M_AGE * $age + $this->M_SBP_NO_TRT * log(125) + $this->M_TCL * log(180) + $this->M_HDL * log(45); 
        } else { 
            return null;  // ERROR invalid gender 
        } 
 
        if ($debug) { 
            echo 'sumBetaX_No_SBP_Trt_OPTIMAL: ' . $sumBetaX_No_SBP_Trt_OPTIMAL * 100 . '<br/>'; 
            echo 'sumBetaX_No_SBP_Trt_NORMAL: ' . $sumBetaX_No_SBP_Trt_NORMAL * 100 . '<br/><br/>'; 
        } 
 
        if ($iSex == GenderType::FEMALE) { 
            $optimalRisk = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt_OPTIMAL - 26.1931)); 
 
            $normalRisk = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt_NORMAL - 26.1931)); 
        } elseif ($iSex == GenderType::MALE) { 
            $optimalRisk = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt_OPTIMAL - 23.9802)); 
 
            $normalRisk = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt_NORMAL - 23.9802)); 
        } else { 
            return null;  // ERROR invalid gender 
        } 
 
        $risk->optimalRisk = $optimalRisk; 
        $risk->normalRisk = $normalRisk; 
 
        if ($iSex == GenderType::FEMALE) { 
            if ($bTreatmentForHypertension) { 
                $consti_num = exp(-($this->F_SBP_TRT * $sbp + $this->F_TCL * $tcl + $this->F_HDL * $hdl + $this->F_SMOKE * $smoke + $this->F_DIAB * $diab - 26.1931) / 2.32888); 
                $consti_denom = pow((-log(0.95012)), (1 / 2.32888)); 
                $consti = $consti_num * 1 / $consti_denom; 
                $expo = 1 * (1 / 2.32888); 
                $term = pow(-log(1 - $riskScore_With_SBP_Trt), $expo); 
            } else { 
                $consti_num = exp(-($this->F_SBP_NO_TRT * $sbp + $this->F_TCL * $tcl + $this->F_HDL * $hdl + $this->F_SMOKE * $smoke + $this->F_DIAB * $diab - 26.1931) / 2.32888); 
                $consti_denom = pow((-log(0.95012)), (1 / 2.32888)); 
                $consti = $consti_num * 1 / $consti_denom; 
                $expo = 1 * (1 / 2.32888); 
                $term = pow(-log(1 - $riskScore_No_SBP_Trt), $expo); 
            } 
 
            $heart_age = $consti * $term; 
        } elseif ($iSex == GenderType::MALE) { 
            if ($bTreatmentForHypertension) { 
                $consti_num = exp(-($this->M_SBP_NO_TRT * $sbp + $this->M_TCL * $tcl + $this->M_HDL * $hdl + $this->M_SMOKE * $smoke + $this->M_DIAB * $diab - 23.9802) / 3.06117); 
                $consti_denom = pow((-log(0.88936)), (1 / 3.06117)); 
                $consti = $consti_num * 1 / $consti_denom; 
                $expo = 1 * (1 / 3.06117); 
                $term = pow(-log(1 - $riskScore_With_SBP_Trt), $expo); 
            } else { 
                $consti_num = exp(-($this->M_SBP_NO_TRT * $sbp + $this->M_TCL * $tcl + $this->M_HDL * $hdl + $this->M_SMOKE * $smoke + $this->M_DIAB * $diab - 23.9802) / 3.06117); 
                $consti_denom = pow((-log(0.88936)), (1 / 3.06117)); 
                $consti = $consti_num * 1 / $consti_denom; 
                $expo = 1 * (1 / 3.06117); 
                $term = pow(-log(1 - $riskScore_No_SBP_Trt), $expo); 
            } 
 
            $heart_age = $consti * $term; 
        } else { 
            return null;  // ERROR invalid gender 
        } 
 
        if ($debug) { 
            echo 'consti_num: ' . $consti_num . '<br/>'; 
            echo 'consti_denom: ' . $consti_denom . '<br/>'; 
            echo 'consti: ' . $consti . '<br/>'; 
            echo 'expo: ' . $expo . '<br/>'; 
            echo 'term: ' . $term . '<br/>'; 
        } 
 
        $risk->heart_age = round($heart_age); 
 
        return $risk->toArray(); 
    } 
 
}