<?php

namespace App\Traits;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Exception;
use Intervention\Image\ImageManager;

trait GeneralHelperTrait
{
    protected function sendMessageToClient($message, $email, $extra=null, $name=null){
        try{
            $mailData = array(
                'to' => $email,
                'text' => $message,
                'extra' => $extra,
                'name' => $name
            );

            Mail::send('emails.send-otp', $mailData, function ($message) use($mailData){
                $message->to($mailData['to'])->subject("OTP Token");
            });
            return array('success'=> true, 'message' => 'Message Sent Successfully');
        }catch (Exception $e){
            Log::error('Error while sending OTP', array('error' => $e->getMessage()));
            return array('success'=> false, 'error' => $e->getMessage());
        }
    }

    protected function uploadBase64File($fileData, $path="uploads/products/", $key=0){
        try {
            $dataInfo = explode(';', $fileData);
            $dataInfoType = explode('/', $dataInfo[0]);
            $ext = $dataInfoType[1];
            $requestFile = explode(',', $fileData);
            $base64Data = $requestFile[1];

            // Decode the base64 data into binary data
            $imageData = base64_decode($base64Data);

            $filename = time().$key . '.'.$ext;
            $filePath = public_path($path . $filename);

            // Save the image to the specified file path
            File::put($filePath, $imageData);

            return asset($path.$filename);
        }catch (Exception $ex){
            Log::error('File Upload Error', array('error' => $ex->getMessage()." Line#".$ex->getLine()));
            return "";
        }
    }

    protected function uploadFile($file, $path="uploads/products/"){
        try {
            $fileName = time().'-'.$file->getClientOriginalName();
            $file->move(public_path($path), $fileName);
            // $this->generateThumbnail(public_path($path), $fileName);
            return array(
                'path' => asset($path.$fileName),
                'ext' => $file->getClientOriginalExtension()
            );
        }catch (Exception $exception){
            Log::error("Error occurred while image upload", array('exception' => $exception->getMessage()." -- ".$exception->getLine()));
        }
        return  null;
    }

    // protected function generateThumbnail($filePath, $fileName){
       
    //     $image = ImageManager::imagick()->read($filePath.$fileName);

    //     // resize only image height to 200 pixel
    //     $image->resize(height: 200);
        
    //     // Save the thumbnail to a new file
    //     $image->save($filePath.'thumbnail-'.$fileName);
    // }


    protected function createUniqueSlug($title, $model, $slugField = 'slug') {
        $slug = Str::slug($title);
        $originalSlug = $slug;
    
        // Check if the slug exists in the database
        $i = 1;
        while ($model::where($slugField, $slug)->exists()) {
            // If the slug exists, append a number to the slug
            $slug = $originalSlug . '-' . $i;
            $i++;
        }
    
        return $slug;
    }

}
