<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Exception\ValidationException;
use DMD\LaLigaApi\Entity\League;

class LeagueDto
{

    public int $id;
    public string $name;
    public string $city;
    public string $logo;
    public string $description;
    public int $matchesBetweenTeams;
    public array $blockedMatchDates;
    public bool $active;
    public array $seasonDtoList;
    public int $presidentId;
    public bool $isPublic;
    public \DateTimeImmutable $createdAt;
    public array $validationErrors;

    public function toArray(): array
    {
        $seasonIdList = [];
        if (!empty($this->seasonDtoList))
        {
            foreach ($this->seasonDtoList as $seasonDto)
            {
                $seasonIdList[] = $seasonDto->id;
            }
        }
        return [
            'id' => $this->id ?? null,
            'name' => $this->name ?? null,
            'city' => $this->city ?? null,
            'logo' => $this->logo ?? null,
            'description' => $this->description ?? null,
            'matchesBetweenTeams' => $this->matchesBetweenTeams ?? [],
            'blockedMatchDates' => $this->blockedMatchDates ?? [],
            'active' => $this->active ?? null,
            'isPublic' => $this->isPublic ?? null,
            'seasonIdList' => $seasonIdList,
            'presidentId' => $this->presidentId ?? null,
            'createdAt' => !empty($this->createdAt) ? $this->createdAt->format('Y-m-d H:i:s') : null
        ];
    }

    public function getAllLeaguesArray(): array
    {
        $seasonCount = !empty($this->seasonDtoList) ? count($this->seasonDtoList) : 0;
        return [
            'id' => $this->id ?? '',
            'name' => $this->name ?? '',
            'city' => $this->city ?? '',
            'logo' => $this->logo ?? '',
            'seasonCount' => $seasonCount,
            'description' => $this->description ?? '',
            'isPublic' => $this->isPublic ?? '',
            '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['name']))
        {
            $this->name = $dataList['name'];
        }
        if (!empty($dataList['city']))
        {
            $this->city = $dataList['city'];
        }
        if (!empty($dataList['logo']))
        {
            $this->logo = $dataList['logo'];
        }
        if (!empty($dataList['description']))
        {
            $this->description = $dataList['description'];
        }
        if (isset($dataList['matchesBetweenTeams']))
        {
            $this->matchesBetweenTeams = (int) $dataList['matchesBetweenTeams'];
        }
        if (isset($dataList['blockedMatchDates']))
        {
            $this->blockedMatchDates = $dataList['blockedMatchDates'];
        }
        if (!empty($dataList['seasonList']))
        {
            foreach ($dataList['seasonList'] as $seasonItem)
            {
                $seasonDto = new SeasonDto();
                $seasonDto->fillFromArray($seasonItem);
                $this->seasonDtoList[] = $seasonDto;
            }
        }
        if (isset($dataList['isPublic']))
        {
            $this->isPublic = (bool) $dataList['isPublic'];
        }
        if (!empty($dataList['createdAt']))
        {
            $this->createdAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dataList['createdAt']);
        }
    }

    public function fillFromObject(League $leagueObj): void
    {
        if ($leagueObj->getId() !== null)
        {
            $this->id = $leagueObj->getId();
        }
        if ($leagueObj->getName() !== null)
        {
            $this->name = $leagueObj->getName();
        }
        if ($leagueObj->getCity() !== null)
        {
            $this->city = $leagueObj->getCity();
        }
        if ($leagueObj->getDescription() !== null)
        {
            $this->description = $leagueObj->getDescription();
        }
        if ($leagueObj->getLogo() !== null)
        {
            $this->logo = $leagueObj->getLogo();
        }
        if ($leagueObj->getMatchesBetweenTeams() !== null)
        {
            $this->matchesBetweenTeams = $leagueObj->getMatchesBetweenTeams();
        }
        if ($leagueObj->getBlockedMatchDates() !== null)
        {
            $this->blockedMatchDates = $leagueObj->getBlockedMatchDates();
        }
        if ($leagueObj->getCreatedAt() !== null)
        {
            $this->createdAt = $leagueObj->getCreatedAt();
        }
        if ($leagueObj->isPublic() !== null)
        {
            $this->isPublic = $leagueObj->isPublic();
        }
        if ($leagueObj->getSeasons() !== null)
        {
            foreach ($leagueObj->getSeasons() as $seasonObj)
            {
                $seasonDto = new SeasonDto();
                $seasonDto->fillFromObject($seasonObj);
                $this->seasonDtoList[] = $seasonDto;
            }
        }
    }

    /**
     * @throws ValidationException
     */
    public function validate(): void
    {
        if (empty($this->name))
        {
            $this->validationErrors[] = 'El nombre no puede estar vacío.';
        }
        if (empty($this->city))
        {
            $this->validationErrors[] = 'La localidad no puede estar vacía.';
        }

        if (!empty($this->validationErrors))
        {
            throw new ValidationException($this->validationErrors);
        }
    }
}