<?php
namespace AppBundle\Service;

use AppBundle\Entity\FewoLodgingImage;
use AppBundle\Entity\FewoLodgingGroupImage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;

class FileManager
{
    private $targetDir;

    public function __construct($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    const MAX_DEST_WIDTH = 848;
    const MAX_DEST_HEIGHT = 280;

    public function upload(UploadedFile $file)
    {
        $baseFileName = md5(uniqid());

        $fileName = $baseFileName.'.'.$file->guessExtension();

        $file->move($this->getTargetDir(), $fileName);
        $path = $this->getTargetDir() .'/'. $fileName;
        // Ermittle Breite, Höhe und Typ des Originalbildes
        $imgInfo = getimagesize($path);
        if ($imgInfo !== false)
        {
            $srcWidth = $imgInfo[0];
            $srcHeight = $imgInfo[1];
            switch ($imgInfo[2]) {
                case IMAGETYPE_GIF  : $src = imagecreatefromgif($path);  break;
                case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($path); break;
                case IMAGETYPE_PNG  : $src = imagecreatefrompng($path);  break;
                // default: // #TODO Warning
            }
            if (isset($src))
            {
                $destWidth = min(2 * self::MAX_DEST_WIDTH, $srcWidth);

                $destHeight = $destWidth * $srcHeight / $srcWidth;

                /*
                $destWidth = self::MAX_DEST_HEIGHT * $srcWidth / $srcHeight;
                if ($destWidth < self::MAX_DEST_WIDTH)
                {
                    $destWidth = self::MAX_DEST_WIDTH;
                    $destHeight = $destWidth * $srcHeight / $srcWidth;
                    $destX = 0;
                    $destY = ($destHeight - self::MAX_DEST_HEIGHT) / 2;
                }
                else
                {
                    $destHeight = self::MAX_DEST_HEIGHT;
                    $destX = ($destWidth - self::MAX_DEST_WIDTH) / 2;
                    $destY = 0;
                }
                */

                // Breite: MIN(2*848,$srcWidth); Höhe ergibt sich aus Seitenverhältnis d. Originalbildes
                $tmp = imagecreatetruecolor($destWidth, $destHeight);


                imagecopyresampled($tmp, $src, 0, 0, 0, 0,
                    $destWidth, $destHeight, $srcWidth, $srcHeight);

                $newFileName = md5(uniqid()) .'.jpg';
                if (imagejpeg($tmp, $this->getTargetDir() .'/'. $newFileName, 80))
                {
                    $filesystem = new Filesystem();
                    $filesystem->remove($this->getTargetDir() .'/'. $fileName);
                    return $newFileName;
                }
                // else #TODO warn
            }
        }

        return $fileName;
    }
    public function delete(FewoLodgingImage $image)
    {
        $filesystem = new Filesystem();

        $filesystem->remove($this->getTargetDir().'/'.$image->getFile());
    }

     public function deleteGroup(FewoLodgingGroupImage $image)
    {
        $filesystem = new Filesystem();

        $filesystem->remove($this->getTargetDir().'/'.$image->getFile());
    }

    public function getTargetDir()
    {
        return $this->targetDir;
    }
}