<?php

namespace App\Http\Controllers\Traits;

use App\Helpers\ImageSize;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

trait ParseFileUploadTrait
{
    /**
     * File upload trait used in controllers to upload files
     * @param Request $request
     * @param null $parameters
     * @return Request
     */
    public function saveFiles($file, $parameters = null)
    {
//        $uploadPath = public_path(env('UPLOAD_PATH'));
//        $thumbPath = public_path(env('UPLOAD_PATH') . '/thumb');
//        $twoOnTwo = public_path(env('UPLOAD_PATH') . '/image_200_200');
//
//        if (isset($parameters['developer'])) {
//            $developerLogo = public_path(env('UPLOAD_PATH') . '/' . ImageSize::DEVELOPER_LOGO);
//            $developerLogoWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::DEVELOPER_LOGO_WEBP);
//            $developerBanner = public_path(env('UPLOAD_PATH') . '/' . ImageSize::DEVELOPER_BANNER);
//            $developerBannerWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::DEVELOPER_BANNER_WEBP);
//        }
//
//        if (isset($parameters['promotion'])) {
//            $promotionLogo = public_path(env('UPLOAD_PATH') . '/' . ImageSize::PROMOTION);
//            $promotionLogoWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::PROMOTION_WEBP);
//        }
//
//        if (isset($parameters['planning'])) {
//            $planning = public_path(env('UPLOAD_PATH') . '/' . ImageSize::PLANNING);
//            $planningWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::PLANNING_WEBP);
//        }
//
//        if (isset($parameters['bank'])) {
//            $bank = public_path(env('UPLOAD_PATH') . '/' . ImageSize::BANK);
//            $bankWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::BANK_WEBP);
//        }
//
//        if (isset($parameters['banner'])) {
//            $banner = public_path(env('UPLOAD_PATH') . '/' . ImageSize::BANNER);
//            $bannerWEBP = public_path(env('UPLOAD_PATH') . '/' . ImageSize::BANNER_WEBP);
//        }
//
//        if (isset($parameters[ImageSize::MARKER_TYPE])) {
//            $markerType = public_path(env('UPLOAD_PATH') . '/' . ImageSize::MARKER_TYPE);
//        }
//
//        if (!file_exists($uploadPath)) {
//            mkdir($uploadPath, 0775);
//            mkdir($thumbPath, 0775);
//        }
//
//
//                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
//                    $file = $request->file($key);
//                    $image = Image::make($file);
//                    $width = $image->width();
//                    $height = $image->height();
//                    if (!file_exists($thumbPath)) {
//                        mkdir($thumbPath, 0775, true);
//                    }
//                    if (!file_exists($twoOnTwo)) {
//                        mkdir($twoOnTwo, 0775, true);
//                    }
//
//                    if (isset($developerLogoWEBP) && !file_exists($developerLogoWEBP)) {
//                        mkdir($developerLogoWEBP, 0775, true);
//                    }
//
//                    if (isset($developerLogo) && !file_exists($developerLogo)) {
//                        mkdir($developerLogo, 0775, true);
//                    }
//
//                    if (isset($developerBanner) && !file_exists($developerBanner)) {
//                        mkdir($developerBanner, 0775, true);
//                    }
//
//                    if (isset($developerBannerWEBP) && !file_exists($developerBannerWEBP)) {
//                        mkdir($developerBannerWEBP, 0775, true);
//                    }
//
//                    if ((isset($promotionLogo) || isset($promotionLogoWEBP)) &&
//                        (!file_exists($promotionLogo) || !file_exists($promotionLogoWEBP))) {
//                        mkdir($promotionLogo, 0775, true);
//                        mkdir($promotionLogoWEBP, 0775, true);
//
//                    }
//
//                    if ((isset($planning) || isset($planningWEBP)) &&
//                        (!file_exists($planning) || !file_exists($planningWEBP))) {
//                        mkdir($planning, 0775, true);
//                        mkdir($planningWEBP, 0775, true);
//                    }
//
//                    if ((isset($bank) || isset($bankWEBP)) &&
//                        (!file_exists($bank) || !file_exists($bankWEBP))) {
//                        mkdir($bank, 0775, true);
//                        mkdir($bankWEBP, 0775, true);
//                    }
//
//                    if ((isset($banner) || isset($bannerWEBP)) &&
//                        (!file_exists($banner) || !file_exists($bannerWEBP))) {
//                        mkdir($banner, 0775, true);
//                        mkdir($bannerWEBP, 0775, true);
//                    }
//
//                    if ((isset($markerType) && !file_exists($markerType))) {
//                        mkdir($markerType, 0775, true);
//                    }
//
//
//                    if (isset($parameters['developer'])) {
//                        if ($key == 'logotype_icon') {
//                            if ($width > 191 && $height > 155) {
//                                Image::make($file)->resize(191, 155)->save($developerLogo . '/' . $filename);
//                                $webp = Image::make($file)->encode('webp')->resize(191, 155)
//                                    ->save($developerLogoWEBP . '/' . $filename . '.webp');
//                            } else {
//                                Image::make($file)->save($developerLogo . '/' . $filename);
//                                $webp = Image::make($file)->encode('webp');
//                                $webp->save($developerLogoWEBP . '/' . $filename . '.webp');
//                            }
//                        } elseif ($key == 'banner') {
//                            Image::make($file)->resize(1920, 607)->save($developerBanner . '/' . $filename);
//                            $webp = Image::make($file)->encode('webp');
//                            $webp->save($developerBannerWEBP . '/' . $filename . '.webp');
//                        }
//                    }
//
//                    if (isset($parameters['promotion'])) {
//                            Image::make($file)->resize(555, 428)->save($promotionLogo . '/' . $filename);
//                            Image::make($file)->encode('webp')->resize(555, 428)
//                                ->save($promotionLogoWEBP . '/' . $filename . '.webp');
//                    }
//
//                    if (isset($parameters['planning'])) {
//                        Image::make($file)->resize(207, 255, function ($constraint) {
//                            $constraint->aspectRatio();
//                        })->save($planning . '/' . $filename);
//
//                        Image::make($file)->encode('webp')->resize(207, 255, function ($constraint) {
//                            $constraint->aspectRatio();
//                        })
//                        ->save($planningWEBP . '/' . $filename . '.webp');
//                    }
//
//                    if (isset($parameters['bank'])) {
//                        Image::make($file)->resize(159, 159, function ($constraint) {
//                            $constraint->aspectRatio();
//                        })->save($bank . '/' . $filename);
//
//                        Image::make($file)->encode('webp')->resize(159, 159, function ($constraint) {
//                            $constraint->aspectRatio();
//                        })
//                        ->save($bankWEBP . '/' . $filename . '.webp');
//                    }
//
//                    if (isset($parameters['banner'])) {
//                        Image::make($file)->save($banner . '/' . $filename);
//                        Image::make($file)->encode('webp')->save($bannerWEBP . '/' . $filename . '.webp');
//                    }
//
//                    if (isset($parameters[ImageSize::MARKER_TYPE])) {
//                        Image::make($file)->save($markerType . '/' . $filename);
//                    }
//
//                    // resize 100 on 100
//                    Image::make($file)->resize(100, 100)->save($thumbPath . '/' . $filename);
//                    // resize 166 on 200
//                    Image::make($file)->resize(166, 200)->save($twoOnTwo . '/' . $filename);
//
//                    if ($width > $request->{$key . '_max_width'} && $height > $request->{$key . '_max_height'}) {
//                        $image->resize($request->{$key . '_max_width'}, $request->{$key . '_max_height'});
//                    } elseif ($width > $request->{$key . '_max_width'}) {
//                        $image->resize($request->{$key . '_max_width'}, null, function ($constraint) {
//                            $constraint->aspectRatio();
//                        });
//                    } elseif ($height > $request->{$key . '_max_height'}) {
//                        $image->resize(null, $request->{$key . '_max_height'}, function ($constraint) {
//                            $constraint->aspectRatio();
//                        });
//                    }
//                    $image->save($uploadPath . '/' . $filename);
//                    $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
//                } else {
//                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
//                    $request->file($key)->move($uploadPath, $filename);
//                    $finalRequest = new Request(array_merge($finalRequest->all(), [$key => $filename]));
//                }
//            }
//        }
//
//        return $finalRequest;
    }
}
