<?php

namespace App\Services;

class MyServices
{
    public function leaveCalculation(int $ab_de_day, int $le_de_day, int $la_de_day, int $ea_de_day, int $ue_de_day, int $al, int $cl)
    {
        $arr = array("Cl" => "0", "Al" => "0", "Salary" => "0");
        $day = 0;
        $temp = 0;
        $t_day = 0;

        $day = $ab_de_day;

        if ($day > 0)
        {
            if ($cl == 0)
            {
                $temp = $al - $day;
                if ($temp < 0)
                {
                    $arr['Al'] = $al;
                    $arr['Salary'] = $temp;
                }
                else
                    $arr['Al'] = $day;
            }
            else
            {
                if ($day > $cl)  // add by Malay
                {
                    $arr['Cl'] = $cl;

                    $rest_day = $day - $cl;
                    $cl = 0;
                    if ($rest_day > 0)
                    {
                        if ($al >= $rest_day)
                        {
                            $arr['Al'] = $rest_day;
                            $al = $al - $rest_day;
                        }
                        else
                            $arr['Salary'] = $rest_day;
                    }
                }
                else          // add by Malay
                {
                    $temp = $cl - $day;
                    if ($temp < 0)
                    {
                        $arr['Cl'] = $cl;

                        $t_day = $temp;
                        $temp = $al - $temp;
                        if ($temp < 0)
                        {
                            $arr['Al'] = $al;
                            $arr['Salary'] = $temp;
                        }
                        else
                            $arr['Al'] = $t_day;
                    }
                    else
                        $arr['Cl'] = $day;
                }
            }

        }

        $temp = (int)($le_de_day + $la_de_day + $ea_de_day + $ue_de_day);
       
            if ($temp > 4)
            {
                if ($al > 0)
                    $arr['Al'] = $arr['Al'] + 1;
                else
                    $arr['Salary'] = $arr['Salary'] + 1;
            }
            else if ($temp > 2)
            {
                if ($cl > 0)
                    $arr['Cl'] = $arr['Cl'] + 1;
                else if ($al > 0)
                    $arr['Al'] = $arr['Al'] + 1;
                else
                    $arr['Salary'] = $arr['Salary'] + 1;
            }
        

        return $arr;
    }
}