<?php
$models = $this->getParam('models', []);
$model = $this->getParam('model', null);
$rules = $model['properties'] ?? [];

$markdown = new \Parsedown();
$markdown->setSafeMode(true);

$example = function ($model, $models) use (&$example)
{
    $output = [];
    $properties = $model['properties'] ?? [];

    if(empty($properties)) {
        return new \stdClass();
    }

    foreach($properties as $key => $property) {
        $value = $property['x-example'] ?? null;
        $type = $property['type'] ?? null;
        $isArray = $type == 'array';
        $child = str_replace('#/definitions/', '', ($property['items']['$ref'] ?? ''));

        // We consider anyOf and oneOf the same in docs views
        $isXOf = false;
        $xOfKey = null;

        if(isset($property['items']['x-anyOf'])) {
            $isXOf = true;
            $xOfKey = 'x-anyOf';
        }

        if(isset($property['items']['x-oneOf'])) {
            $isXOf = true;
            $xOfKey = 'x-oneOf';
        }

        if(!empty($child) && \array_key_exists($child, $models)) {
            $childExample = $example($models[$child], $models);
            $output[$key] = $isArray ? [$childExample] : $childExample;
        }
        else if($isXOf) {
            if($isArray) {
                $output[$key] = \array_map(function($anyOfOption) use($example, $models) {
                    $anyofChild = str_replace('#/definitions/', '', ($anyOfOption['$ref'] ?? ''));
                    return $example($models[$anyofChild], $models);
                }, $property['items'][$xOfKey]);
            } else {
                $model = $property['items'][$xOfKey][0];
                $anyofChild = str_replace('#/definitions/', '', ($model['$ref'] ?? ''));
                $output[$key] = $example($models[$anyofChild], $models);
            }
        }
        else {
            $output[$key] = $value;
        }
    }

    return $output;
};
?>

<h2>Properties</h2>

<table class="vertical full args">
    <thead>
    <tr>
        <td style="width: 140px">Name</td>
        <td style="width: 120px">Type</td>
        <td>Description</td>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($rules as $key => $rule):
        $type = (isset($rule['type'])) ? $rule['type'] : '';
        $array = ($rule['type'] === 'array') ? true : false;
        $description = (isset($rule['description'])) ? $rule['description'] : '';
        $required = (isset($rule['require'])) ? $rule['require'] : false;
        $arrayType = (isset($rule['items']['type'])) ? $rule['items']['type'] : '';
        $ref = (isset($rule['items']['$ref'])) ? str_replace('#/definitions/', '', $rule['items']['$ref']) : '';

        // We consider anyOf and oneOf the same in docs views
        $isXOf = false;
        $xOfKey = null;

        if(isset($rule['items']['x-anyOf'])) {
            $isXOf = true;
            $xOfKey = 'x-anyOf';
        }

        if(isset($rule['items']['x-oneOf'])) {
            $isXOf = true;
            $xOfKey = 'x-oneOf';
        }
        ?>
        <tr>
            <td data-title="Name: ">
                <?php echo $this->escape($key); ?>
            </td>
            <td data-title="Type: ">
                <?php if(\array_key_exists($ref, $models)): ?>
                    <a href="/docs/models/<?php echo $this->escape($ref); ?>"><?php echo $this->escape($ref); ?><?php if($array): ?>[]<?php endif; ?></a>


                <?php elseif($isXOf): ?>
                    <?php
                        if($array) {
                            echo '<span>array of</span><br />';
                        }
                    ?>
                    
                    <?php
                    echo \implode('<br />', \array_map(function($xOfRule) {
                        $xOfRule = (isset($xOfRule['$ref'])) ? str_replace('#/definitions/', '', $xOfRule['$ref']) : '';
                        return '<a href="/docs/models/' . $this->escape($xOfRule) . '">' . $this->escape($xOfRule) . '</a>';
                    }, $rule['items'][$xOfKey]));
                    ?>
                <?php else: ?>
                    <?php if($array): ?>
                        <?php echo $this->escape($arrayType); ?>[]
                    <?php else: ?>
                        <?php echo $this->escape($type); ?>
                    <?php endif; ?>
                <?php endif; ?>
            </td>
            <td data-title="<?php echo (!empty($description)) ? 'Description: ' : ''; ?>"><?php echo $markdown->text($description); ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

<h2>Examples</h2>

<p><b>JSON</b></p>

<div class="ide" data-lang="json" data-lang-label="JSON">
    <pre class="line-numbers"><code class="prism language-json" data-prism><?php echo json_encode($example($model, $models), JSON_PRETTY_PRINT); ?></code></pre>
</div>

<!-- <p><b>YAML</b></p>

<div class="ide" data-lang="yaml" data-lang-label="YAML">
    <pre class="line-numbers"><code class="prism language-yaml" data-prism><?php echo yaml_emit($example($model, $models)); ?></code></pre>
</div> -->