<?php

//app/Helpers/Envato/User.php
//namespace App\Helpers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;

/**
 * @param int $user_id User-id
 * 
 * @return string
 */


if (!function_exists('pr')) {

    function pr($array = array()) {
        echo '<pre>';
        print_r($array);
        die;
    }

}

if (!function_exists('imgUpload')) {

    function imgUpload($rootFolderPath, $file) {
        $realPath = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();
        $mimeType = $file->getClientMimeType(); // $file->getMimeType();
        $fileName = md5(microtime()) . '.' . $extension;

        \File::makeDirectory($rootFolderPath, $mode = 0777, true, true);
        $img = Image::make($realPath);

        // store web file
        \File::makeDirectory($rootFolderPath . '/web', $mode = 0777, true, true);
        $img->resize(1024, 786, function ($constraint) {
            $constraint->aspectRatio();
        })->save($rootFolderPath . '/web/' . $fileName);

        // store api file
        \File::makeDirectory($rootFolderPath . '/api', $mode = 0777, true, true);
        $img->resize(1080, 676, function ($constraint) {
            $constraint->aspectRatio();
        })->save($rootFolderPath . '/api/' . $fileName);

        // store thumbnail file
        \File::makeDirectory($rootFolderPath . '/thumbnail', $mode = 0777, true, true);
        $img->resize(150, 100, function ($constraint) {
            $constraint->aspectRatio();
        })->save($rootFolderPath . '/thumbnail/' . $fileName);

        return $fileName;
    }
}

if (!function_exists('unlinkImg')) {
    function unlinkImg($rootFolderPath, $fileName) {
        if (!empty($fileName)) {
            if (file_exists($rootFolderPath . '/web/' . $fileName)) {
                unlink($rootFolderPath . '/web/' . $fileName);
            }
            if (file_exists($rootFolderPath . '/api/' . $fileName)) {
                unlink($rootFolderPath . '/api/' . $fileName);
            }
            if (file_exists($rootFolderPath . '/thumbnail/' . $fileName)) {
                unlink($rootFolderPath . '/thumbnail/' . $fileName);
            }
        }
    }
}


if (!function_exists('getImgPath')) {
    function getImgPath($folder, $fileName, $subFolder = 'web') {
        $name = 'storage/images/'.$folder.'/'.$subFolder.'/'.$fileName;
        return file_exists(public_path($name))?asset($name):'';
    }
}

if (!function_exists('getTrimmedValue')) {
    function getTrimmedValue($data)
    {
        if(is_array($data) && $data)
        {
            array_walk_recursive($data,function(&$v){$v=trim($v);});
            return $data;       // trim works on multi-dimension array
        }
        else if($data){
            return trim($data);
        }
        else{
            return $data;
        }
    }
}

if (!function_exists('objectToArray')) {
    function objectToArray($obj) {        
        return json_decode(json_encode($obj), true);
    }
}

