<?php

declare(strict_types=1);

namespace KupShop\FeedsBundle\Wrapper;

use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Contexts;

class LabelWrapper extends BaseWrapper
{
    /** ID */
    public function field_id(): int
    {
        return $this->object['id'];
    }

    /** Název */
    public function field_name(): string
    {
        return $this->object['name'];
    }

    /** Krátký název */
    public function field_name_short(): ?string
    {
        return $this->object['short_name'];
    }

    /**
     * Aktivní.
     *
     * Y - ano
     * N - ne
     */
    public function field_active(): string
    {
        return $this->object['active'];
    }

    /**
     * Porovnává datumy: zobrazit od, zobrazit do a určuje, jestli je štítek validní.
     * Y - ano
     *  N - ne.
     */
    public function field_is_valid(): string
    {
        $valid = true;

        // Kontrola jestli je štítek aktivní
        if ($this->object['active'] === 'N') {
            $valid = false;
        }

        // Kontrola jestli štítek splňuje podmínky na datum
        if ($valid && (!empty($this->object['date_from']) || !empty($this->object['date_to']))) {
            $today = strtotime(date('Y-m-d H:i:s'));

            $dateFrom = !empty($this->object['date_from']) ? strtotime($this->object['date_from']) : null;
            $dateTo = !empty($this->object['date_to']) ? strtotime($this->object['date_to']) : null;

            $valid = false;
            if ((!$dateFrom || $today >= $dateFrom) && (!$dateTo || $today <= $dateTo)) {
                $valid = true;
            }
        }

        // Kontrola jestli je štítek zobrazen na detailu
        if ($this->object['data']['detail_visibility'] != 'Y') {
            $valid = false;
        }

        // Kontrola jestli uživatel splňuje nastavené skupiny
        if ($user = Contexts::get(UserContext::class)->getActive()) {
            $hasUserLabelGroup = !empty(array_intersect($this->object['data']['users_groups'], array_keys($user->getGroups() ?? [])));
            $invertSelect = $this->object['data']['users_groups_invert'] ?? false;

            if (($invertSelect && $hasUserLabelGroup) || (!$invertSelect && !$hasUserLabelGroup)) {
                $valid = false;
            }
        }

        return $valid ? 'Y' : 'N';
    }

    /** Zobrazovat od */
    public function field_date_from(): ?string
    {
        return $this->object['date_from'];
    }

    /** Zobrazovat do */
    public function field_date_to(): ?string
    {
        return $this->object['date_to'];
    }

    public function field_coupon(): ?string
    {
        foreach ($this->object['data']['order_discounts'] ?? [] as $orderDiscount) {
            foreach ($orderDiscount['coupons'] ?? [] as $coupon) {
                return $coupon;
            }
        }

        return '';
    }

    public function field_coupon_discount(): ?float
    {
        foreach ($this->object['data']['order_discounts'] ?? [] as $orderDiscount) {
            foreach ($orderDiscount['discounts'] ?? [] as $discount) {
                return floatval($discount);
            }
        }

        return null;
    }
}
