<?php

namespace App\Http\Controllers\Dashboard\Brand;

use App\Api\ImageResize;
use App\Models\Brand;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as ExController;

use App\Http\Requests\Dashboard\Brand\Update as UpdateRequest;
use App\Http\Requests\Dashboard\Brand\Store as StoreRequest;

use App\Jobs\Dashboard\Brand\Store as StoreJob;
use App\Jobs\Dashboard\Brand\Update as UpdateJob;
use Illuminate\Support\Facades\Storage;

class Controller extends ExController
{

    protected $brands;

    /**
     * Controller constructor.
     * @param Brend $brand
     */
    public function __construct(Brand $brand)
    {
        $this->brands = $brand;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index()
    {
        $this->authorize('view', 'brands');
        $brands = $this->brands->orderBy('position')->paginate(20);
        return view('dashboard.brands.index', compact('brands'));
    }

    /**
     * @param UpdateRequest $request
     * @param Brand $brand
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(UpdateRequest $request, Brand $brand)
    {
        if ($request->isMethod('get')) {
            $this->authorize('update', 'brands');
            return view('dashboard.brands.update', compact('brand'));
        }

        if ($request->hasFile('image')) {
            if (env('FILESYSTEM_DISK') == 's3') {
                // delete old image from s3
                Storage::disk('s3')->delete($brand->image);
                $path = $this->storeImageToS3($request->file('image'));
            } else {
                // detele old image
                if (is_file($brand->image)) {
                    unlink($brand->image);
                }
                $path =  $request->file('image')->store('uploads/brands');
            }
        } else {
            $path = $brand->image;
        }

        $this->dispatchSync(UpdateJob::fromRequest($brand, $request, $path));
        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.brands');
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(StoreRequest $request)
    {
        if ($request->isMethod('get')) {
            $this->authorize('create', 'brands');
            return view('dashboard.brands.store');
        }

        if ($request->hasFile('image')) {
            $file = $request->file('image');
            if (env('FILESYSTEM_DISK') == 's3') {
                $path = $this->storeImageToS3($file);
            } else {
                $path =  $file->store('uploads/brands');
            }
        }

        $this->dispatchSync(StoreJob::fromRequest($request, $path));
        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.brands');
    }

    /**
     * @param Brand $brand
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function delete(Brand $brand)
    {
        $this->authorize('delete', 'brands');

        if (is_file($brand->image)) {
            unlink($brand->image);
        }

        // delete image from s3
        if (env('FILESYSTEM_DISK') == 's3') {
            Storage::disk('s3')->delete($brand->image);
        }

        Product::where('brand_id', $brand->id)->withTrashed()->update([
            'brand_id' => null
        ]);

        $brand->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }

    private function storeImageToS3($image): string
    {
        $path = '';

        // first store temp file and resize it, then upload to s3
        // 1 - store temp file
        $tempPath = $image->store('temp', 'public');
        if ($tempPath) {
            $tempFilePath = storage_path('app/public/' . $tempPath);
            $image = new ImageResize();
            $path = $image->resize($tempPath, 322, 'brands');

            // 2 - upload to s3 or keep local
            if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
                Storage::disk('s3')->put($path, file_get_contents($path));
                unlink($path);
            }
        }

        return $path;
    }
}
