<?php

  if (!function_exists('getMediaUrl')) {
    /**
     * Generates thumb filename and url
     */

    function getMediaUrl($fileName = FALSE, $fileNameSize = '')
    {

      if (!$fileName) {
        return "";
      }
      if ($fileNameSize) {
        $path = Config::get('path.frontend.thumbs');

        return $path . '/' . getFileNameSize($fileName, $fileNameSize);
      } else {
        $path = Config::get('path.frontend.media');

        return $path . '/' . $fileName;
      }
    }
  }


  if (!function_exists('getMediaPublic')) {
    /**
     * Generates thumb filename and url
     */

    function getMediaPublic($fileName = FALSE, $fileNameSize = '')
    {

      if (!$fileName) {
        return "";
      }
      if ($fileNameSize) {
        $path = Config::get('path.frontend.thumbs');

        return $path . '/' . getFileNameSize($fileName, $fileNameSize);
      } else {
        $path = Config::get('path.frontend.img_public');

        return $path . '/' . $fileName;
      }
    }
  }


  if (!function_exists('getMediaPublicOriginal')) {
    /**
     * Generates thumb filename and url
     */

    function getMediaPublicOriginal($fileName = FALSE, $fileNameSize = '')
    {

      if (!$fileName) {
        return "";
      }
      if ($fileNameSize) {
        $path = Config::get('path.frontend.thumbs');

        return $path . '/' . getFileNameSize($fileName, $fileNameSize);
      } else {
        $path = Config::get('path.export.original');

        return $path . '/' . $fileName;
      }
    }
  }

  if (!function_exists('getMediaPublicTreated')) {
    /**
     * Generates thumb filename and url
     */

    function getMediaPublicTreated($fileName = FALSE, $fileNameSize = '')
    {

      if (!$fileName) {
        return "";
      }
      if ($fileNameSize) {
        $path = Config::get('path.frontend.thumbs');

        return $path . '/' . getFileNameSize($fileName, $fileNameSize);
      } else {
        $path = Config::get('path.export.treated');

        return $path . '/' . $fileName;
      }
    }
  }

  if (!function_exists('getFileNameSize')) {
    function getFileNameSize($fileName = '', $sizeName = '')
    {
      if (empty($fileName))
        return "";

      $pos = strrpos($fileName, ".");
      if (!empty($sizeName)) {
        if ($pos === FALSE) {
          return "${fileName}_{$sizeName}";
        } else {
          return substr($fileName, 0, $pos) . "_" . $sizeName . substr($fileName, $pos);
        }
      } else {
        return $fileName;
      }
    }
  }

  if (!function_exists('getMediaByID')) {
    /**
     * Generates thumb filename and url by ID
     */

    function getMediaByID($mediaID = '', $fileNameSize = '')
    {
      $filename = '';

      $mediaItem = MediaItem::find($mediaID);
      if (!empty($mediaItem->filename) && $mediaItem->filename != '') {
        $filename = getMediaUrl($mediaItem->filename, $fileNameSize);
      }

      return $filename;
    }
  }
