<?php

namespace App\Aws;

use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as Image;
use File;
class FileSystem {

    public static function storeImage($fileName, $file, $destination, $destinationThumb = "", $width = 400, $height = 400) {
        $extension = $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();

        $fileName = $fileName;
        
        $path = public_path() . "/";
        $image = $path . $destination . '' . $fileName;
        
        $upload_success = $file->move($path . $destination, $fileName);

       if($extension!='svg'){
        if ($destinationThumb != "") {
            $upload = Image::make($image)->resize($width, $height)->save($path . $destinationThumb . $fileName);
        }
       }
        return $fileName;
    }

    public static function saveHotelImage($photo, $fileName, $destination, $destinationThumb = "", $width = 400, $height = 400, $session_id) {

        $path = public_path() . "/";
        $tmp = public_path() . "/tmp/" . $session_id . "/" . $photo['name'];
        $extension = pathinfo($photo['name'], PATHINFO_EXTENSION);
        $fileName = $fileName . '.' . $extension;
        $upload = Image::make($tmp)->save($path . $destination . $fileName);
        $upload = Image::make($tmp)->resize($width, $height)->save($path . $destinationThumb . $fileName);
        return $fileName;
    }

    public static function deleteImage($fileName, $destination, $destinationThumb = "") {

        if ($fileName != "") {
            if (Storage::has($destination . $fileName)) {
                Storage::delete($destination . $fileName);
            }

            if ($destinationThumb != "") {
                if (Storage::has($destinationThumb . $fileName)) {
                    Storage::delete($destinationThumb . $fileName);
                }
            }
        }
    }

    public static function copy($from, $to) {
        if (Storage::has($from)) {
            Storage::copy($from, $to);
        }
    }

    public static function saveAmenityImage($amenity, $fileName, $destination, $destinationThumb = "", $width = 400, $height = 400, $session_id) {

        $path = public_path() . "/";
        $tmp = public_path() . "/tmp/" . $session_id . "/" . $amenity['name'];
        $extension = pathinfo($amenity['name'], PATHINFO_EXTENSION);
        $fileName = $fileName . '.' . $extension;

//        //$image = $tmp;
        $upload = Image::make($tmp)->save($path . $destination . $fileName);
        $upload = Image::make($tmp)->resize($width, $height)->save($path . $destinationThumb . $fileName);
        return $fileName;
    }

    public static function storeFile($fileName, $file, $destination) {
        $extension = $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();

        $fileName = $fileName . '.' . $extension;
        $path = public_path() . "/";
        $image = $destination . '/' . $fileName;
        $upload_success = $file->move($path . $destination, $fileName);
        return $fileName;
    }

    public static function StoreVideo($fileName, $file, $destination) {
        $extension = $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();

        $fileName = $fileName . '.' . $extension;
        $path = public_path() . "/";
        //$path . $destination . $fileName;
        //Storage::put($path . $destination . $fileName, file_get_contents($realPath), 'public');
        //$fileName = rand(111, 999) . time() . '.' . $extension;
        $image = $destination . '/' . $fileName;
        $upload_success = $file->move($path . $destination, $fileName);



        return $fileName;
    }

    public static function storeTeamImage($fileName, $file, $destination, $destinationThumb = "", $width = 525, $height = 525) {
        $extension = $file->getClientOriginalExtension();
        $realPath = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();

        $fileName = $fileName . '.' . $extension;
        $path = public_path() . "/";
        //$path . $destination . $fileName;
        //Storage::put($path . $destination . $fileName, file_get_contents($realPath), 'public');
        //$fileName = rand(111, 999) . time() . '.' . $extension;
        $image = $destination . '/' . $fileName;
        $upload_success = $file->move($path . $destination, $fileName);


        if ($destinationThumb != "") {
            //$image = Image::make($realPath);
            //$upload = $image->resize($width, $height)->encode($extension);
            $upload = Image::make($image)->resize($width, $height)->save($path . $destinationThumb . $fileName);
            //Storage::put($path . $destinationThumb . $fileName, (string) $upload, 'public');
        }
        return $fileName;
    }
      public static function image_resize($fileName,$width, $height){
        
       
       
        $destination='uploads/media/';
        $path = public_path() . "/";
        $str_arr = explode (".", $fileName); 

        $fileNames = $str_arr[0].'-'.$width.'x'.$height.'.'.$str_arr[1];
        
        $image = $path . $destination . '' . $fileName;
        
//         $upload_success = $file->move($path . $destination, $fileName);
        
        $upload = Image::make($image)->resize($width, $height)->save($path . $destination.'thumbnails/' . $fileNames);
//        return $fileName->imageDetail;
        return $fileNames;
      

    }

}
