<?php
namespace App\Helpers;

use Illuminate\Support\Str;
use ImageResize;

/**
* Class based Helper to upload and resize Images
*/
class ImageHelper
{

    /**
     * Helper to upload and resize Images
     * @param $image File to upload
     * @param null $destination
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function upload($image, $destination = null, $width= 200, $height= 200)
    {

        $newFileName = time().'_'. Str::random(20) . '.' .$image->extension();

        // Resize image and upload to Thumbnail path
        $destinationPath = public_path('/uploads/images/thumbs');

        if ($image->extension() != 'pdf'){
            $img = ImageResize::make($image->path());

            $img->resize($width, $height, function ($constraint) {
                $constraint->aspectRatio(); // to save the ratio
            })->save($destinationPath.'/'.$newFileName);
        }

        // upload file to  Original path
        if ($destination){
            $destinationPath = public_path('/uploads/images/'.$destination);
        } else {
            $destinationPath = public_path('/uploads/images');
        }
        $image->move($destinationPath, $newFileName);
        return $destination ? '/uploads/images/'.$destination.'/'.$newFileName : '/uploads/images/'.$newFileName;
    }

    /**
     * Class based Helper to resize Images
     * @param $image file to resize, string
     * @param int $width
     * @param int $height
     * @return string to resized image url
     */
    public static function resize($image, $width= 200, $height= 200): string
    {
        if (preg_match('/https:\/\/img\.youtube\.com/', $image, $match) ) {
            return $image;
        }

        $image= public_path($image);
        if (! is_file($image) ) {
            $defaultImage = settings('website_logo') ?? 'logo.png';
            return asset($defaultImage);
        }

        $imageInfo= pathinfo($image);

        $newFileName = $imageInfo['filename'].'-'.$width.'X'.$height.'.'.$imageInfo['extension'];

        // Resize image and upload to Thumbnail path
        // Thumbnail Path
        $upload_dir= '/uploads/images/thumbs';
        $destinationPath = public_path($upload_dir);
        // Save Image
        $img = ImageResize::make( $image );

        $img->resize($width, $height, function ($constraint) {
            $constraint->aspectRatio(); // to save the ratio
        })->save($destinationPath.'/'.$newFileName);

        return asset($upload_dir.'/'.$newFileName);
    }

    /**
     * Class based Helper to Check if Image exist
     * @param $image file
     * @return string image url
     */
    public static function get( $image ): string
    {
        if(!$image) {
            $defaultImage = settings('website_logo') ?? 'logo.png';
            return asset($defaultImage);
        }

        if (preg_match('/https:\/\/img\.youtube\.com/', $image, $match) ) {
            return $image;
        }

        $imagePath= public_path($image);

        if (is_file($imagePath)) {
            return asset($image);
        } else {
            $defaultImage = settings('website_logo') ?? 'logo.png';
            return asset($defaultImage);
        }
    }

    public static function getFullPath($file)
    {
        return request()->root().'/'.trim($file, '/');
        // .request()->getHttpHost().request()->getBaseUrl().'/'.trim($file, '/')
    }
}


