<?php

function hook_media_bbcode(&$codes, &$no_autolink_tags) {
    $codes[] = array(
        'tag' => 'video',
        'type' => 'unparsed_content',
        'content' => '<div class="media-video"><video data-src="$1" controls><source src="$1" /></video></div>',
        'disabled_content' => '<a href="$1" target="_blank" rel="noopener">$1</a>',
        'block_level' => true,
    );
    $codes[] = array(
        'tag' => 'audio',
        'type' => 'unparsed_content',
        'content' => '<div class="media-audio"><audio data-src="$1" controls><source src="$1" /></audio></div>',
        'disabled_content' => '<a href="$1" target="_blank" rel="noopener">$1</a>',
        'block_level' => true,
    );

    $no_autolink_tags[] = 'video';
    $no_autolink_tags[] = 'audio';
}

function hook_media_bbcode_buttons(&$bbc_tags) {
    $group_idx = 0;
    $idx = 0;
    // We want to put our media buttons next to the yt/image/etc group, so find
    // the image button
    foreach ($bbc_tags as $group) {
        $idx = 0;
        foreach ($group as $item) {
            if (isset($item['code']) && $item['code'] == 'image') {
                // done!
                break 2;
            }
            $idx++;
        }
        $group_idx++;
    }

    $media_btns = array(
        array(
            'code' => 'video',
            'description' => 'Insert a video',
        ),
        array(
            'code' => 'audio',
            'description' => 'Insert audio',
        ),
    );

    $bbc_tags[$group_idx] = array_merge(
        array_slice($bbc_tags[$group_idx], 0, $idx, true),
        $media_btns,
        array_slice($bbc_tags[$group_idx], $idx, null, true),
    );
}

function media_sce_options(&$sce_options) {
    loadJavaScriptFile('sceditor.mediabbc.js', array('minimize' => true), 'smf_sceditor_media');
    loadCSSFile('sceditor.mediabbc.css', array('minimize' => true));
}

function media_bbc_credits() {
    global $context;
    $context['credits_modifications'][] = 'Silk SVG icons by <a href="https://github.com/frhun">@frhun</a>, licensed under CC-BY-SA';
}

?>
