<?php 

/**
 *
 * @param  Nav
 * @return mixed
 */
function nav($nav = null, $active = null, $count = null)
{
    $i = 0;
    $html = null;
    foreach ($nav as $row) {
        if (isset($row['header'])) {

            $html .= '<li class="nav-header hidden-folded ' . (isset($row['class']) ? $row['class'] : '') . '">' . $row['header'] . '</span></li>';

        } else {

            $html .= '<li class="' . (isset($row['margin']) ? $row['margin'] : '');
            if (isset($active) AND $active == $row['main']) {
                $html .= ' active ';
            } elseif (empty($active) AND $row['main'] == 'main') {
                $html .= ' active ';
            }
            $html .= '">';
            
            $html .= '<a href="';
            if (empty($row['sub'])) {
                $html .= APP . '/' . $row['url'];
            } else {
                $html .= 'javascript:;';
            }
            $html .= '">';
            $html .= '<span class="nav-icon"><svg width="20" height="20" fill="currentColor"><use xlink:href="'.ASSETS.'/sprite/sprite.svg#'.(isset($row['icon']) ? $row['icon'] : '').'" /></svg></span>';
            if (isset($count[$row['main']])) {
                $html .= '<div class="nav-text">';
                $html .= '<div class="fw-semibold">' . $row['name'] . '</div>';
                $html .= '<div class="fs-xs text-muted">' . (isset($count[$row['main']]) ? $count[$row['main']] : '') . ' ' . (isset($row['subtext']) ? $row['subtext'] : '') . '</div>';
                $html .= '</div>';
            } else {
                $html .= '<div class="nav-text fw-semibold">' . $row['name'] . '</div>';
            }
            
            if (isset($row['sub'])) {
                $html .= '<span class="nav-arrow"></span>';
            }
            $html .= '</a>';
            if (isset($row['sub'])) {
                $html .= '<ul class="nav-sub">';
                foreach ($row['sub'] as $SubCategory) {
                    $html .= '<li><a href=\'' . APP . '/' . $SubCategory['url'] . '\'>' . $SubCategory['name'] . '</a></li>';
                }
                $html .= '</ul>';
            }
            $html .= '</li>';
        }
        $i++;
    }
    return $html;
}

/**
 *
 * @param getRandomColor
 * @return mixed
 */  
function getRandomColor() { 
    $rand = array('55E4DB', '66D78C', '68CF6C', '6ABEE2', '706DE4', '7391F8', 'D34949', 'D760D2', 'F06868', 'F6A963');
    return '#'.$rand[rand(0,9)];
}
/**
 *
 * @param  Json Get
 * @return mixed
 */

function get($data = array(), $field = null, $name = null, $parse = true)
{
    if ($name) {
        foreach ($data  as $key => $val) {
            if ($val['name'] == $name) {
                $data = $data[$key];
            }
        }
    }
    if (is_string($field) && $field) {
        if ($parse) {
            $fields = explode(".", $field);
        }
        
        if (!empty($fields) && count($fields) > 1) {
            $column = $fields[0];
            
            if (isset($data[$column]) && is_string($column) && $column) {
                $parsedjson = @json_decode($data[$column]);
                
                if ($parsedjson) {
                    array_shift($fields);
                    
                    $val = $parsedjson;
                    foreach ($fields as $name) {
                        if ($name && isset($val->$name)) {
                            $val = $val->$name;
                        } else {
                            $val = null;
                            break;
                        }
                    }
                    
                    return is_string($val) ? trim($val) : $val;
                }
            }
        } else {
            if (isset($data[$field])) {
                return is_string($data[$field]) ? trim($data[$field]) : $data[$field];
            }
        }
    }
    
    return null;
}


/**
 *
 * @param file format array
 * @return mixed
 */
function filesizer($bytes)
    {
        if ($bytes >= 1073741824)
        {
            $bytes = number_format($bytes / 1073741824, 2) . ' GB';
        }
        elseif ($bytes >= 1048576)
        {
            $bytes = number_format($bytes / 1048576, 2) . ' MB';
        }
        elseif ($bytes >= 1024)
        {
            $bytes = number_format($bytes / 1024, 2) . ' KB';
        }
        elseif ($bytes > 1)
        {
            $bytes = $bytes . ' bytes';
        }
        elseif ($bytes == 1)
        {
            $bytes = $bytes . ' byte';
        }
        else
        {
            $bytes = '0 bytes';
        }

        return $bytes;
}

/**
 *
 * @param  duration
 * @return mixed
 */
function duration($value) {
    $str = null;
    if(isset($value)) {
        $hours = intval($value / 60);
        $minutes = $value % 60;

        if ($hours != 0) {
            $str = $hours . ' '.App::translate('hr');

        }

        // Always show minutes if there are no hours.
        if ($minutes != 0 || $hours == 0) {
            $str .= ' ' . $minutes . ' '.App::translate('min');
        }

        // There will be a leading space if hours is zero.
        return trim($str);
    } else {
        return null;
    }
}

/**
 *
 * @param  Downloader
 * @return mixed
 */

function downloader($url, $filename)
{
    if (file_exists($filename)) {
        @unlink($filename);
    }
    $fp = fopen($filename, 'w');
    if ($fp) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $result = parse_url($url);
        curl_setopt($ch, CURLOPT_REFERER, $result['scheme'] . '://' . $result['host']);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
        $raw = curl_exec($ch);
        curl_close($ch);
        if ($raw) {
            fwrite($fp, $raw);
        }
        fclose($fp);
        if (!$raw) {
            @unlink($filename);
            return false;
        }
        return true;
    }
    return false;
}

/**
 *
 * @param delete directory
 * @return mixed
 */
function removefolder($dirname) {
         if (is_dir($dirname))
           $dir_handle = opendir($dirname);
     if (!$dir_handle)
          return false;
     while($file = readdir($dir_handle)) {
           if ($file != "." && $file != "..") {
                if (!is_dir($dirname."/".$file))
                     unlink($dirname."/".$file);
                else
                     removefolder($dirname.'/'.$file);
           }
     }
     closedir($dir_handle);
     rmdir($dirname);
     return true;
}

function webper($image = '') {
    $searchVal  = array("jpg", "jpeg", "png");
    $replaceVal = array("webp", "webp", "webp");
    return str_replace($searchVal, $replaceVal, $image);
}
/**
 *
 * @param  Word Limit
 * @return mixed
 */

function wordlimit($deger, $krktr = 80)
{
    $deger  = Input::cleaner($deger);
    $toplam = strlen($deger);
    if ($toplam > $krktr) {
        $deger = mb_substr($deger, 0, $krktr, 'UTF-8');
    }
    return $deger;
}

/**
 *
 * @param Hit view
 * @return mixed
 */
function hitview($n, $precision = 1) {
    if ($n < 900) {
        // 0 - 900
        $n_format = number_format($n, $precision);
        $suffix = '';
    }
    else if ($n < 900000) {
        // 0.9k-850k
        $n_format = number_format($n / 1000, $precision);
        $suffix = ' K';
    }
    else if ($n < 900000000) {
        // 0.9m-850m
        $n_format = number_format($n / 1000000, $precision);
        $suffix = ' M';
    }
    else if ($n < 900000000000) {
        // 0.9b-850b
        $n_format = number_format($n / 1000000000, $precision);
        $suffix = ' B';
    }
    else {
        // 0.9t+
        $n_format = number_format($n / 1000000000000, $precision);
        $suffix = ' T';
    } 
    
    if ( $precision > 0 ) {
        $dotzero = '.' . str_repeat( '0', $precision );
        $n_format = str_replace( $dotzero, '', $n_format );
    }

    return $n_format . $suffix.' '.App::translate('view');
}
/**
 *
 * @param Search array
 * @return mixed
 */
function searchForId($id,$key2=null, $array=null) {
   foreach ($array as $key => $val) {
       if ($val[$key2] == $id) {
           return $key;
       }
   }
   return null;
}
/**
 *
 * @param Minify
 * @return mixed
 */
function buffer($buffer){
    if(MINIFY == true) {
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        $buffer = str_replace(': ', ':', $buffer);
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    }
    return $buffer;
}

/**
 *
 * @param  Avatar
 * @return mixed
 */
function gravatar($Id = null, $Name = null, $Avatar = null, $Class = null, $Color = null)
{ 
    if ($Avatar) {
        return '<div class="bg-img-cover ' . $Class . '" style="background-image:url(' . UPLOAD . '/avatar/' . $Avatar . ');"></div>';
    } else {
        return '<div class="' . $Class . '" style="'.(isset($Color) ? 'background-color:'.$Color . ';' : '').'">' . mb_strtoupper(mb_substr($Name, 0, 1, "UTF-8"), "UTF-8") . '</div>';
    }
}

/**
 *
 * @param  iconer
 * @return mixed
 */
function iconer($Icon = null, $Class = null) {  
    return '<div class="' . $Class . '"><svg width="14" height="14" stroke="currentColor" stroke-width="1.75" fill="none"><use xlink:href="'.ASSETS.'/sprite/sprite.svg#'.$Icon.'"></use></svg></div>';
}

/**
 *
 * @param  preview image
 * @return mixed
 */
function picture($folder = null, $image = null, $class = null,$title = null,$size = null) {
    if(isset($size)) {
        $size = explode(',', $size);
    }
    if(empty($image)) {
    return '<picture>
        <img src="'.APP.'/image.php?size=200x200&bg=14171f&fg=999&text=Not Found" alt="'.$title.'" class="'.$class.'" width="'.$size[0].'" height="'.$size[1].'">
    </picture>';
    } else {
    return '<picture>
        <source data-srcset="' . UPLOAD . '/'.$folder.'/'.webper($image).'" type="image/webp" class="img-fluid" srcset="' . UPLOAD . '/'.$folder.'/'.webper($image).'">
        <source data-srcset="' . UPLOAD . '/'.$folder.'/'.$image.'" type="image/png" class="'.$class.'" srcset="' . UPLOAD . '/' . $folder . '/'.$image.'"> 
        <img data-src="' . UPLOAD . '/'.$folder.'/'.$image.'" alt="'.$title.'" class="lazyload '.$class.'" width="'.$size[0].'" height="'.$size[1].'">
    </picture>';

    }
}
function ranked($xp,$rank = null) {
    $Array = array();
    $index = 0;
    foreach ($rank as $key) {
        if($xp >= $key['xp']){
            $index++;
            $Array['id']        = $index;
            $Array['level']     = $key['level'];
            $Array['xp']        = $key['xp'];
            $Array['name']      = $key['name'];
        }
    }
    return $Array;
}

/**
 *
 * @param uploader
 * @return mixed
 */
function uploader($file = null, $name = null, $folder = null, $sizer = null,$format = null) {
    if(isset($sizer)) {
        $size = explode(',', $sizer);
    }

    $foo = new \Verot\Upload\Upload($file);
    if ($foo->uploaded) {
        $foo->allowed                   = array('image/jpg','image/jpeg','image/pjpeg','image/png','image/x-png','image/webp');
        $foo->file_auto_rename          = true;
        $foo->file_new_name_body        = $name;
        $foo->image_resize              = true;
        if(empty($size[1])) {
            $foo->image_ratio_y         = true;
        } else {
            $foo->image_ratio_crop      = true;
            $foo->image_y               = $size[1];
        }


        $foo->image_x               = $size[0];
        if(isset($format)) {
            $foo->image_convert         = $format;
        }
        $foo->jpeg_quality          = 100;
        $foo->webp_quality          = 100;
        $foo->Process($folder);
        return $foo->file_dst_name;
    }
}

/**
 *
 * @param tagger
 * @return mixed
 */
function tagger($dd,$decode = null) {
    $tr = array(" ");
    $eng = array("+");
    $dd = str_replace($tr,$eng,trim($dd));
    if(isset($decode)) {
        $dd = str_replace('+',' ',urldecode(trim($dd)));
    }
    return $dd;
}
/**
 *
 * @param random code
 * @return mixed
 */
function randomcode($input = null, $strength = 10) {
    $input = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }
 
    return $random_string;
}

/**
 *
 * @param ip address
 * @return mixed
 */
function ipaddress() {  
    if (!empty($_SERVER['HTTP_CLIENT_IP']))  {  
        $ip = $_SERVER['HTTP_CLIENT_IP'];  
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){  
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];  
    } else{  
        $ip = $_SERVER['REMOTE_ADDR'];  
    }  
    return $ip;  
}  

/**
 *
 * @param money
 * @return mixed
 */
function money($money = NULL)
{
    $money = @number_format($money, 2, ",", ".");
    return '$ '.$money;
}


/**
 *
 * @param  Dating
 * @return mixed
 */
function dating($str = null, $format = null)
{   
    if(isset($format) AND $format == 'year') {
        $str = date('Y', strtotime($str));
    } else {
        $calendar = array(
            'Monday'    => App::translate('Pazartesi'),
            'Tuesday'   => App::translate('Salı'),
            'Wednesday' => App::translate('Çarşamba'),
            'Thursday'  => App::translate('Perşembe'),
            'Friday'    => App::translate('Cuma'),
            'Saturday'  => App::translate('Cumartesi'),
            'Sunday'    => App::translate('Pazar'),
            'January'   => App::translate('Ocak'),
            'February'  => App::translate('Şubat'),
            'March'     => App::translate('Mart'),
            'April'     => App::translate('Nisan'),
            'May'       => App::translate('Mayıs'),
            'June'      => App::translate('Haziran'),
            'July'      => App::translate('Temmuz'),
            'August'    => App::translate('Ağustos'),
            'September' => App::translate('Eylül'),
            'October'   => App::translate('Ekim'),
            'November'  => App::translate('Kasım'),
            'December'  => App::translate('Aralık'),
            'Mon'       => App::translate('Pts'),
            'Tue'       => App::translate('Sal'),
            'Wed'       => App::translate('Çar'),
            'Thu'       => App::translate('Per'),
            'Fri'       => App::translate('Cum'),
            'Sat'       => App::translate('Cts'),
            'Sun'       => App::translate('Paz'),
            'Jan'       => App::translate('Oca'),
            'Feb'       => App::translate('Şub'),
            'Mar'       => App::translate('Mar'),
            'Apr'       => App::translate('Nis'),
            'May'       => App::translate('May'),
            'Jun'       => App::translate('Haz'),
            'Jul'       => App::translate('Tem'),
            'Aug'       => App::translate('Ağu'),
            'Sep'       => App::translate('Eyl'),
            'Oct'       => App::translate('Eki'),
            'Nov'       => App::translate('Kas'),
            'Dec'       => App::translate('Ara'),
        );
        $month = date('M', strtotime($str));
        $str = $calendar[$month].'. '.date('d', strtotime($str)).', '.date('Y', strtotime($str));
    }
    return $str;
}

/**
 *
 * @param Timeago
 * @return mixed
 */
function timeago($date)
{
    $timestamp = strtotime($date);
    
    $strTime = array(
        'sec',
        'min',
        'hour',
        'day',
        'month',
        'year'
    );
    $length  = array(
        "60",
        "60",
        "24",
        "30",
        "12",
        "10"
    );
    
    $currentTime = time();
    if ($currentTime >= $timestamp) {
        $diff = time() - $timestamp;
        for ($i = 0; $diff >= $length[$i] && $i < count($length) - 1; $i++) {
            $diff = $diff / $length[$i];
        }
        
        $diff = round($diff);
        return $diff . " " . $strTime[$i] . " ".'ago';
    }
}

/**
 *
 * @param  Ads
 * @return mixed
 */
function ads($data,$id = null,$class = null) {
    $ArrayId        = array_search($id, array_column($data, 'id'));
    $Row            = isset($data["$ArrayId"]) ? $data["$ArrayId"] : null;
    $html = null; 
    if(isset($Row['id'])) {
        $JsonData       = json_decode($Row['ads_data'], true);  
        if($Row['display_user'] == 2 || empty($_COOKIE[AUTH_NAME])) {
            if ($Row['type'] == 'image') {
                
                $html = '<a href="'.$JsonData['link'].'" rel="noreferrer" class="d-block '.$class.'" target="_blank" rel="nofollow"><img src="'.UPLOAD.'/ads/'.$JsonData['image'].'" alt="Ads" class="img-fluid rounded-3"></a>';
                
            } elseif ($Row['type'] == 'code') {
                $html = '<div class="'.$class.'">'.htmlspecialchars_decode($Row['ads_code']).'</div>';
            }
        }
    }
    return $html;

}

/**
 *
 * @param rating
 * @return mixed
 */
function rating($rating) {
    for ($i=1; $i <=5 ; $i++) { 
        if($i<=round($rating)) {
            echo '<i class="bg-warning"></i>';
        }else{
            echo '<i class="bg-gray-300"></i>';
        }
    }
}

/**
 *
 * @param games link
 * @return mixed
 */

function games($self = null) {
    return APP.'/'.App::translate('games').'/'.$self;
}
/**
 *
 * @param search link
 * @return mixed
 */

function search($self = null) {
    return APP.'/'.App::translate('search').'/'.$self;
}
/**
 *
 * @param Play link
 * @return mixed
 */
function play($id, $self = null) {
    return APP.'/'.App::translate('play').'/'.$self;
}

/**
 *
 * @param collection link
 * @return mixed
 */

function collection($id,$self = null) {
    return APP.'/'.App::translate('collection').'/'.$self.'-'.$id;
}

/**
 *
 * @param user link
 * @return mixed
 */

function user($id,$self = null) {
    return APP.'/'.App::translate('user').'/'.$self;
}
 
/**
 *
 * @param category link
 * @return mixed
 */

function category($id,$self = null) {
    return APP.'/'.App::translate('category').'/'.$self;
}
 
/**
 *
 * @param page link
 * @return mixed
 */

function page($id,$self = null) {
    return APP.'/'.App::translate('page').'/'.$self;
}

/**
 *
 * @param tag link
 * @return mixed
 */

function tag($self = null) {
    return APP.'/'.App::translate('tag').'/'.trim($self);
}