<?php

namespace Opentalent\OtTemplating\Domain\Repository;

use GuzzleHttp\Exception\GuzzleException;
use Opentalent\OtTemplating\Domain\Model\Member;
use Opentalent\OtTemplating\Exception\ApiRequestException;

class MemberRepository extends BaseApiRepository
{
    CONST URI = BaseApiRepository::BASE_URI . 'public/members';
    CONST URI_CA = BaseApiRepository::BASE_URI . 'public/members_ca';
    const HYDRA_TYPE = 'PortailMemberBySpeciality';
    const HYDRA_TYPE_CA = 'PortailMemberByRole';

    /**
     * Returns a Member object from an Api record
     *
     * @param $record
     * @return Member|null
     * @throws \Exception
     */
    private function memberFromJson($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'});
        $member->setStartDate(new \DateTime($record->{'startDate'}));
        $member->setEndDate(new \DateTime($record->{'endDate'}));
        $member->setInstrumentGroup($record->{'instrumentGroup'});
        $member->setInstrument($record->{'instrument'});
        $member->setMission($record->{'mission'});
        $member->setPersonId((int)$record->{'personId'});
        $member->setImage($record->{'image'});
        $member->setAdressCity($record->{'addressCity'});
        $member->setStreetAdress($record->{'streetAddress'});
        $member->setStreetAdressSecond($record->{'streetAddressSecond'});
        $member->setStreetAdressThird($record->{'streetAddressThird'});
        $member->setPostalCode($record->{'postalCode'});
        $member->setTelphone($record->{'telphone'});
        $member->setMobilPhone($record->{'mobilPhone'});
        $member->setEmail($record->{'email'});

        return $member;
    }

    /**
     * Returns an array of Member objects from an array of Api records
     *
     * @param $records
     * @return array
     * @throws \Exception
     */
    private function membersFromJson($records) {
        $members = [];
        foreach ($records as $record) {
            $members[] = $this->memberFromJson($record);
        }
        return $members;
    }

    /**
     * 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 array              Members
     * @throws ApiRequestException
     * @throws \Exception
     */
    public function findByOrganizationId(int $organizationId, bool $only_ca = false) {
        $params = [];
        $params['filter[where][organizationId]'] = $organizationId;

        if ($only_ca) {
            $records = $this->getApiRecords($this::URI_CA, $params);
        } else {
            $records = $this->getApiRecords($this::URI, $params);
        }

        return $this->membersFromJson($records);
    }

}
