<?php

namespace App;

use function GuzzleHttp\json_decode;
use Shikiryu\LBCReposter\Categories;

class Deal
{
    /** @var Folder */
    protected $folder;
    /** @var array */
    protected $data;
    /** @var string[] */
    protected $images = [];
    /** @var int */
    protected $id;

    /**
     * Deal constructor.
     * @param $folder
     * @param int $id
     */
    public function __construct($folder, $id)
    {
        $this->folder = $folder;
        $this->id = $id;
        foreach (new \DirectoryIterator(sprintf('%s/%s/%s', config('app.deals_dire'), $folder, $id)) as $file) {
            if ($file->getExtension() === 'json') {
                $content = file_get_contents($file->getPathname());
                $this->data = json_decode($content, true);
            } elseif ($file->getExtension() === 'jpg') {
                $data = file_get_contents($file->getPathname());
                $base64 = 'data:image/jpg;base64,' . base64_encode($data);
                $this->images[] = $base64;
            }
        }

    }

    public function getDateCreation(): \DateTime
    {
        $date_creation = \DateTime::createFromFormat('Y-m-d H:i:s.u', $this->data['datecreation']['date']);
        return $date_creation;
    }
    /**
     * @return array
     */
    public function getData(): array
    {
        return $this->data;
    }

    /**
     * @return \string[]
     */
    public function getImages(): array
    {
        return $this->images;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getSubject(): string
    {
        return $this->data['subject'];
    }

    /**
     * @return string
     */
    public function getBody(): string
    {
        return $this->data['body'];
    }

    /**
     * @return float
     */
    public function getPrice(): float
    {
        return $this->data['price'];
    }

    public function getCategory(): string
    {
        return Categories::$categories[$this->data['category']];
    }

}