<?php
namespace App\Http\Helpers;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;

class PdfHandel
{
	function __construct()
	{

	}

	public static function getPdfFullPath($video_info = [])
	{
		$video_info = json_decode($video_info);
		if (!empty($video_info)) {
			foreach ($video_info as $key_img => $row_img) {
				$video_info[$key_img]->file_info->original->file = asset($row_img->file_info->original->file);
			}
		}
		else {
			$video_info = [(object)[
				'empty' => true,
				'file_info' => (object)[
						'original' => (object) [
							'file' => asset('share/no-image.png')
						]
				]
			]];
		}
	    return $video_info;
	}

	public static function extractFullPath($file = '')
	{
		$file = explode(asset('/'), $file);
		return $file[1];
	}

	public static function generateFolder($set_path = '')
    {
        $folder = public_path() . $set_path;
        \File::isDirectory($folder) or \File::makeDirectory($folder, 0777, true, true); //check folder is exist
        return $folder;
    }

	public static function insertPdf($path = '', $base64Set = [])
	{
    	$file_info = [];
        $folder_path = PdfHandel::generateFolder('/share/images/' . $path);

        foreach ($base64Set as $key => $row) {

	    	$pdfdata = $row;
	        $filename = (string) Str::uuid();

	        $f = finfo_open();
	        $mime_type = finfo_file($f, $pdfdata, FILEINFO_MIME_TYPE);

	        $extension = 'pdf';
	        switch ($mime_type) {
	            case 'application/pdf':
	                $extension = 'pdf';
	                break;
	        }

	        if (empty($extension)) {
	            return false;
	        }

	        $pdfdata = explode('base64,', $pdfdata);
	        $pdfdata = base64_decode($pdfdata[1]);

	        $filename_with_ext = $filename . '.' . $extension;

	        $file = PdfHandel::generatePdf($path, $pdfdata, $filename, $extension);

	        $file_info[] = [
	            'path' => $path,
	            'filename' => $filename_with_ext,
	            'mime_type' => $mime_type,
	            'extension' => $extension,
	            'file_info' => empty($file) ? [] : $file,
	        ];
    	}

        return $file_info;
	}

	public static function generatePdf($path = '', $pdfdata = '', $filename = '', $extension = '')
    {
        $file_info = [];
	    $file = \File::put(public_path('share/images/' . $path) . '/' . $filename . '.' . $extension, $pdfdata);
        $file_info['original']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
        $file_info['large']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
        $file_info['medium']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
        $file_info['small']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
        
        return $file_info;
    }

    public static function removePdf($img_info = [])
    {
        foreach ($img_info as $key_img => $row_img) {
        	if (!empty($row_img->path)) {
	    		$img_original = PdfHandel::extractFullPath($row_img->file_info->original->file);
	            \File::delete([$img_original]);
           }
        }
        return true;
    }

}