<?php

namespace App\Repositories\Core;

class Medias
{
    public static function getImage($model, $conversion = 'normal', $collection = 'images')
    {
        $img = $model->getMedia($collection)->first();

        return $img ? $img->getUrl($conversion) : false;
    }

    public static function getImages($model)
    {
        if (! $model) {
            return false;
        }
        $model->getMedia();
        foreach ($model->media as $key => $media) {
            $model->media[$key]['url'] = $media->getUrl();
        }

        return $model->media;
    }

    public static function storeImages($model, $files)
    {
        if ($files) {
            foreach ($files as $file) {
                self::storeImage($model, $file);
            }
        }
    }

    public static function storeImage($model, $file, $collection = 'images')
    {
        return $model->addMedia($file)->sanitizingFileName(function ($fileName) {
            return str_replace(['#', '/', '\\', ' '], '-', $fileName);
        })->toMediaCollection($collection);
    }

    public static function deleteImages($model, $collection = 'images')
    {
        $ret = $model->clearMediaCollection($collection);

        return true;
    }

    public static function deleteImage($model, $index)
    {
        $model->getMedia();
        $ret = $model->media[$index]->delete();

        return true;
    }

    public static function buildURL($image, $conversion = '')
    {
        return self::getPath($image).self::getConversion($image, $conversion);
    }

    public static function getPath($image)
    {
        $model = basename(str_replace('\\', '/', $image->model_type));

        return '/storage/'.$model.'/'.$image->collection_name.'/'.$image->id;
    }

    public static function getConversion($image, $conversion = '')
    {
        // return $conversion ? '/conversions/' . $image->name . '-' . $conversion . self::getExtension($image->file_name) : $image->file_name;
        return $conversion ? '/conversions/'.$image->name.'-'.$conversion.'.jpg' : $image->file_name;
    }

    public static function getExtension($filename)
    {
        return '.'.pathinfo($filename, PATHINFO_EXTENSION);
    }

    public static function getImageSrc($image)
    {
        if (! $image) {
            return null;
        }
        $id = $image['id'];
        $filename = self::getFilename($image);

        return "/storage/$id/$filename";
    }

    public static function getThumbSrc($image)
    {
        return self::getSrcByType($image, 'thumb');
    }

    public static function getPreviewSrc($image)
    {
        return self::getSrcByType($image, 'preview');
    }

    public static function getNormalSrc($image)
    {
        return self::getSrcByType($image, 'normal');
    }

    public static function getSrcByType($image, $type)
    {
        return $image ? '/storage/'.$image['id'].'/conversions/'.self::getFilename($image, $type) : false;
    }

    public static function getFilename($image, $type = false)
    {
        $image['name'] = self::convertName($image['name']);

        return $image['name'].($type ? '-'.$type : '').self::getExtension($image['file_name']);
    }

    public static function convertName($name)
    {
        return str_replace(['#', '/', '\\', ' '], '-', $name);
    }
}
