<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$this->htmlElement('body')->appendAttribute('class', 'media show');

$sectionNavs = [
    'media-metadata' => $translate('Metadata'),
];
?>

<?php echo $this->pageTitle($media->displayTitle(), 1, $translate('Media')); ?>

<div id="page-actions">
    <?php $this->trigger('view.show.page_actions', ['resource' => $media]); ?>
    <?php if ($media->userIsAllowed('update')): ?>
    <?php echo $media->link($translate('Edit media'), 'edit', ['class' => 'button']); ?>
    <?php endif; ?>
</div>

<?php echo $this->sectionNav($sectionNavs, 'view.show.section_nav', $media); ?>

<div id="media-metadata" class="active section">
    <?php echo $media->render(); ?>
    <?php if ( $media->resourceClass()): ?>
    <dl class="resource-class">
        <div class="meta-group">
            <dt><?php echo $translate('Class'); ?></dt>
            <dd class="value"><?php echo $media->resourceClass()->label(); ?></dd>
        </div>
    </dl>
    <?php endif; ?>
    <?php if ($media->resourceTemplate()): ?>
    <dl>
        <div class="meta-group">
            <dt><?php echo $translate('Resource template'); ?></dt>
            <dd class="value"><?php echo $escape($media->resourceTemplate()->label()); ?></dd>
        </div>
    </dl>
    <?php endif; ?>
    <?php echo $media->displayValues(); ?>
</div>

<div class="sidebar active">
    <div class="meta-group">
        <h4><?php echo $translate('ID'); ?></h4>
        <div class="value"><?php echo $escape($media->id()); ?></div>
    </div>
    <div class="meta-group">
        <h4><?php echo $translate('Visibility'); ?></h4>
        <div class="value"><?php echo ($media->isPublic()) ? $translate('Public') : $translate('Private'); ?></div>
    </div>
    <div class="meta-group">
        <h4><?php echo $translate('Part of item'); ?></h4>
        <?php $mediaItem = $media->item(); ?>
        <div class="value"><?php echo $mediaItem->link($mediaItem->displayTitle()); ?></div>
    </div>
    <div class="meta-group">
        <h4><?php echo $translate('Created'); ?></h4>
        <div class="value"><?php echo $escape($this->i18n()->dateFormat($media->created())); ?></div>
    </div>
    <?php if ($media->lang()): ?>
    <div class="meta-group">
        <h4><?php echo $translate('Language'); ?></h4>
        <div class="value"><?php echo $escape($media->lang()); ?></div>
    </div>
    <?php endif; ?>
    <?php if ($media->mediaType()): ?>
    <div class="meta-group">
        <h4><?php echo $translate('MIME type'); ?></h4>
        <div class="value"><?php echo $escape($media->mediaType()); ?></div>
    </div>
    <?php endif; ?>
    <?php if (is_numeric($resource->size())): ?>
    <div class="meta-group">
        <h4><?php echo $translate('Size'); ?></h4>
        <div class="value"><?php echo sprintf($translate('%s bytes'), number_format($resource->size())); ?></div>
    </div>
    <?php endif; ?>
    <div class="meta-group">
        <h4><?php echo $translate('Ingester'); ?></h4>
        <div class="value"><?php echo $escape($translate($media->ingesterLabel())); ?></div>
    </div>
    <?php if ($media->source()): ?>
    <div class="meta-group">
        <h4><?php echo $translate('Source'); ?></h4>
        <div class="value"><?php echo $escape($media->source()); ?></div>
    </div>
    <?php endif; ?>
    <?php if ($media->hasThumbnails() || $media->hasOriginal()): ?>
    <div class="meta-group">
        <h4><?php echo $translate('File Derivatives'); ?></h4>
        <?php if ($media->hasOriginal()): ?>
            <div class="value"><a href="<?php echo $escape($media->originalUrl()); ?>"><?php echo $translate('original'); ?></a></div>
        <?php endif; ?>
        <?php if ($media->hasThumbnails()): ?>
            <div class="value"><a href="<?php echo $escape($media->thumbnailUrl('large')); ?>"><?php echo $translate('large'); ?></a></div>
            <div class="value"><a href="<?php echo $escape($media->thumbnailUrl('medium')); ?>"><?php echo $translate('medium'); ?></a></div>
            <div class="value"><a href="<?php echo $escape($media->thumbnailUrl('square')); ?>"><?php echo $translate('square'); ?></a></div>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    <?php $sites = $media->item()->sites(); ?>
    <?php if (count($sites) > 0): ?>
    <div class="meta-group resource-sites">
        <h4><?php echo $translate('Sites'); ?></h4>
        <?php foreach ($sites as $site): ?>
        <div class="value">
            <?php
            $siteTitle = $site->title();
            $externalLinkText = sprintf($translate('View this media in "%s"'), $siteTitle);
            echo $site->link($siteTitle);
            echo $this->hyperlink('', $this->url('site/resource-id', ['site-slug' => $site->slug(), 'controller' => 'media', 'id' => $media->id()]),
                ['class' => 'o-icon-external', 'target' => '_blank', 'aria-label' => $externalLinkText, 'title' => $externalLinkText]);
            ?>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php $this->trigger('view.show.sidebar'); ?>
</div>

<?php $this->trigger('view.show.after'); ?>
