<?php

namespace DMD\LaLigaApi\Dto;

use DMD\LaLigaApi\Entity\Player;

class PlayerDto
{
    public int $id;
    public string $firstName;
    public string $lastName;
    public string $position;
    public int $jerseyNumber;
    public bool $isFederated;
    public string $pictureFileName;
    public \DateTime $birthday;
    public array $fileDtoList;
    public TeamDto $teamDto;
    public \DateTime $createdAt;

    public function fillFromArray(array $dataList): void
    {
        if (isset($dataList['id']))
        {
            $this->id = $dataList['id'];
        }
        if (!empty($dataList['firstName']))
        {
            $this->firstName = $dataList['firstName'];
        }
        if (!empty($dataList['lastName']))
        {
            $this->lastName = $dataList['lastName'];
        }
        if (!empty($dataList['position']))
        {
            $this->position = $dataList['position'];
        }
        if (!isset($dataList['jerseyNumber']))
        {
            $this->jerseyNumber = (int) $dataList['jerseyNumber'];
        }
        if (isset($dataList['isFederated']))
        {
            $this->isFederated = (bool) $dataList['isFederated'];
        }
        if (isset($dataList['pictureFileName']))
        {
            $this->pictureFileName = $dataList['pictureFileName'];
        }
        if (!empty($dataList['birthday']))
        {
            $this->birthday = \DateTime::createFromFormat('Y-m-d', $dataList['birthday']);
        }
        if (!empty($dataList['fileList']))
        {
            foreach ($dataList['fileList'] as $fileItem)
            {
                $fileDto = new FileDto();
                $fileDto->fillFromArray($fileItem);
                $this->fileDtoList[] = $fileDto;
            }
        }
        if (!empty($dataList['team']))
        {
            $teamDto = new TeamDto();
            $teamDto->fillFromArray($dataList['team']);
            $this->teamDto = $teamDto;
        }
        if (!empty($dataList['createdAt']))
        {
            $this->createdAt = \DateTime::createFromFormat('Y-m-d H:i:s', $dataList['createdAt']);
        }
    }

    public function fillFromObject(Player $playerEntity): void
    {
        if ($playerEntity->getId() !== null)
        {
            $this->id = $playerEntity->getId();
        }
        if ($playerEntity->getFirstName() !== null)
        {
            $this->firstName = $playerEntity->getFirstName();
        }
        if ($playerEntity->getLastName() !== null)
        {
            $this->lastName = $playerEntity->getLastName();
        }
        if ($playerEntity->getBirthday() !== null)
        {
            $this->birthday = new \DateTime($playerEntity->getBirthday()->format('Y-m-d'),new \DateTimeZone('Europe/Madrid'));
        }
        if ($playerEntity->getJerseyNumber() !== null)
        {
            $this->jerseyNumber = $playerEntity->getJerseyNumber();
        }
        if ($playerEntity->getPosition() !== null)
        {
            $this->position = $playerEntity->getPosition();
        }
        if ($playerEntity->getPictureFileName() !== null)
        {
            $this->pictureFileName = $playerEntity->getPictureFileName();
        }
        if ($playerEntity->getTeam() !== null)
        {
            $teamDto = new TeamDto();
            $teamDto->fillFromEntity($playerEntity->getTeam());
            $this->teamDto = $teamDto;
        }
        if ($playerEntity->getFiles() !== null)
        {
            foreach ($playerEntity->getFiles() as $fileEntity)
            {
                $fileDto = new FileDto();
                $fileDto->fillFromObject($fileEntity);
                $this->fileDtoList[] = $fileDto;
            }
        }
    }

    public function validate(): void
    {

    }
}