<?php
namespace App\Classes\General;

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

class ExcelHandel
{
	function __construct()
	{

	}

	public static function generateFolder($path = '')
    {
    	$folder = \Storage::disk('public')->makeDirectory($path);
        return $folder;
    }

	public static function insertExcel($path = '', $base64Set = [])
	{
    	$file_info = [];
        $folder_path = ExcelHandel::generateFolder('excels/' . $path);

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

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

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

	        $extension = 'xls';

	        switch ($mime_type) {
	            case 'application/vnd.ms-excel':
	                $extension = 'xls';
	                break;
	            case 'application/zip':
	                $extension = 'xlsx';
	                break;
	        }

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

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

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

	        $file = ExcelHandel::generateExcel($path, $exceldata, $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 generateExcel($path = '', $exceldata = '', $filename = '', $extension = '')
    {
        $file_info = [];
        $filesystemDriver = Storage::getDefaultDriver();
        if($filesystemDriver === 's3'){
            $filePathForS3 = 'public/excels/' . $path . '/' . $filename . '.' . $extension;
            Storage::disk('s3')->put($filePathForS3, $exceldata);

            $temporaryUrl = Storage::disk('s3')->temporaryUrl(
                $filePathForS3,
                Carbon::now()->addMinutes(10)
            );

            $file_info['original']['file'] = $temporaryUrl; //REMINDER: This is a path to AWS S3 URL (HTTPS) as a public anonymous user, NOT an internal system path
        }
        else{
            $file = Storage::disk('public')->put('excels/' . $path . '/' . $filename . '.' . $extension, $exceldata);
            $file_info['original']['file'] = storage_path('app/public/excels/' . $path . '/' . $filename . '.' . $extension);
        }

        return $file_info;
    }

    public static function removeExcel($excel_info = [])
    {
        $excel_info = json_decode(json_encode($excel_info), true);
        foreach ($excel_info as $key => $row) {
        	if (!empty($row['path'])) {
	            \File::delete([$row['file_info']['original']['file']]);
           }
        }
        return true;
    }

}
