<?php

namespace Opentalent\OtCore\Domain\Repository;

use DateTimeZone;
use GuzzleHttp\Exception\GuzzleException;
use Opentalent\OtCore\Domain\Model\Member;
use Opentalent\OtCore\Exception\ApiRequestException;

class MemberRepository extends BaseApiRepository
{
    const URI_TRAILING_PART = 'public/members';
    const URI_TRAILING_PART_CA = 'public/members_ca';
    const HYDRA_TYPE = 'PortailMemberBySpeciality';
    const HYDRA_TYPE_CA = 'PortailMemberByRole';

    protected function getApiUriCa(): string
    {
        return $this->getApiUri(self::URI_TRAILING_PART_CA);
    }

    /**
     * Get the members of the organization
     * If $only_ca is true, returns only the members onf the CA
     *
     * @param int $organizationId The id of the organization
     * @param bool $only_ca If true, returns the CA members instead of all the members
     * @return ApiPagedCollection              Members
     * @throws ApiRequestException
     */
    public function findByOrganizationId(int $organizationId,
                                         $only_ca = false) {
        $params = [];
        $params['filter[where][organizationId]'] = $organizationId;
        $params['itemsPerPage'] = '200';

        if ($only_ca) {
            return $this->getApiRecords($params, $this->getApiUriCa());
        } else {
            return $this->getApiRecords($params);
        }
    }

    /**
     * Returns a Member object from an Api record
     *
     * @param $record
     * @return Member|null
     * @throws \Exception
     */
    protected function memberToObject(array $record) {
        if ($record['@type'] != $this::HYDRA_TYPE &&
            $record['@type'] != $this::HYDRA_TYPE_CA) {
            return null;
        }
        $member = new Member();
        $a = explode('/', $record['@id']);
        $member->setId(end($a));
        $member->setOrganizationId((int)$record['organizationId']);
        $member->setGender($record['gender']);
        $member->setGivenName($record['givenName']);
        $member->setName($record['name']);
        $dateStart = new \DateTime($record['datetimeStart']);
        $dateStart->setTimezone(new \DateTimeZone('Europe/Paris'));
        $member->setStartDate($dateStart);
        $dateEnd = new \DateTime($record['datetimeStart']);
        $dateEnd->setTimezone(new \DateTimeZone('Europe/Paris'));
        $member->setEndDate($dateEnd);
        $member->setInstrumentGroup($record['instrumentGroup']);
        $member->setInstrument($record['instrument']);
        $member->setMission($record['mission']);
        $member->setPersonId((int)$record['personId']);
        $member->setImage($record['image']);
        $member->setAddressCity($record['addressCity']);
        $member->setStreetAddress($record['streetAddress']);
        $member->setStreetAddressSecond($record['streetAddressSecond']);
        $member->setStreetAddressThird($record['streetAddressThird']);
        $member->setPostalCode($record['postalCode']);
        $member->setTelphone($record['telphone']);
        $member->setMobilPhone($record['mobilPhone']);
        $member->setEmail($record['email']);

        return $member;
    }
}
