<?php

namespace Fifa\ConnectId\Sdk\Util\Serialization\Configurations;

use Fifa\ConnectId\Sdk\Model\DataStandard\PictureEmbedded;
use Fifa\ConnectId\Sdk\Model\Transfer\DocumentEmbedded;
use SimpleXMLElement;

class CommonXmlSerializerConfiguration
{
    const W3XMLMIMEURL = 'http://www.w3.org/2005/05/xmlmime';

    public static function buildOptions(string $rootName, array $additionalOptions): array
    {
        return array_merge($additionalOptions,
            [
                XML_SERIALIZER_OPTION_INDENT => '',
                XML_SERIALIZER_OPTION_ROOT_NAME => $rootName,
                XML_SERIALIZER_OPTION_XML_ENCODING => 'UTF-8',
                XML_SERIALIZER_OPTION_XML_DECL_ENABLED => true,
                XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME => true,
                XML_SERIALIZER_OPTION_IGNORE_NULL => true,
                XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => '_attributes',
                XML_SERIALIZER_OPTION_CONTENT_KEY => '_content',
                XML_SERIALIZER_OPTION_LINEBREAKS => '',
                XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_SIMPLEXML,
                XML_SERIALIZER_OPTION_ROOT_ATTRIBS => [
                    'xmlns' => 'http://fifa.com/fc',
                    'xmlns:xmime' => self::W3XMLMIMEURL
                ]
            ]
        );
    }

    public static function getCustomConstructors(): array
    {
        return [
            PictureEmbedded::class => function (SimpleXMLElement $element) {
                $contentType = $element->attributes(self::W3XMLMIMEURL)->contentType;
                if($contentType !== null)
                {
                    return PictureEmbedded::createFromBase64String($element->__toString(), $contentType->__toString());
                }
                return PictureEmbedded::createFromBase64String($element->__toString(), $contentType);
            },
            DocumentEmbedded::class => function (SimpleXMLElement $element) {
                $contentType = $element->attributes(self::W3XMLMIMEURL)->contentType;
                if($contentType !== null)
                {
                    return DocumentEmbedded::createFromBase64String($element->__toString(), $contentType->__toString());
                }
                return DocumentEmbedded::createFromBase64String($element->__toString(), $contentType);
            }
        ];
    }

    public static function getPlayerRegistrationScalarAsAttributes(): array
    {
        return ['Level', 'Discipline', 'RegistrationNature', 'PersonFIFAId', 'Status', 'OrganisationFIFAId', 'RegistrationValidFrom', 'RegistrationValidTo', 'ClubTrainingCategory'];
    }

    public static function getTeamOfficialRegistrationScalarAsAttributes(): array
    {
        return ['TeamOfficialRole', 'Discipline', 'PersonFIFAId', 'Status', 'OrganisationFIFAId', 'RegistrationValidFrom', 'RegistrationValidTo'];
    }

    public static function getMatchOfficialRegistrationScalarAsAttributes(): array
    {
        return ['PersonFIFAId', 'Status', 'OrganisationFIFAId', 'RegistrationValidFrom', 'MatchOfficialRole', 'Discipline', 'RegistrationValidTo'];
    }

    public static function getOrganisationOfficialRegistrationScalarAsAttributes(): array
    {
        return ['PersonFIFAId', 'Status', 'OrganisationFIFAId', 'RegistrationValidFrom', 'OrganisationOfficialRole', 'RegistrationValidTo'];
    }

    public static function getLocalPersonNameScalarAsAttributes(): array
    {
        return ['Language', 'LastName', 'Title', 'FirstName', 'BirthName', 'PopularName'];
    }

    public static function getNationalIdentifierScalarAsAttributes(): array
    {
        return ['Identifier', 'NationalIdentifierNature', 'Country', 'DateFrom', 'DateTo', 'Description'];
    }

    public static function getEmploymentContractScalarAsAttributes(): array
    {
        return ['EmploymentContractStartDate', 'EmploymentContractEndDate'];
    }

    public static function getWorkPermitScalarAsAttributes(): array
    {
        return ['IsPermitDeclared', 'HasWorkPermit', 'WorkPermitStartDate', 'WorkPermitEndDate'];
    }

    public static function getClubScalarAsAttributes(): array
    {
        return ['ClubFIFAId', 'AssociationFIFAId', 'InternationalLongName', 'LocalName'];
    }

    public static function getResidencePermitScalarAsAttributes(): array
    {
        return ['HasResidencePermit', 'ResidencePermitStartDate', 'ResidencePermitEndDate'];
    }

    public static function getTransferFeeDetailsScalarAsAttributes(): array
    {
        return ['Amount', 'Currency', 'DateOfPayment'];
    }

    public static function getPaymentDetailsScalarAsAttributes(): array
    {
        return ['Amount', 'Currency', 'DateOfPayment'];
    }

    public static function getConditionalTransferFeeScalarAsAttributes(): array
    {
        return ['Condition'];
    }
}