<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\Notification;

class NotificationDto
{
    public static string $TYPE_JOIN_LEAGUE = 'joinLeague';
    public static string $TYPE_JOIN_TEAM = 'joinTeam';
    public static string $TYPE_CAPTAIN_REQUEST = 'captainRequest';

    public int $id;
    public string $type;
    public string $message;

    public int $teamId;
    public int $leagueId;
    public int $userWhoFiredEventId;
    public int $userToNotifyId;
    public bool $isRead;
    public \DateTimeImmutable $readAt;
    public \DateTimeImmutable $createdAt;

    public function toArray(): array
    {
        return [
            'id' => $this->id ?? '',
            'type' => $this->type ?? '',
            'message' => $this->message ?? '',
            'teamId' => $this->teamId ?? '',
            'leagueId' => $this->leagueId ?? '',
            'isRead' => $this->isRead ?? false,
            'readAt' => !empty($this->readAt) ? $this->readAt->format('Y-m-d H:i:s') : '',
            'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : ''
        ];
    }

    public function fillFromArray(array $dataList): void
    {
        if (isset($dataList['id']))
        {
            $this->id = $dataList['id'];
        }
        if (!empty($dataList['type']))
        {
            $this->type = match($dataList['type']) {
                self::$TYPE_JOIN_LEAGUE => 'JOIN_LEAGUE',
                self::$TYPE_JOIN_TEAM => 'JOIN_TEAM',
                self::$TYPE_CAPTAIN_REQUEST => 'CAPTAIN_REQUEST',
                default => null
            };
        }
        if (!empty($dataList['message']))
        {
            $this->message = $dataList['message'];
        }
        if (!empty($dataList['isRead']))
        {
            $this->isRead = $dataList['isRead'];
        }
        if (!empty($dataList['readAt']))
        {
            $this->readAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dataList['readAt']);
        }
        if (!empty($dataList['createdAt']))
        {
            $this->createdAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dataList['createdAt']);
        }
        if (isset($dataList['teamId']))
        {
            $this->teamId = (int) $dataList['teamId'];
        }
        if (isset($dataList['userWhoFiredEventId']))
        {
            $this->userWhoFiredEventId = $dataList['userWhoFiredEventId'];
        }
        if (isset($dataList['userToNotifyId']))
        {
            $this->userToNotifyId = $dataList['userToNotifyId'];
        }
        if (isset($dataList['leagueId']))
        {
            $this->leagueId = $dataList['leagueId'];
        }
    }

    public function fillFromObj(Notification $notificationObj): self
    {
        if ($notificationObj->getId() !== null)
        {
            $this->id = $notificationObj->getId();
        }
        if ($notificationObj->getType() !== null)
        {
            $this->type = $notificationObj->getType();
        }
        if ($notificationObj->getMessage() !== null)
        {
            $this->message = $notificationObj->getMessage();
        }
        if ($notificationObj->getUserWhoFiredEvent() !== null)
        {
            $this->userWhoFiredEventId = ($notificationObj->getUserWhoFiredEvent())->getId();
        }
        if ($notificationObj->getUserToNotify() !== null)
        {
            $this->userToNotifyId = ($notificationObj->getUserToNotify())->getId();
        }
        if ($notificationObj->getLeague() !== null)
        {
            $this->leagueId = ($notificationObj->getLeague())->getId();
        }
        if ($notificationObj->getTeamId() !== null)
        {
            $this->teamId = $notificationObj->getTeamId();
        }
        if ($notificationObj->isIsRead() !== null)
        {
            $this->isRead = $notificationObj->isIsRead();
        }
        if ($notificationObj->getReadAt() !== null)
        {
            $this->readAt = $notificationObj->getReadAt();
        }
        if ($notificationObj->getCreatedAt() !== null)
        {
            $this->createdAt = $notificationObj->getCreatedAt();
        }
        return $this;
    }

    public function validate(): void
    {

    }
}