<?php

namespace Fifa\ConnectId\Sdk\Util\Builder\Organisation;

use DateTime;
use Fifa\ConnectId\Sdk\Api\Model\AddressType;
use Fifa\ConnectId\Sdk\Api\Model\ComplianceAndPaymentManagerType;
use Fifa\ConnectId\Sdk\Api\Model\LocalNameType;
use Fifa\ConnectId\Sdk\Api\Model\NationalIdentifierType;
use Fifa\ConnectId\Sdk\Api\Model\OrganisationLocalType;
use Fifa\ConnectId\Sdk\Api\Model\PictureEmbeddedType;
use Fifa\ConnectId\Sdk\Api\Model\PictureLinkType;
use Fifa\ConnectId\Sdk\Api\Model\PictureType;
use Fifa\ConnectId\Sdk\Api\Model\SupportedDisciplineType;

/**
 * Class OrganisationBuilder
 * @package Fifa\ConnectId\Sdk\Util\Builder\Organisation
 */
class OrganisationBuilder implements OrganisationBuilderInterface
{
    /**
     * @var OrganisationLocalType
     */
    private $organisationType;

    /**
     * Static way of creating an instance of OrganisationBuilder
     *
     * @return OrganisationBuilder
     */
    public static function create(): OrganisationBuilder
    {
        return new OrganisationBuilder();
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->organisationType = new OrganisationLocalType();
    }

    /**
     * Gets the organisation type
     *
     * @return OrganisationLocalType
     */
    public function getOrganisationType(): OrganisationLocalType
    {
        return $this->organisationType;
    }

    /**
     * Sets organisation id
     *
     * @param string|null $fifaId
     * @return OrganisationBuilderInterface
     */
    public function setOrganisationFifaId(string $fifaId = null): OrganisationBuilderInterface
    {
        $this->organisationType->setOrganisationFifaId($fifaId);

        return $this;
    }

    /**
     * Sets parent organisation id
     *
     * @param string $parentOrganisationId
     * @return OrganisationBuilderInterface
     */
    public function setParentOrganisationId(string $parentOrganisationId): OrganisationBuilderInterface
    {
        $this->organisationType->setParentOrganisationFifaId($parentOrganisationId);

        return $this;
    }

    /**
     * Sets organisation status
     *
     * @param string $status
     * @return OrganisationBuilderInterface
     */
    public function setStatus(string $status): OrganisationBuilderInterface
    {
        $this->organisationType->setStatus($status);

        return $this;
    }

    /**
     * Sets organisation nature
     *
     * @param string $organisationNature
     * @return OrganisationBuilderInterface
     */
    public function setOrganisationNature(string $organisationNature): OrganisationBuilderInterface
    {
        $this->organisationType->setOrganisationNature($organisationNature);

        return $this;
    }

    /**
     * Sets organisation official address
     *
     * @param string $country
     * @param string $town
     * @param string $address
     * @param string|null $postalCode
     * @param string|null $region
     * @return OrganisationBuilderInterface
     */
    public function setOfficialAddress(string $country, string $town, string $address, ?string $postalCode = null, ?string $region = null): OrganisationBuilderInterface
    {
        $this->organisationType->setOfficialAddress(new AddressType(array(
            'country' => (string)$country,
            'region' => (string)$region,
            'postal_code' => (string)$postalCode,
            'town' => (string)$town,
            'address' => (string)$address,
        )));

        return $this;
    }

    /**
     * Sets organisation international name
     *
     * @param string|null $internationalName
     * @return OrganisationBuilderInterface
     */
    public function setInternationalName(?string $internationalName = null): OrganisationBuilderInterface
    {
        $this->organisationType->setInternationalName($internationalName);

        return $this;
    }

    /**
     * Sets international short name
     *
     * @param string|null $internationalShortName
     * @return OrganisationBuilderInterface
     */
    public function setInternationalShortName(?string $internationalShortName = null): OrganisationBuilderInterface
    {
        $this->organisationType->setInternationalShortName($internationalShortName);

        return $this;
    }

    /**
     * Sets web address
     *
     * @param string $webAddress
     * @return OrganisationBuilderInterface
     */
    public function setWebAddress(string $webAddress): OrganisationBuilderInterface
    {
        $this->organisationType->setWebAddress($webAddress);

        return $this;
    }

    /**
     * Sets email address
     *
     * @param string $email
     * @return OrganisationBuilderInterface
     */
    public function setEmail(string $email): OrganisationBuilderInterface
    {
        $this->organisationType->setEmail($email);

        return $this;
    }

    /**
     * Sets phone number
     *
     * @param string $phone
     * @return OrganisationBuilderInterface
     */
    public function setPhone(string $phone): OrganisationBuilderInterface
    {
        $this->organisationType->setPhone($phone);

        return $this;
    }

    /**
     * Sets fax number
     *
     * @param string $fax
     * @return OrganisationBuilderInterface
     */
    public function setFax(string $fax): OrganisationBuilderInterface
    {
        $this->organisationType->setFax($fax);

        return $this;
    }

    /**
     * Sets foundation date
     *
     * @param DateTime|null $date
     * @return OrganisationBuilderInterface
     */
    public function setFoundationDate(?DateTime $date = null): OrganisationBuilderInterface
    {
        $this->organisationType->setFoundationDate($date);

        return $this;
    }

    /**
     * Sets DissolutionDate
     *
     * @param DateTime|null $date
     * @return OrganisationBuilderInterface
     */
    public function setDissolutionDate(?DateTime $date = null): OrganisationBuilderInterface
    {
        $this->organisationType->setDissolutionDate($date);

        return $this;
    }

    /**
     * Sets logo
     *
     * @param PictureEmbeddedType $embedded
     * @param PictureLinkType $url
     * @return OrganisationBuilderInterface
     */
    public function setLogo(PictureEmbeddedType $embedded, PictureLinkType $url): OrganisationBuilderInterface
    {
        $this->organisationType->setLogo(new PictureType(array(
            'picture_embedded' => $embedded,
            'picture_link' => $url,
        )));

        return $this;
    }

    /**
     * Adds supported discipline to organisation
     *
     * @param string $discipline
     * @param string $gender
     * @return OrganisationBuilderInterface
     */
    public function addSupportedDiscipline(string $discipline, string $gender): OrganisationBuilderInterface
    {
        $supportedDiscipline = new SupportedDisciplineType();
        $supportedDiscipline->setDiscipline($discipline);
        $supportedDiscipline->setGender($gender);

        $this->organisationType->setSupportedDiscipline(array_merge(
            $this->organisationType->getSupportedDiscipline() ?: [],
            array($supportedDiscipline)
        ));

        return $this;
    }

    /**
     * Adds local name to organisation
     *
     * @param string $language
     * @param string $name
     * @param string|null $shortName
     * @return OrganisationBuilderInterface
     */
    public function addLocalName(string $language, string $name, ?string $shortName = null): OrganisationBuilderInterface
    {
        $localOrganisationName = new LocalNameType();
        $localOrganisationName->setLanguage($language);
        $localOrganisationName->setName($name);
        $localOrganisationName->setShortName($shortName);

        $this->organisationType->setLocalOrganisationName(array_merge(
            $this->organisationType->getLocalOrganisationName() ?: [],
            array($localOrganisationName)
        ));

        return $this;
    }

    /**
     * Adds national identifier to organisation
     *
     * @param string $identifier
     * @param string $nationalIdentifierNature
     * @param string $country
     * @param DateTime|null $dateFrom
     * @param DateTime|null $dateTo
     * @param string|null $description
     * @return OrganisationBuilderInterface
     */
    public function addNationalIdentifier(
        string   $identifier,
        string   $nationalIdentifierNature,
        string   $country,
        DateTime $dateFrom = null,
        DateTime $dateTo = null,
        ?string  $description = null
    ): OrganisationBuilderInterface
    {
        $nationalIdentifier = new NationalIdentifierType();
        $nationalIdentifier->setIdentifier($identifier);
        $nationalIdentifier->setNationalIdentifierNature($nationalIdentifierNature);
        $nationalIdentifier->setCountry($country);
        $nationalIdentifier->setDateFrom($dateFrom);
        $nationalIdentifier->setDateTo($dateTo);
        $nationalIdentifier->setDescription($description);

        $this->organisationType->setNationalIdentifier(array_merge(
            $this->organisationType->getNationalIdentifier() ?: [],
            array($nationalIdentifier)
        ));

        return $this;
    }

    /**
     * Sets local name
     *
     * @param string $localName
     * @return OrganisationBuilderInterface
     */
    public function setLocalName(string $localName): OrganisationBuilderInterface
    {
        $this->organisationType->setLocalName($localName);
        return $this;
    }

    /**
     * Sets local language
     *
     * @param string $localLanguage
     * @return OrganisationBuilderInterface
     */
    public function setLocalLanguage(string $localLanguage): OrganisationBuilderInterface
    {
        $this->organisationType->setLocalLanguage($localLanguage);
        return $this;
    }

    /**
     * Sets local country
     *
     * @param string $localCountry
     * @return OrganisationBuilderInterface
     */
    public function setLocalCountry(string $localCountry): OrganisationBuilderInterface
    {
        $this->organisationType->setLocalCountry($localCountry);
        return $this;
    }

    /**
     * Sets local short name
     *
     * @param string $localShortName
     * @return OrganisationBuilderInterface
     */
    public function setLocalShortName(string $localShortName): OrganisationBuilderInterface
    {
        $this->organisationType->setLocalShortName($localShortName);
        return $this;
    }

    /**
     * Sets Local System MA ID
     *
     * @param string $localSystemMaId
     * @return OrganisationBuilderInterface
     */
    public function setLocalSystemMaId(string $localSystemMaId): OrganisationBuilderInterface
    {
        $this->organisationType->setLocalSystemMaId($localSystemMaId);
        return $this;
    }

    /**
     * Sets Compliance And Payment Manager
     *
     * @param ComplianceAndPaymentManagerType $complianceAndPaymentManagerType
     * @return OrganisationBuilderInterface
     */
    public function setComplianceAndPaymentManager(ComplianceAndPaymentManagerType $complianceAndPaymentManagerType): OrganisationBuilderInterface
    {
        $this->organisationType->setComplianceAndPaymentManager($complianceAndPaymentManagerType);
        return $this;
    }

    /**
     * Sets Club Training Category
     *
     * @param string $clubTrainingCategory
     * @return OrganisationBuilderInterface
     */
    public function setClubTrainingCategory(string $clubTrainingCategory): OrganisationBuilderInterface
    {
        $this->organisationType->setClubTrainingCategory($clubTrainingCategory);
        return $this;
    }
}