<?php
namespace App\Services;

use App\Models\ProductImage as ProductImageModel;
use App\Models\Product;
use Request;

class ProductImage {

    public static function imageUpload($relation, $model, $upload_type = 'product'){

        $route = "/";
        if($relation === 'product'){
            $route = route('admin_product_edit', [$model->id]);
            $storage_path = 'images/product/'.$model->id;

        }
        if($relation === 'user_wl_product'){
            $show = Request::get('show');
            $route = route('admin_lead_edit', [$model->user_id])."?show=".$show;
            $storage_path = 'images/user_product/'.$model->id;
        }
        
        try {
            $image = \App\Services\Slim::getImages('images')[0];

            if ( isset($image['output']['data']) )
            {
                // Base64 of the image
                $data = $image['output']['data'];
                $file_ex = array( 'image/jpeg' => 'jpg', 'image/png' => 'png');

                if (!isset($file_ex[$image['output']['type']])) {
                    \Session()->flash('alert-danger', 'File is not jpg or png!');
                    return redirect($route);
                }

                $ext = $file_ex[$image['output']['type']];
                // Original file name
                $name = $image['output']['name'];
                $name = \App\Services\Slim::sanitizeFileName($name);
                $name = uniqid() . '_' . $name;

                $data = \Storage::disk('public')->put(
                    $storage_path.'/'.$name,
                    $data
                );

                ProductImageModel::create([
                    'product_id' => $relation === 'product' ? $model->id : null,
                    'user_wl_product_id' => $relation === 'user_wl_product' ? $model->id : null,
                    'type' => $upload_type,
                    'filename' => $name,
                    'original_name' => $image['output']['name'],
                    'ext' => $ext,
                    'mine' => $image['output']['type'],
                    'size' => $image['input']['size']
                ]);


                \Session()->flash('alert-success', "Datei hochgeladen");
                return redirect($route);
            }
            \Session()->flash('alert-danger', "Datei leer");
            return redirect($route);

        }
        catch (\Exception $e) {
            \Session()->flash('alert-danger', "Fehler".$e);
            return redirect($route);
        }
    }

    public static function imageDelete($relation, $model, $product_image_id, $redirect = true){

        $route = "/";
        if($relation === 'product'){
            $route = route('admin_product_edit', [$model->id]);
            $storage_path = 'images/product/'.$model->id;

        }
        if($relation === 'user_wl_image'){
            $show = Request::get('show');
            $route = route('admin_lead_edit', [$model->user_id])."?show=".$show;
            $storage_path = 'images/user_wl_product/'.$model->id;
        }

        $product_image = ProductImageModel::findOrFail($product_image_id);
        if($product_image->product_id == $model->id || $product_image->user_wl_product_id == $model->id){
            $file =  $storage_path.'/'.$product_image->filename;
            \Storage::disk('public')->delete($file);
            $product_image->delete();
            \Session()->flash('alert-success', "Datei gelöscht");
            return $redirect ? redirect($route) : true;

        }
        \Session()->flash('alert-danger', "Datei nicht gefunden");
        return $redirect ? redirect($route) : true;

    }


}