<?php

declare(strict_types=1);

namespace Plugin\tnt_stabila_shop\src\Repositories;

use JTL\Cart\CartItem;
use JTL\Plugin\PluginInterface;
use Plugin\tnt_stabila_shop\src\Abstracts\AbstractSessionRepository;
use Plugin\tnt_stabila_shop\src\DomainObjects\PerformanceDTO;

class SessionRepository extends AbstractSessionRepository
{
    /**
     * @var SessionRepository
     */
    protected static SessionRepository $instance;

    public static function getInstance(): self
    {
        return self::$instance;
    }

    public function __construct(string $pluginID)
    {
        self::$instance = $this;
        parent::__construct($pluginID);
    }

    public function setLastEventTimeStamp(float $timeStamp): void
    {
        $this->set('lastEventTimeStamp', $timeStamp);
    }

    public function getLastEventTimeStamp(): float|null
    {
        return \is_float($this->get('lastEventTimeStamp'))
            ? $this->get('lastEventTimeStamp')
            : null;
    }

    public function setPerformance(PerformanceDTO $data): void
    {
        $this->set('stabila_performance', [$data]);
    }

    /**
     * @return PerformanceDTO[]
     */
    public function getPerformance(): array
    {
        return $this->get('stabila_performance') ?? [];
    }

    public function appendPerformance(PerformanceDTO $data): void
    {
        $result   = $this->getPerformance();
        $result[] = $data;
        $this->set('stabila_performance', $result);
    }

    public function unsetPerformance(): void
    {
        $this->unset('stabila_performance');
    }

    public function getCurrentLanguage(): string
    {
        return $_SESSION['cISOSprache'] ?? '';
    }

    public function getCurrentCurrency(): string
    {
        return $_SESSION['cWaehrungName'] ?? '';
    }

    public function getCartItemCount(): int
    {
        return (int)($_SESSION['Warenkorb']->PositionenArr ?? '0');
    }

    /**
     * @return CartItem[]
     */
    public function getCartItems(): array
    {
        $result = $_SESSION['Warenkorb']->PositionenArr ?? [];
        return \is_array($result) ? $result : [];
    }

    public function getActiveTemplate(): string
    {
        return $_SESSION['cTemplate'] ?? '';
    }

    public function getDeliveryCountry(): string
    {
        return $_SESSION['cLieferlandISO'] ?? '';
    }

    public function getInvoiceCountry(): string
    {
        return $_SESSION['Steuerland'] ?? '';
    }

    /**
     * @return list<string|int|float>
     */
    public function getTaxRates(): array
    {
        $result = $_SESSION['Steuersatz'] ?? [];
        if (\is_array($result) === false) {
            return [];
        }

        return \array_values(\array_filter(
            $result,
            static fn (mixed $taxRate): bool => \is_int($taxRate) || \is_float($taxRate) || \is_string($taxRate)
        ));
    }

    public function getUserBrowser(): string
    {
        return ($_SESSION['oBesucher']->cBrowser ?? '');
    }

    public function getUserID(): int
    {
        return (int)($_SESSION['oBesucher']->kBesucher ?? '0');
    }

    public function isBot(): bool
    {
        return (int)($_SESSION['oBesucher']->kBesucherBot ?? '0') > 0;
    }

    public function getUserIP(): string
    {
        return $_SESSION['oBesucher']->cIP ?? '';
    }

    public function setReferrer(string $referrer): void
    {
        $this->set('stabila_referrer', $referrer);
    }

    public function getReferrer(): string
    {
        return $this->get('stabila_referrer') ?? '';
    }
}
