<?php

use Illuminate\Support\Facades\Log;

if (!function_exists('toFloat')) {
    function toFloat($var)
    {
        if (is_numeric($var)) {
            return (float) $var;
        }
        return 0;
    }
}

if (!function_exists('toInt')) {
    function toInt($var)
    {
        if (is_numeric($var)) {
            return (int) $var;
        }
        return 0;
    }
}
if (!function_exists('limitar_cadena')) {
    function limitar_cadena($cadena, $limite, $sufijo = '')
    {
        // Si la longitud es mayor que el límite...
        if (strlen($cadena) > $limite) {
            // Entonces corta la cadena y ponle el sufijo
            return substr($cadena, 0, $limite) . $sufijo;
        }

        // Si no, entonces devuelve la cadena normal
        return $cadena;
    }
}
if (!function_exists('toArrayInt')) {
    function toArrayInt($data, $separador = ',')
    {
        $array = explode($separador, $data);
        $return = [];
        foreach ($array as $item) {
            $aux = toInt(trim($item));
            if ($aux) {
                $return[] = $aux;
            }
        }
        return $return;
    }
}
if (!function_exists('arrayExist')) {
    function arrayExist(array $array, String $valor_buscado)
    {
        if ($valor_buscado) {
            foreach ($array as $item) {
                if ($item == $valor_buscado) {
                    return true;
                } else if (\strtolower($item) == strtolower($valor_buscado)) {
                    return true;
                }
            }
        }
        return false;
    }
}

if (!function_exists('logMessage')) {
    function logMessage($mensaje)
    {
        Log::debug('->' . $mensaje);
    }
}

if (!function_exists('colorSemaforo')) {
    function colorSemaforo($type)
    {
        switch (strtolower($type)) {
            case ('success'):
                return 'background-color: #38c172; color: white;';
                break;
            case ('danger'):
                return 'background-color: #e3342f; color: white;';
                break;
            case ('warning'):
                return 'background-color: #ffed4a; color: black;';
                break;
            default:
                return 'background-color: #6c757d; color: white;';
                break;
        }
    }
}
if (!function_exists('htmlToPlainText')) {
    function htmlToPlainText($str)
    {
        $str = str_replace('&nbsp;', ' ', $str);
        $str = str_replace('>', '> ', $str);
        $str = str_replace('\n', ' ', $str);
        $str = html_entity_decode($str, ENT_QUOTES | ENT_COMPAT, 'UTF-8');
        $str = html_entity_decode($str, ENT_HTML5, 'UTF-8');
        $str = html_entity_decode($str);
        $str = htmlspecialchars_decode($str);
        $str = strip_tags($str);

        return trim(preg_replace(['/\s+/', '/^\s|\s$/'], [' ', ''], $str));
    }
}
if (!function_exists('getSubString')) {
    function getSubString($string, $length = NULL)
    {
        //Si no se especifica la longitud por defecto es 50
        if ($length == NULL)
            $length = 15;
        //Primero eliminamos las etiquetas html y luego cortamos el string
        $stringDisplay = substr(strip_tags($string), 0, $length);
        //Si el texto es mayor que la longitud se agrega puntos suspensivos
        if (strlen(strip_tags($string)) > $length)
            $stringDisplay .= '...';
        return $stringDisplay;
    }
}
if (!function_exists('isImageBase64')) {
    function isImageBase64($image)
    {
        return !!preg_match('/data:image/', $image);
    }
}

if (!function_exists('extraerId')) {
    function extraerId($data = null)
    {
        if (!$data) return null;
        try {
            if (is_array($data)) {

                $ar = [];
                foreach ($data as $item) {
                    $d = is_string($item) ? explode('_', $item) : [];
                    $ar[] = sizeof($d) ? (int)end($d) : null;
                }
                return $ar;
            } else if (is_string($data)) {
                $data = $data ? explode('_', $data) : [];
                $data = sizeof($data) ? (int)end($data) : null;
            } else {
                return null;
            }
            return $data;
        } catch (Exception $e) {
            return null;
        }
    }
}
