<?php

namespace App\Http\Livewire\Frontend\Pouzivatel\Notifications;

use App\Enums\UpozornenieStavEnum;
use App\Models\Pouzivatel\Pouzivatel;
use App\Notifications\Push\MAAISEKUserNotificationsCountPushNotification;
use Livewire\Component;
use Livewire\WithPagination;

/**
 * Class Zoznam
 */
final class Zoznam extends Component
{
    use WithPagination;

    private const PER_PAGE = 15;

    public int $sort = 0;

    public string $status;

    public bool $unreaded = false;

    protected string $paginationTheme = 'bootstrap';

    protected int $countAll = 0;

    protected int $countRead = 0;

    protected int $countNotRead = 0;

    /**
     * @var string[]
     */
    protected $listeners = [
        'filterByReadState',
    ];

    /**
     * @return void
     */
    public function mount()
    {
        $this->pouzivatel = auth()->user();
        $this->status = UpozornenieStavEnum::NOTREAD();
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function render()
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        $query = $pouzivatel->notifications();

        $this->countAll = $pouzivatel->notifications()->count();
        $this->countRead = $pouzivatel->readNotifications()->count();
        $this->countNotRead = $pouzivatel->unreadNotifications()->count();

        $this->unreaded = (bool) $this->countNotRead;

        if (UpozornenieStavEnum::make($this->status)->equals(UpozornenieStavEnum::NOTREAD())) {
            $query = $query->whereNull('read_at');
        }

        if (UpozornenieStavEnum::make($this->status)->equals(UpozornenieStavEnum::READ())) {
            $query = $query->whereNotNull('read_at');
        }

        $upozornenia = $query
            ->orderBy('created_at', $this->sort ? 'asc' : 'desc')
            ->paginate(self::PER_PAGE);

        return view('livewire.frontend.pouzivatel.notifications.zoznam')
            ->withUpozornenia($upozornenia);
    }

    /**
     * Oznacit vsetky neprecitane upozornenia ako precitane
     */
    public function notificationsReaded(): void
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        $pouzivatel->unreadNotifications->markAsRead();
        $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification);

        // udalost pre posluchacov inych aktivnych komponentov na aktualizaciu ich stavu
        $this->emit('userNotificationsReadChanged');
    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function markAsRead(string $uuid, string $url)
    {
        /** @var Pouzivatel $pouzivatel */
        $pouzivatel = auth()->user();
        /** @var \Illuminate\Notifications\DatabaseNotification $upozornenie */
        $upozornenie = $pouzivatel->notifications()->findOrFail($uuid);

        // oznacit upozornenie ako precitane ak este nie je precitane
        if ($upozornenie->read() === false) {
            $upozornenie->markAsRead();
            $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification);
        }

        // presmerovanie na zdroj upozornenia
        return redirect($url);
    }

    /**
     * Filtrovanie upozorneni podla stavu precitania upozornenia
     */
    public function filterByReading(string $state): void
    {
        $this->status = $state;
    }

    public function getCountByType(string $type): int
    {
        $stav = UpozornenieStavEnum::make($type);

        switch ($stav) {
            case UpozornenieStavEnum::ALL():
                return $this->countAll;
            case UpozornenieStavEnum::READ():
                return $this->countRead;
            case UpozornenieStavEnum::NOTREAD():
                return $this->countNotRead;
            default:
                return 0;
        }
    }
}
