<?php

namespace App\Traits;
use Illuminate\Http\Request;
use App\Modules\RemoteStorage\GoogleBucketGateway;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

trait GoogleBucket
{

    // Note, there is a bug when you used GoogleBucketGateway in the traits construct so, 
    //To fix the issue the google bucket is now one of the parameters

    public $googleBucket;
    public function __construct(GoogleBucketGateway $googleBucket)
    {
        $this->googleBucket = $googleBucket;
    }

    public function uploadNewFile($payload, $filePath, $filePayloadName, $googleBucket)
    {   
        #1 step : save to local server first - temporarily save it on the server
        $temporaryFilePath = $payload->file($filePayloadName)->storeAs(
            '',  $filePath
        );

        #2: Upload file on the google bucket
        $fileSource = Storage::get($temporaryFilePath); //get the file on the tempary storage
        $savedProfile = $googleBucket->store($fileSource, $filePath); //send the file to google bucket

        #3 step : remove the file to the server
        if($savedProfile){
            $this->removeLocalFiles($filePath);
        }
    }

    public function createUniqueFileName($companyID, $extension){
        $uniqueString = date('Y_m_d_H_i_s') .  Str::random(5);
        $googleBucketLocation = 'files/worker/' . $companyID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/files/worker/'. $companyID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueJobseekerFileName($jobseekerID, $extension){
        $uniqueString = date('Y_m_d_H_i_s') .  Str::random(5);
        $googleBucketLocation = 'files/jobseeker/' . $jobseekerID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/files/jobseeker/'. $jobseekerID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueResumeNameWorker($companyID, $extension){
        $uniqueString = date('Y_m_d_H_i_s');
        $googleBucketLocation = 'files/resume/' . $companyID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/files/resume/'. $companyID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueImageNameWorker($companyID, $extension){
        $uniqueString = date('Y_m_d_H_i_s');
        $googleBucketLocation = 'images/profile/' . $companyID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/images/profile/'. $companyID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueResumeNameJobseeker($jobseekerID, $extension){
        $uniqueString = date('Y_m_d_H_i_s');
        $googleBucketLocation = 'files/resume/jobseeker/' . $jobseekerID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/files/resume/jobseeker/' . $jobseekerID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueImageNameJobseeker($jobseekerID, $extension){
        $uniqueString = date('Y_m_d_H_i_s');
        $googleBucketLocation = 'images/jobseeker/profile/' . $jobseekerID .'/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/images/jobseeker/profile/'. $jobseekerID .'/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function createUniqueImageNameAdmin($extension){
        $uniqueString = date('Y_m_d_H_i_s');
        $googleBucketLocation = 'images/admin/profile/'. $uniqueString .'.'. $extension; //use on live 
        if(env('APP_STATUS') == 'development'){
            $googleBucketLocation = 'dev/images/admin/profile/'. $uniqueString .'.'. $extension;  //just to separate the dev and live datas
        }
        return $googleBucketLocation; 
    }

    public function deleteUploadedFile($filePath, $googleBucket){
        if($filePath){ 
            return $googleBucket->removeFile($filePath);
        }
        return true;
    }

    public function uploadBase64Image($base64Image, $filePath, $googleBucket){
        #1 step : save to local server first - temporarily save it on the server
        Storage::put($filePath, base64_decode($base64Image));

        #2: Upload file on the google bucket
        $fileSource = Storage::get($filePath); //get the file on the tempary storage
        $savedProfile = $googleBucket->store($fileSource, $filePath); //send the file to google bucket

        #3 step : remove the file to the server
        if($savedProfile){
            $this->removeLocalFiles($filePath);
        }
    }

    public function removeLocalFiles($filePath){
        Storage::delete($filePath);
        Storage::deleteDirectory('files');
        Storage::deleteDirectory('images');
        Storage::deleteDirectory('dev/files'); //for dev purposes
        Storage::deleteDirectory('dev/images'); //for dev purposes
        return true;
    }

    public function removeUploadedFile($filePath, $googleBucket){
        if($filePath){ 
            return $googleBucket->removeFile($filePath);
        }
        return true;
    }
}