<?php

if (!class_exists('Enum')) {
    include(__DIR__ . '/../config/enum.php');
}

class Config
{
}

$user_roles = [
    1 => 'Super Admin',
    2 => 'Admin',
    3 => 'User'
];

$user_status = [
    0 => 'DE-ACTIVE',
    1 => 'ACTIVE'
];

$currentMonth = date('n'); // Current month (1 to 12)
$currentYear = date("Y");

$daysNameShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
$daysNameFull = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
$monthsNameShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
$monthsNameFull = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function Permission($role_required = 1)
{
    $user_role = User::DesignationCode();

    if ($user_role == $role_required || $user_role == 1) {
        return true;
    }

    return false;
}

function sCurrencySymbol($beforSpace = true, $afterSpace = true)
{
    $symble = '&#2547;';
    $currencySymble = $symble;
    if ($beforSpace) {
        $currencySymble = "&nbsp;" . $currencySymble;
    }
    if ($afterSpace) {
        $currencySymble = $currencySymble . "&nbsp;";
    }
    return $currencySymble;
}

function sDate($date, $daysName = false, $time = false, $use12HourFormat = true, $hideSecond = true)
{
    global $daysNameShort, $monthsNameShort;

    if ($date instanceof DateTime) {
        $formattedDate = $date;
    } else {
        $formattedDate = new DateTime($date);
    }

    $dayName = $daysNameShort[$formattedDate->format('w')];
    $day = $formattedDate->format('d');
    $monthName = $monthsNameShort[$formattedDate->format('m') - 1];
    $year = $formattedDate->format('Y');

    $result = "{$day}-{$monthName}-{$year}";

    if ($daysName) {
        $result = "{$dayName}, {$day}-{$monthName}-{$year}";
    }

    $result = $result . ($time ? ' ' . sTime($date, $use12HourFormat, $hideSecond) : '');
    return $result;
}

function sTime($date, $use12HourFormat = true, $hideSecond = true)
{
    if ($date instanceof DateTime) {
        $formattedDate = $date;
    } else {
        $formattedDate = new DateTime($date);
    }

    $hours = (int) $formattedDate->format('H');
    $minutes = $formattedDate->format('i');
    $seconds = $formattedDate->format('s');

    $period = '';
    if ($use12HourFormat) {
        $period = ($hours >= 12) ? ' PM' : ' AM';
        $hours = $hours % 12;
        if ($hours == 0) {
            $hours = 12;
        }
        $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
    } else {
        $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
    }

    return "{$hours}:{$minutes}" . ($hideSecond ? '' : ":{$seconds}") . "{$period}";
}

function eventDuration($date): string
{
    if ($date instanceof DateTime) {
        $start = $date;
    } else {
        $start = new DateTime($date);
    }

    $end = clone $start;
    $end->modify('+5 hours');

    return sTime($start) . ' - ' . sTime($end);
}

function AdditionalTimeArr($input, $onlyNumber = false)
{
    if (empty($input))
        return [];

    $parts = explode('|', $input);

    if (count($parts) !== 2) {
        return [];
    }

    $hours = $parts[0];
    $amount = $parts[1];

    $hoursLabel = ((int) $hours > 1) ? $hours . ' Hours' : $hours . ' Hour';
    $hoursLabel = ($onlyNumber) ? $hours : $hoursLabel;

    return [
        'hours' => $hoursLabel,
        'amount' => $amount
    ];
}

function AddOnsArr($input)
{
    if (empty($input))
        return [];

    $result = [];
    $items = explode(',', $input);

    foreach ($items as $item) {
        $parts = explode('|', $item);
        if (count($parts) !== 2)
            continue;

        $result[] = [
            'name' => trim($parts[0]),
            'amount' => trim($parts[1])
        ];
    }

    return $result;
}

function PackageArr($input)
{
    if (empty($input))
        return [];

    $result = [];
    $items = explode(',', $input);

    foreach ($items as $item) {
        $parts = explode('|', $item);
        if (count($parts) !== 2)
            continue;

        $result[] = [
            'title' => trim($parts[0]),
            'details' => trim($parts[1])
        ];
    }

    return $result;
}

function sTakaFormat($amount, $paisa = false, $symble = false, $beforSpace = true, $afterSpace = true)
{
    $amount = number_format($amount, 2, '.', '');
    $parts = explode('.', $amount);
    $number = $parts[0];
    $decimal = isset($parts[1]) ? '.' . $parts[1] : '';

    $len = strlen($number);
    if ($len > 3) {
        $last3 = substr($number, -3);
        $restUnits = substr($number, 0, $len - 3);
        $restUnits = preg_replace("/\B(?=(\d{2})+(?!\d))/", ",", $restUnits);
        $formatted = $restUnits . ',' . $last3;
    } else {
        $formatted = $number;
    }

    $result = '';

    if ($paisa) {
        $result = $formatted . $decimal;
    } else {
        $result = $formatted;
    }

    return ($symble ? sCurrencySymbol($beforSpace, $afterSpace) : '') . $result;
}

function sNumberToWords($number)
{
    $words = array(
        '0' => 'zero',
        '1' => 'one',
        '2' => 'two',
        '3' => 'three',
        '4' => 'four',
        '5' => 'five',
        '6' => 'six',
        '7' => 'seven',
        '8' => 'eight',
        '9' => 'nine',
        '10' => 'ten',
        '11' => 'eleven',
        '12' => 'twelve',
        '13' => 'thirteen',
        '14' => 'fourteen',
        '15' => 'fifteen',
        '16' => 'sixteen',
        '17' => 'seventeen',
        '18' => 'eighteen',
        '19' => 'nineteen',
        '20' => 'twenty',
        '30' => 'thirty',
        '40' => 'forty',
        '50' => 'fifty',
        '60' => 'sixty',
        '70' => 'seventy',
        '80' => 'eighty',
        '90' => 'ninety'
    );

    $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');

    if (!is_numeric($number))
        return false;

    $number = number_format($number, 2, '.', '');
    $parts = explode('.', $number);
    $no = (int) $parts[0];
    $point = isset($parts[1]) ? (int) $parts[1] : 0;

    $str = array();
    $digits_1 = strlen($no);
    $i = 0;

    $divider = [10000000, 100000, 1000, 100, 1];
    $words_order = ['crore', 'lakh', 'thousand', 'hundred', ''];

    foreach ($divider as $idx => $value) {
        $quotient = intval($no / $value);
        $no = $no % $value;

        if ($quotient) {
            if ($quotient < 21) {
                $str[] = $words[$quotient] . ' ' . $words_order[$idx];
            } else {
                $tens = intval($quotient / 10) * 10;
                $unit = $quotient % 10;
                $str[] = $words[$tens] . ' ' . ($unit ? $words[$unit] : '') . ' ' . $words_order[$idx];
            }
        }
    }

    $taka = implode(' ', array_filter($str));

    $paisa = '';
    if ($point) {
        if ($point < 21) {
            $paisa = $words[$point];
        } else {
            $tens = intval($point / 10) * 10;
            $unit = $point % 10;
            $paisa = $words[$tens] . ' ' . ($unit ? $words[$unit] : '');
        }
    }

    if (empty($taka)) {
        $taka = 'zero';
    }

    $result = ucwords($taka);
    if ($paisa) {
        $result .= ' and ' . ucwords($paisa) . ' Paisa';
    }
    $result .= ' Taka Only';

    return $result;
}
