<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\Facility;
use DMD\LaLigaApi\Entity\Team;
use DMD\LaLigaApi\Exception\ValidationException;

class TeamDto
{
    public int $id;
    public string $name;
    public string $dayOfWeekForHomeGame;
    public string $logo;
    public bool $active;
    public int $leagueId;
    public array $seasonDtoList;
    public array $playerDtoList;
    public \DateTimeImmutable $createdAt;
    public UserDto $captainDto;
    public array $validationErrors;

    public function toArray(): array
    {
        return [
            'id' => $this->id ?? '',
            'name' => $this->name ?? '',
            'dayOfWeekForHomeGame' => $this->dayOfWeekForHomeGame ?? '',
            'logo' => $this->logo ?? '',
            'captainName'=> isset($this->captainDto) ? $this->captainDto->firstName . ' ' . $this->captainDto->lastName : 'No asignado',
            'captainId'=> isset($this->captainDto) ? $this->captainDto->id : 'No asignado',

        ];
    }

    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['dayOfWeekForHomeGame']))
        {
            $this->dayOfWeekForHomeGame = $dataList['dayOfWeekForHomeGame'];
        }
        if (isset($dataList['active']))
        {
            $this->active = $dataList['active'];
        }
        if (isset($dataList['logo']))
        {
            $this->logo = $dataList['logo'];
        }
        if (isset($dataList['leagueId']))
        {
            $this->leagueId = (int) $dataList['leagueId'];
        }
        if (!empty($dataList['seasonList']))
        {
            foreach ($dataList['seasonList'] as $seasonItem)
            {
                $seasonDto = new SeasonDto();
                $seasonDto->fillFromArray($seasonItem);
                $this->seasonDtoList[] = $seasonDto;
            }
        }
        if (!empty($dataList['playerList']))
        {
            foreach ($dataList['playerList'] as $playerItem)
            {
                $playerDto = new PlayerDto();
                $playerDto->fillFromArray($playerItem);
                $this->playerDtoList[] = $playerDto;
            }
        }
        if (!empty($dataList['captain']))
        {
            $captainDto = new UserDto();
            $captainDto->fillFromArray($dataList['captain']);
            $this->captainDto = $captainDto;
        }
        if (!empty($dataList['createdAt']))
        {
            $this->createdAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dataList['createdAt'], new \DateTimeZone('Europe/Madrid'));
        }
    }

    public function fillFromEntity(Team $teamEntity): void
    {
        if ($teamEntity->getId())
        {
            $this->id = $teamEntity->getId();
        }
        if ($teamEntity->getName())
        {
            $this->name = $teamEntity->getName();
        }
        if ($teamEntity->getDayOfTheWeekForHomeGame())
        {
            $this->dayOfWeekForHomeGame = $teamEntity->getDayOfTheWeekForHomeGame();
        }
        if ($teamEntity->getPlayers())
        {
            foreach ($teamEntity->getPlayers() as $playerEntity)
            {
                $playerDto = new PlayerDto();
                $playerDto->fillFromObject($playerEntity);
                $this->playerDtoList[] = $playerDto;
            }
        }
        if ($teamEntity->getTeamLogo())
        {
            $this->logo = $teamEntity->getTeamLogo();
        }
        if ($teamEntity->getCaptain())
        {
            $this->captainDto = new UserDto;
            $this->captainDto->fillFromObject($teamEntity->getCaptain());
        }
        if ($teamEntity->getCreatedAt())
        {
            $this->createdAt = $teamEntity->getCreatedAt();
        }
    }

    public function validate(): void
    {
        if (empty($this->name))
        {
            $this->validationErrors[] = 'El nombre del equipo no puede estar vacío.';
        }
        if (!empty($this->validationErrors))
        {
            throw new ValidationException($this->validationErrors);
        }
    }
}