<?php

namespace App\Http\Controllers\Traits;

use App\Helpers\ImageSize;
use App\ResidentialComplex;
use Intervention\Image\Facades\Image;
use Spatie\MediaLibrary\Models\Media;

trait ImageUploadTrait
{
    private $extension = ['image/jpeg', 'image/png'];

    private function saveImagePlanning(string $img, string $folderPng, string $folderWebp, array $size, string $fileName, $file, $time)
    {
        if (empty($img) || $img == "https:/images/default_company.svg") {
            return null;
        }

        $pathPng = public_path(env('UPLOAD_PATH') . '/' . $folderPng);
        $pathWebp = public_path(env('UPLOAD_PATH') . '/' . $folderWebp);
        $thumbPath = public_path(env('UPLOAD_PATH') . '/thumb');
        $filename = $time . '-' . $fileName;
        if (!file_exists($pathPng) && !file_exists($pathWebp)) {
            mkdir($pathPng, 0775, true);
            mkdir($pathWebp, 0775, true);
        }

        $fileExtension = $this->fileExtension($file);
        if ($fileExtension == false) {
            return null;
        }

        $fileNameWithExtension = $filename . '.' . $fileExtension;

//        Image::make($file)->resize(ImageSize::THUMB_SIZE[0], ImageSize::THUMB_SIZE[1])
//            ->save($thumbPath . '/' . $fileNameWithExtension);

        if (count($size) > 0) {
            Image::make($file)->resize($size[0], $size[1])->save($pathPng . '/' . $fileNameWithExtension);
            Image::make($file)->encode('webp')->resize($size[0], $size[1])
                ->save($pathWebp . '/' . $fileNameWithExtension . ImageSize::IMAGE_WEBP);
        } else {
            Image::make($file)->save($pathPng . '/' . $fileNameWithExtension);
            Image::make($file)->encode('webp')
                ->save($pathWebp . '/' . $fileNameWithExtension . ImageSize::IMAGE_WEBP);
        }

        return $fileNameWithExtension;
    }

    private function saveImage(string $img, string $folderPng, string $folderWebp, array $size, string $fileName)
    {
        if (empty($img) || $img == "https:/images/default_company.svg") {
            return null;
        }

        try {
            $file = file_get_contents($img);
        } catch (\Exception $exception) {
            dump($img);
            dump('iamge was not exist');
            return null;
        }
        $file = file_get_contents($img);

        $pathPng = public_path(env('UPLOAD_PATH') . '/' . $folderPng);
        $pathWebp = public_path(env('UPLOAD_PATH') . '/' . $folderWebp);
        $thumbPath = public_path(env('UPLOAD_PATH') . '/thumb');
        $filename = time() . '-' . $fileName;
        if (!file_exists($pathPng) && !file_exists($pathWebp)) {
            mkdir($pathPng, 0775, true);
            mkdir($pathWebp, 0775, true);
        }

        $fileExtension = $this->fileExtension($file);
        if ($fileExtension == false) {
            return null;
        }

        $fileNameWithExtension = $filename . '.' . $fileExtension;

//        Image::make($file)->resize(ImageSize::THUMB_SIZE[0], ImageSize::THUMB_SIZE[1])
//            ->save($thumbPath . '/' . $fileNameWithExtension);

        if (count($size) > 0) {
            Image::make($file)->resize($size[0], $size[1])->save($pathPng . '/' . $fileNameWithExtension);
            Image::make($file)->encode('webp')->resize($size[0], $size[1])
                ->save($pathWebp . '/' . $fileNameWithExtension . ImageSize::IMAGE_WEBP);
        } else {
            Image::make($file)->save($pathPng . '/' . $fileNameWithExtension);
            Image::make($file)->encode('webp')
                ->save($pathWebp . '/' . $fileNameWithExtension . ImageSize::IMAGE_WEBP);
        }

        return $fileNameWithExtension;
    }

    public function saveImageCollection($model, $image, $collectionKey, $parameters, $link)
    {
        $model->exists = true;

        try {
            /** @var ResidentialComplex $model */
            $media = $model
                ->addMedia($image->basePath())
                ->toMediaCollection($collectionKey);

            $media->model_id = $model->id;
            $media->save();
        } catch (\Exception $e) {
            dump($e->getMessage());
            return null;
        }

        return $media;
    }

    public function fileExtension($file)
    {
        $mime = Image::make($file)->mime();
        $extensionExplode = explode('/', $mime);

        if (count($extensionExplode) > 0 && in_array($mime, $this->extension)) {
            return $extensionExplode[1];
        }

        return false;
    }

    public function imagePlanningSize($file)
    {
        $image = Image::make($file);

        $imageHeight = $image->height();
        $imageWidth = $image->width();
        $imageAspectRatio = $imageHeight / $imageWidth;

        $needWidth = ImageSize::PLANING_SIZE_BIG[0];
        $needHeight = ImageSize::PLANING_SIZE_BIG[1];
        $needAspectRatio = $needHeight / $needWidth;

        if ($needAspectRatio > $imageAspectRatio) {
            $widthAspectRatio = $needWidth;
            $heightAspectRatio = $imageHeight * $needWidth / $imageWidth;
        } else {
            $widthAspectRatio = $imageWidth * $needWidth / $imageHeight;
            $heightAspectRatio = $needHeight;
        }

        return [
            'height' => $heightAspectRatio,
            'width' => $widthAspectRatio
        ];
    }

    //    final function getIssetImg(string $img, array $cropXY = [])
//    {
//        $file = file_get_contents($img);
//        $testImage = Image::make($file)->save($developerLogo . '/' . 'developer-logo');
//
//        try {
//            $img = Image::make($img)->save();
//            $img->crop(($img->getWidth() - $cropXY[0]), ($img->getHeight() - $cropXY[1]), 0, 0);
//        } catch (\Exception $exception) {
//            $img = Image::make(public_path('adminlte/img/error/img-error-404.png'));
//        }
//
//        return $img;
//    }
}
