<?php

class DModelToolImage extends ResizeImg
{


  public function __construct($registry)
  {
    parent::__construct($registry);

    $this->setFolder('image');
    $this->setDirpath(DIR_IMAGE);

  }


  public function resizeWebp($filename, $width, $height, $type = "")
  {

    $newimage = parent::resize($filename, $width, $height, $type, false);

    if (is_null($newimage)) {
      return;
    }

    $webp = utf8_substr($newimage, 0, utf8_strrpos($newimage, '.')) . '.webp';
    $newwebp = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.webp';

    if (!file_exists(DIR_IMAGE . $webp) || (filemtime($this->getDirpath() . $filename) > filemtime($this->getDirpath() . $newwebp))) {
      $image = new Imagick();
      $image->readImage($this->getDirpath() . $newimage);
      $image->setImageCompression(Imagick::COMPRESSION_JPEG);
      $image->setImageColorspace(Imagick::COLORSPACE_SRGB);
      $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
      $image->setSamplingFactors(array('2x2', '1x1', '1x1'));
      $image->setImageDepth(8);
      $image->stripImage();
      $image->optimizeImageLayers();
      $image->writeImage($this->getDirpath() . $webp);
      $image->destroy();
    }

    return array(
      'jpg' => $this->url->link('', '', 'SSL', false) . 'image/' . $newimage,
      'webp' => $this->url->link('', '', 'SSL', false) . 'image/' . $webp
    );


  }

  public function resizeProductImageWebp($filename, $width, $height, $type = "")
  {

      $newimage = parent::resizeWithWatermark($filename, $width, $height, $type, false);

      if (is_null($newimage)) {
          return;
      }

      $config_watermark = $this->config->get('config_watermark');
      if ($config_watermark) {
          $cache_dir = 'cache/watermark/';
      } else {
          $cache_dir = 'cache/';
      }

      $webp = utf8_substr($newimage, 0, utf8_strrpos($newimage, '.')) . '.webp';
      $newwebp = $cache_dir . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.webp';

      $newwebp_cache_date = file_exists($this->getDirpath() . $newwebp) ? filemtime($this->getDirpath() . $newwebp) : 0;
      $config_watermark_date = $this->config->get('config_watermark_date');
      $config_watermark_date = $config_watermark_date ? DateTime::createFromFormat('d/m/Y H:i:s', $config_watermark_date) : false;
      $config_watermark_date = $config_watermark_date ? $config_watermark_date->getTimestamp() : false;
      $update_watermark = ($config_watermark_date) && ($newwebp_cache_date < $config_watermark_date);

      if (!file_exists(DIR_IMAGE . $webp) || (filemtime($this->getDirpath() . $filename) > filemtime($this->getDirpath() . $newwebp))
          || $update_watermark) {

          $image = new Imagick();
          $image->readImage($this->getDirpath() . $newimage);
          $image->setImageCompression(Imagick::COMPRESSION_JPEG);
          $image->setImageColorspace(Imagick::COLORSPACE_SRGB);
          $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
          $image->setSamplingFactors(array('2x2', '1x1', '1x1'));
          $image->setImageDepth(8);
          $image->stripImage();
          $image->optimizeImageLayers();
          $image->writeImage($this->getDirpath() . $webp);
          $image->destroy();
      }

      return array(
          'jpg' => $this->url->link('', '', 'SSL', false) . 'image/' . $newimage,
          'webp' => $this->url->link('', '', 'SSL', false) . 'image/' . $webp
      );
  }

  public function proportionatelyResize($image_url, $min_size, $webp = false)
  {

    if (!file_exists($this->getDirpath() . $image_url) || !is_file($this->getDirpath() . $image_url)) {
      return;
    }

    $image_info = getimagesize($this->getDirpath() . $image_url);
    $width = $image_info[0];
    $height = $image_info[1];

    if ($width > $height) {
      $new_height = $min_size;
      $multiplier_factor = $width / $height;
      $new_width = round($new_height * $multiplier_factor);
    } else {
      $new_width = $min_size;
      $multiplier_factor = $height / $width;
      $new_height = round($new_width * $multiplier_factor);
    }

    if ($webp) {
      return $this->resizeWebp($image_url, $new_width, $new_height);
    } else {
      return $this->resize($image_url, $new_width, $new_height);
    }

  }

    public function getImageLink($imagepath)
    // essa função foi criada pra pegar a imagem original de forma simples, porque usa em classes que processam muitos produtos em foreach,
    // por exemplo: google base, mante-la simples
    {
        return $this->urlloja->link('', '', 'SSL', false) . $this->getFolder() . '/' . $imagepath;
    }

}
