<?php

use App\Models\Module;
use App\Models\Setting;
use App\Models\Language;
use Illuminate\Support\Facades\Auth;
use App\Providers\AppServiceProvider;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

if (!function_exists('usermodules')) {
    function usermodules(){
        $modules = Auth::user()->modules()->get();
        return $modules;
    }
}

if (!function_exists('languages')) {

    /**
     * Return list of languages from database
     * @return mixed
     */
    function languages() {
        $languages = Language::where('status', 1)->cursor();
        return $languages;
    }

}

if (!function_exists('currentLanguage')) {

    /**
     * Return current language from database
     * @return mixed
     */
    function currentLanguage() {
        $languages = Language::where('status', 1)->where('local', LaravelLocalization::getCurrentLocale())->first();
        return $languages;
    }

}

if (!function_exists('getCurrentLocale')) {

    /**
     * Return current current locale from database
     * @return mixed
     */
    function getCurrentLocale() {
        $current = Language::where('status', 1)->where('local', LaravelLocalization::getCurrentLocale())->first()->local;
        return $current;
    }

}

if (!function_exists('getAmountDetails')) {
    /**
     * params => $total = all total (subtotal + tax)
     * return => amount = subtotal
     *
     * @param $total
     * @return array
     */
    function getAmountDetails($total): array
    {
        $tax = (float)settings('tax_value   ');
        $taxAmount = round(($tax * $total) / ($tax + 100), 2);
        return [
            'tax' => $taxAmount,
            'amount' => round($total - $taxAmount, 2),
        ];
    }
}


if (!function_exists('array_first')) {

    /**
     * Return first item  of array

     */
    function array_first($array) {
        if (count($array) > 0) {
            return $array[0];
        } else {
            return null;
        }
    }

}

if (!function_exists('settings')) {

    /**
     * Return settings
     * @param string|array $key
     * @return
     */
    function settings($key = null) {
        if (is_array($key)) {
            $allSettings = Setting::whereIn('key', $key)->cursor()->toArray();
            foreach ($allSettings as $setting) {
                $settings[$setting['key']] = $setting['value'];
            }
        } elseif ($key) {
            $settings = Setting::where('key', $key)->first()->value;
        } else {
            $allSettings = Setting::cursor()->toArray();
            foreach ($allSettings as $setting) {
                $settings[$setting['key']] = $setting['value'];
            }
        }
        return $settings;
    }

}

if (!function_exists('isActive')) {

    /**
     * Return module name to module by module path from Modules table
     * most used in blades views
     * @param $url
     * @param $class
     * @return mixed
     */
    function isActive($url, $class = 'active') {
        $currentUrl = url()->current();
        if (is_array($url)) {
            if (in_array($currentUrl, $url)) {
                return $class;
            }
        }

        if ($currentUrl == $url) {
            return $class;
        }

        return true;
    }

}

if (!function_exists('stripText')) {

    /**
     * Return module name to module by module path from Modules table
     * most used in blades views
     * @param $text
     * @param $limit
     * @return mixed
     */
    function stripText($text, int $limit = null) {
        $description = strip_tags($text);
        $description = preg_replace('/\s\s+/', ' ', $description);
        if ($limit) {
            $description = \Illuminate\Support\Str::limit($description, $limit);
        }
        return $description ?? '';
    }

    if (!function_exists('wordLimit')) {

        /**
         * Return limit words
         * @param $string
         * @param $limit
         * @param null $end
         * @return string
         */
        function wordLimit($string, $limit = null, $end = '...'): string {
            return Str::words(stripText($string), $limit, $end);
        }

    }
}




if (!function_exists('limitString')) {
    function limitString($string, $limit = null, string $end = '...'): string {
        $clean = stripText($string);

        $truncated = mb_substr($clean, 0, $limit, 'UTF-8');

        return $truncated . ($end && mb_strlen($clean, 'UTF-8') > $limit ? $end : '');
    }
}

