<?php

namespace Application\Service\Extension\Xml;

use Application\Service\DB\Dictionary\Form;
use Application\Service\DB\Dictionary\ItemForm;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Helper\SyncPatientHelper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Xml\exceptions\InvalidDataException;
use Laminas\Validator\Uuid;

class PatientXmlGenerator extends FormContentXmlGenerator
{
    public function generate(): array
    {
        $clinicalDataTag = $this->buildClinicalDataTag($this->getClinicalDataAttributes());

        $subjectDataTag = $this->buildSubjectDataTag($this->getSubjectDataAttributes());

        $siteRefTag = $this->buildSiteRefDataTag($this->getLocationRefDataAttributes());

        return [
            $clinicalDataTag => [
                $subjectDataTag => [
                    $siteRefTag => [],
                ]
            ]
        ];
    }

    protected function getSubjectDataAttributes(): array
    {
        if (!$this->getPatientData()['patient_number']) {
            throw new InvalidDataException("受试者`{$this->patientId}`没有受试者编号");
        }

        $res = [];

        if ($this->getPatientData()['patient_number']) {
            $res['SubjectKey'] = $this->getPatientData()['patient_number'];
        }

        if ($this->getPatientData()['patient_content']['ns']['value'] ?? '') {
            $res['Initials'] = $this->getPatientData()['patient_content']['ns']['value'];
        }

        if ($this->getPatientData()['patient_content']['birthday']['value'] ?? '') {
            $res['Birthday'] = $this->getPatientData()['patient_content']['birthday']['value'];
        }

        $res['TransactionType'] = SyncPatientHelper::getIsSync($this->patientId) ? self::TRANSACTION_TYPE_UPDATE : self::TRANSACTION_TYPE_INSERT;

        return $res;
    }

    public function getSyncType()
    {
        return self::SYNC_TYPE_PATIENT;
    }

}
