<?php

namespace App\Http\Requests\Dashboard\Product;

use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Storage;
use App\Api\ImageResize;

class Store extends FormRequest
{

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->isMethod('get')) {
            return [];
        }

        return [
            'name' => 'array',
            'name.*' => 'required',

            'body' => 'array',
            'body.*' => 'required',

            'short_body' => 'array',
            'short_body.*' => 'required|max:300',

            'price' => 'required|numeric|gt:price_discount',
            'price_discount' => 'nullable',
            'poster' => 'required|mimes:jpeg,png,jpg',

            'brand_id' => 'required',
            'measurement_id' => 'nullable|exists:measurements,id',

            'category_id' => 'required',
            'colors' => 'array|required',
            'colors.*.color_id' => 'nullable',
            'colors.*.sizes' => 'nullable|array',
            'colors.*.sizes.*.name' => 'required',
            'colors.*.sizes.*.count' => 'required',

            'colors.*.screens' => 'array|required',
            'colors.*.screens.*.image' => 'required|mimes:jpeg,png,jpg',
            "calc" => [],


            'popular' => 'nullable',
            'leader_of_sales' => 'nullable',
            'article_number' => 'required|unique:products,article_number'
        ];
    }

    /**
     * @return mixed
     */
    public function getColors()
    {
        return $this->get('colors');
    }

    /**
     * @return string
     */
    public function getPoster(): string
    {
        $folder = "uploads/posters/" . Carbon::now()->format('Y/m/d');

        return (string) $this->file('poster')->store($folder);
    }

    /**
     * @return string
     */
    public function getCalc(): string|null
    {
        if ($this->hasFile('calc')) {
            $folder = "uploads/calc/" . Carbon::now()->format('Y/m/d');
            return (string) $this->file('calc')->store($folder);
        }
        return null;
    }

    public function getDataSheet()
    {
        if ($this->hasFile('data_sheet')) {
            $folder = "uploads/datasheet/" . Carbon::now()->format('Y/m/d');
            return (string) $this->file('data_sheet')->store($folder);
        }

        return null;
    }

    /**
     * @return string
     */
    public function getPosterThumb()
    {
        $url = $this->storeImageToS3($this->file('poster'));

        return $url;
    }

    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, 'posters');

            // 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;
    }

    /**
     * @return array
     */
    public function getName(): array
    {
        return $this->get('name');
    }

    /**
     * @return array
     */
    public function getBody(): array
    {
        return $this->get('body');
    }

    public function getPrice()
    {
        return $this->get('price');
    }

    public function getPriceDiscount()
    {
        if ($this->get('price_discount') > 0) {
            return $this->get('price_discount');
        }

        return 0;
    }

    public function getPriceCredit()
    {
        if ($this->get('price_credit') > 0) {
            return $this->get('price_credit');
        }

        return null;
    }

    /**
     * @return mixed
     */
    public function getCategoryID()
    {
        return $this->get('category_id');
    }

    /**
     * @return string
     */
    public function getSlug(): string
    {
        if ($this->get('slug') != 'null') {
            return (string) str_slug($this->get('slug'));
        }

        return (string) str_slug($this->get('name')['ru']);
    }

    /**
     * @return bool
     */
    public function getPublished()
    {
        if ($this->get('published') == 'true') {
            return true;
        }

        return false;
    }

    /**
     * @return bool
     */
    public function getAvailable()
    {
        if ($this->get('available') == 'true') {
            return true;
        }

        return false;
    }

    /**
     * @return int|null
     */
    public function getBrandID()
    {
        if ($this->get('brand_id') != 0) {
            return (int) $this->get('brand_id');
        }

        return null;
    }

    public function getMeasurementId()
    {
        if ($this->get('measurement_id') != 0) {
            return (int) $this->get('measurement_id');
        }

        return null;
    }

    public function getPopular(): bool
    {
        return $this->get('popular');
    }

    public function getLeaderOfSales(): bool
    {
        if ($this->has('leader_of_sales')) {
            if ($this->get('leader_of_sales') == 'true') {
                return true;
            }
        }

        return false;
    }
}
