<?php

namespace Application\Service\Extension\Xml;

class LaboratoryXmlGenerator extends FormContentXmlGenerator
{
    protected array $content = [];
    protected array $header = [];

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

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

        $studyEventData = $this->buildStudyEventDataTag($this->getStudyEventDataAttributes());

        $formData = $this->buildFormDataDataTag($this->getFormDataAttributes());

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

        return [
            $clinicalDataTag => [
                $subjectDataTag => [
                    $siteRefTag => [],
                    $studyEventData => [
                        $formData => call_user_func(function () {
                            return $this->getGroupData();
                        }),
                    ]
                ]
            ]
        ];
    }

    public function setContent(array $content): LaboratoryXmlGenerator
    {
        $this->content = $content;
        return $this;
    }

    public function setHeader(array $header): LaboratoryXmlGenerator
    {
        $this->header = $header;
        return $this;
    }

    public function setPatientId(int $patientId): LaboratoryXmlGenerator
    {
        $this->patientId = $patientId;

        return $this;
    }

    public function setFormId(int $formId): LaboratoryXmlGenerator
    {
        $this->formId = $formId;

        return $this;
    }

    public function setItemSignId(int $signId): LaboratoryXmlGenerator
    {
        $this->itemSignId = $signId;

        return $this;
    }

    public function setCheckTimeId(int $checkTimeId): LaboratoryXmlGenerator
    {
        $this->checkTimeId = $checkTimeId;

        return $this;
    }

    public function setItemId(int $itemId): LaboratoryXmlGenerator
    {
        $this->itemId = $itemId;

        return $this;
    }

    /**
     * [
     *      'itemGroupRepeatKey' => [
     *          ['itemOID' => 'INDICATORS', 'transactionType' => 'Insert', 'value' => '白细胞'], // 指标
     *          ['itemOID' => 'UNIT', 'transactionType' => 'Insert', 'value' => '10^9/L'], // 单位
     *          ['itemOID' => 'RESULT', 'transactionType' => 'Insert', 'value' => '6.31'], // 结果
     *          ['itemOID' => 'RANGE', 'transactionType' => 'Insert', 'value' => '3.5-9.5'], // 范围
     *          ['itemOID' => 'NORMAL', 'transactionType' => 'Insert', 'value' => 'true'], // 正常
     *      ],
     *      'itemGroupRepeatKey' => [
     *          ['itemOID' => 'INDICATORS', 'transactionType' => 'Insert', 'value' => '中性粒细胞绝对值'], // 指标
     *          ['itemOID' => 'UNIT', 'transactionType' => 'Insert', 'value' => '10^9/L'], // 单位
     *          ['itemOID' => 'RESULT', 'transactionType' => 'Insert', 'value' => '4.05'], // 结果
     *          ['itemOID' => 'RANGE', 'transactionType' => 'Insert', 'value' => '1.8-6.3'], // 范围
     *          ['itemOID' => 'NORMAL', 'transactionType' => 'Insert', 'value' => 'true'], // 正常
     *      ]
     * ]
     * @return array
     */
    protected function getGroupData(): array
    {
        $res = [];
        $rootTag = 'ItemData';
        $itemGroupData = $this->buildItemGroupDataTag($this->getItemGroupDataAttributes(0));

        foreach ($this->header as $item) {
            $attributesTag = $this->buildTagAttributes([
                'ItemOID' => $item['key'] . $this->getFormData()['var_name'],
                'TransactionType' => $item['transactionType'],
                'Value' => $item['value'],
            ]);
            $itemData["{$rootTag} {$attributesTag}"] = [];
            $res[$itemGroupData] = $itemData;
        }

        foreach ($this->content as $key => $contentItem) {
            $itemGroupData = $this->buildItemGroupDataTag($this->getItemGroupDataAttributes($key));
            $itemData = [];
            foreach ($contentItem as $item) {
                $itemOID = call_user_func(function () use ($item) {
                    $value = $item['itemOID'];
                    if (stripos($value, '_') !== false) {
                        $value = explode('_', $value)[0] . '_' .$this->getFormData()['var_name'];
                    } elseif (stripos($value, '-') !== false) {
                        $value = explode('-', $value)[0] . '_' .$this->getFormData()['var_name'];
                    }

                    return $value;
                });
                $attributesTag = $this->buildTagAttributes([
                    'ItemOID' => $itemOID,
                    'TransactionType' => $item['transactionType'],
                    'Value' => $item['value'],
                ]);
                $itemData["{$rootTag} {$attributesTag}"] = [];
                $this->syncField[] = $item['itemOID'];
            }

            $res[$itemGroupData] = $itemData;
            $this->multiId[] = $key;
        }

        return $res;
    }

    protected function getItemGroupDataAttributes(int $itemGroupRepeatKey = 0, $isDelete = false)
    {
        $groupTransactionType = self::TRANSACTION_TYPE_UPDATE;

        $res = [
            'ItemGroupOID' => $this->getFormData()['var_name'],
            'TransactionType' => $this->getRecordData($itemGroupRepeatKey) ? self::TRANSACTION_TYPE_UPDATE : self::TRANSACTION_TYPE_INSERT
        ];

        if ($itemGroupRepeatKey) {
            $res['ItemGroupRepeatKey'] = $itemGroupRepeatKey;
        }
//        $res['TransactionType'] = self::TRANSACTION_TYPE_INSERT;


        return $res;
    }

    protected function buildSingleFormItemData(string $data = '')
    {

    }

}